FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Variable variables?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Variable variables? [message #182261] Fri, 26 July 2013 02:56 Go to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
I have a situation which I 'think' I need to use variable variables and
not sure how to go about it.


I have several <select multiple> elements on a search form with their
names created dynamically and the options created dynamically.

$parent holds the name such as "Women", "Mens" etc..
$c_id holds category_id and is sent off as the 'value' of the option.

This form is self calling and an array is created for the multiple
option selected.

Women => array([0]=>20,[1]=>21,...)
Mens => array([0]=>32,[1]=>35,...)

I am using in_array() to check returned posted values against an id.

How do I reference that array by using the value of $parent.

if $parent is 'Women' I need to reference the array 'Women'

Here is a nutshell of the algorithm.

foreach($parent_ary as $parent) {
echo "<select {$parent}[] multiple>";
foreach($category_ary as $c_id=>$category) {
if(in_array($c_id, array of posted value)) {
// Create select option
} else {
// Create regular option
}
}
echo "</select>";
}

I hope this makes sense and yes I did RTM but could not transpose what
they where explaining to what I need to do.

Thanks
Scotty
Re: Variable variables? [message #182265 is a reply to message #182261] Fri, 26 July 2013 10:56 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 7/25/2013 10:56 PM, Scott Johnson wrote:
> I have a situation which I 'think' I need to use variable variables and
> not sure how to go about it.
>
>
> I have several <select multiple> elements on a search form with their
> names created dynamically and the options created dynamically.
>
> $parent holds the name such as "Women", "Mens" etc..
> $c_id holds category_id and is sent off as the 'value' of the option.
>
> This form is self calling and an array is created for the multiple
> option selected.
>
> Women => array([0]=>20,[1]=>21,...)
> Mens => array([0]=>32,[1]=>35,...)
>
> I am using in_array() to check returned posted values against an id.
>
> How do I reference that array by using the value of $parent.
>
> if $parent is 'Women' I need to reference the array 'Women'
>
> Here is a nutshell of the algorithm.
>
> foreach($parent_ary as $parent) {
> echo "<select {$parent}[] multiple>";
> foreach($category_ary as $c_id=>$category) {
> if(in_array($c_id, array of posted value)) {
> // Create select option
> } else {
> // Create regular option
> }
> }
> echo "</select>";
> }
>
> I hope this makes sense and yes I did RTM but could not transpose what
> they where explaining to what I need to do.
>
> Thanks
> Scotty

I'm not sure why you think you need variable variables (another sucky
idea on PHP's part, IMHO). Build your array with the field name as the
key and the value as the data. Then you can access it as

foreach ($array as $key=>$value)

If each key contains multiple values, then $value is just another array.

Just be sure to validate your incoming data. Such constructions are
more susceptible to be hacked unless you're very careful.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Variable variables? [message #182267 is a reply to message #182265] Fri, 26 July 2013 19:39 Go to previous messageGo to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
On 7/26/2013 3:56 AM, Jerry Stuckle wrote:
> On 7/25/2013 10:56 PM, Scott Johnson wrote:
>> I have a situation which I 'think' I need to use variable variables and
>> not sure how to go about it.
>>
>>
>> I have several <select multiple> elements on a search form with their
>> names created dynamically and the options created dynamically.
>>
>> $parent holds the name such as "Women", "Mens" etc..
>> $c_id holds category_id and is sent off as the 'value' of the option.
>>
>> This form is self calling and an array is created for the multiple
>> option selected.
>>
>> Women => array([0]=>20,[1]=>21,...)
>> Mens => array([0]=>32,[1]=>35,...)
>>
>> I am using in_array() to check returned posted values against an id.
>>
>> How do I reference that array by using the value of $parent.
>>
>> if $parent is 'Women' I need to reference the array 'Women'
>>
>> Here is a nutshell of the algorithm.
>>
>> foreach($parent_ary as $parent) {
>> echo "<select {$parent}[] multiple>";
>> foreach($category_ary as $c_id=>$category) {
>> if(in_array($c_id, array of posted value)) {
>> // Create select option
>> } else {
>> // Create regular option
>> }
>> }
>> echo "</select>";
>> }
>>
>> I hope this makes sense and yes I did RTM but could not transpose what
>> they where explaining to what I need to do.
>>
>> Thanks
>> Scotty
>
> I'm not sure why you think you need variable variables (another sucky
> idea on PHP's part, IMHO). Build your array with the field name as the
> key and the value as the data. Then you can access it as
>
> foreach ($array as $key=>$value)
>
> If each key contains multiple values, then $value is just another array.
>
> Just be sure to validate your incoming data. Such constructions are
> more susceptible to be hacked unless you're very careful.
>

Well I was thinking of variable variables because.....I ran out of ideas
and it seemed like a way to go? ;)

I will give what you suggest a go, seems like a simple enough approach.

Thanks
Scotty
Re: Variable variables? [message #182268 is a reply to message #182261] Fri, 26 July 2013 20:04 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 26-07-2013 04:56, Scott Johnson wrote:
> I have a situation which I 'think' I need to use variable variables and
> not sure how to go about it.
>
>
> I have several <select multiple> elements on a search form with their
> names created dynamically and the options created dynamically.
>
> $parent holds the name such as "Women", "Mens" etc..
> $c_id holds category_id and is sent off as the 'value' of the option.
>
> This form is self calling and an array is created for the multiple
> option selected.
>
> Women => array([0]=>20,[1]=>21,...)
> Mens => array([0]=>32,[1]=>35,...)
>
> I am using in_array() to check returned posted values against an id.
>
> How do I reference that array by using the value of $parent.
>
> if $parent is 'Women' I need to reference the array 'Women'
>
> Here is a nutshell of the algorithm.
>
> foreach($parent_ary as $parent) {
> echo "<select {$parent}[] multiple>";
> foreach($category_ary as $c_id=>$category) {
> if(in_array($c_id, array of posted value)) {
> // Create select option
> } else {
> // Create regular option
> }
> }
> echo "</select>";
> }
>
> I hope this makes sense and yes I did RTM but could not transpose what
> they where explaining to what I need to do.
>
> Thanks
> Scotty

This will return 'test':
$a="test"; $b="a"; print ${$b};

so, if $parent="Woman", then ${$parent} should refer to the $Woman[],
and if $parent="Mens" then ${$parent} should refer to $Mens[]
Re: Variable variables? [message #182269 is a reply to message #182261] Fri, 26 July 2013 20:17 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
Scott Johnson wrote:

> if $parent is 'Women' I need to reference the array 'Women'

The concept of variable variables is quite simple:

$women = array(/* ... */);
$parent = 'women';
$$parent == $women; // TRUE

I don't consider this concept "sucky" per se, but I would recommend to
avoid overusing it, as it easily leads to unwieldy code due to its
dynamic nature:

$a = 1;
$b = 'a';
$c = 'b';
$$$c; // 1
$c = 'a';
$$$c; // Notice: Undefined variable: 1

However, sometimes it comes in quite handy, for instance:

foreach (array('foo', 'bar', 'baz') as $var) {
$$var = $_POST[$var];
}

--
Christoph M. Becker
Re: Variable variables? [message #182270 is a reply to message #182268] Sat, 27 July 2013 03:27 Go to previous messageGo to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
On 7/26/2013 1:04 PM, Luuk wrote:
> On 26-07-2013 04:56, Scott Johnson wrote:
>> I have a situation which I 'think' I need to use variable variables and
>> not sure how to go about it.
>>
>>
>> I have several <select multiple> elements on a search form with their
>> names created dynamically and the options created dynamically.
>>
>> $parent holds the name such as "Women", "Mens" etc..
>> $c_id holds category_id and is sent off as the 'value' of the option.
>>
>> This form is self calling and an array is created for the multiple
>> option selected.
>>
>> Women => array([0]=>20,[1]=>21,...)
>> Mens => array([0]=>32,[1]=>35,...)
>>
>> I am using in_array() to check returned posted values against an id.
>>
>> How do I reference that array by using the value of $parent.
>>
>> if $parent is 'Women' I need to reference the array 'Women'
>>
>> Here is a nutshell of the algorithm.
>>
>> foreach($parent_ary as $parent) {
>> echo "<select {$parent}[] multiple>";
>> foreach($category_ary as $c_id=>$category) {
>> if(in_array($c_id, array of posted value)) {
>> // Create select option
>> } else {
>> // Create regular option
>> }
>> }
>> echo "</select>";
>> }
>>
>> I hope this makes sense and yes I did RTM but could not transpose what
>> they where explaining to what I need to do.
>>
>> Thanks
>> Scotty
>
> This will return 'test':
> $a="test"; $b="a"; print ${$b};
>
> so, if $parent="Woman", then ${$parent} should refer to the $Woman[],
> and if $parent="Mens" then ${$parent} should refer to $Mens[]
>
>
>
>
>

Ahhhh.

I saw that in the manual but could not wrap my head around it. Thanks
very much.

Scotty
Re: Variable variables? [message #182271 is a reply to message #182269] Sat, 27 July 2013 07:37 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Christoph Michael Becker wrote:

> Scott Johnson wrote:
>> if $parent is 'Women' I need to reference the array 'Women'
>
> The concept of variable variables is quite simple:
>
> $women = array(/* ... */);
> $parent = 'women';
> $$parent == $women; // TRUE
>
> I don't consider this concept "sucky" per se, but I would recommend to
> avoid overusing it, as it easily leads to unwieldy code due to its
> dynamic nature:

ACK.

> $a = 1;
> $b = 'a';
> $c = 'b';
> $$$c; // 1
> $c = 'a';
> $$$c; // Notice: Undefined variable: 1

Thanks, I did not know that you could have variables named β€œ1” this way :)

$ php -r '$a = 1; $b = "a"; $c = "b"; $$$c = 42; echo $1 . PHP_EOL;'
PHP Parse error: syntax error, unexpected '1' (T_LNUMBER), expecting
variable (T_VARIABLE) or '$' in Command line code on line 1

$ php -r '$a = 1; $b = "a"; $c = "b"; $$$c = 42; echo $$$c . PHP_EOL;'
42

Another interesting fact that I have learned (or have been reminded of) in
the ZCE (PHP 5.3) preparation class recently:

$ php -r '$a = 42; $b = "a";

echo $$b . PHP_EOL;
echo "$$b" . PHP_EOL;
echo "${$b}" . PHP_EOL;
echo "{$$b}" . PHP_EOL;'
42
$a
42
42


PointedEars
--
When all you know is jQuery, every problem looks $(olvable).
Re: Variable variables? [message #182280 is a reply to message #182267] Sat, 27 July 2013 13:48 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 7/26/2013 3:39 PM, Scott Johnson wrote:
> On 7/26/2013 3:56 AM, Jerry Stuckle wrote:
>> On 7/25/2013 10:56 PM, Scott Johnson wrote:
>>> I have a situation which I 'think' I need to use variable variables and
>>> not sure how to go about it.
>>>
>>>
>>> I have several <select multiple> elements on a search form with their
>>> names created dynamically and the options created dynamically.
>>>
>>> $parent holds the name such as "Women", "Mens" etc..
>>> $c_id holds category_id and is sent off as the 'value' of the option.
>>>
>>> This form is self calling and an array is created for the multiple
>>> option selected.
>>>
>>> Women => array([0]=>20,[1]=>21,...)
>>> Mens => array([0]=>32,[1]=>35,...)
>>>
>>> I am using in_array() to check returned posted values against an id.
>>>
>>> How do I reference that array by using the value of $parent.
>>>
>>> if $parent is 'Women' I need to reference the array 'Women'
>>>
>>> Here is a nutshell of the algorithm.
>>>
>>> foreach($parent_ary as $parent) {
>>> echo "<select {$parent}[] multiple>";
>>> foreach($category_ary as $c_id=>$category) {
>>> if(in_array($c_id, array of posted value)) {
>>> // Create select option
>>> } else {
>>> // Create regular option
>>> }
>>> }
>>> echo "</select>";
>>> }
>>>
>>> I hope this makes sense and yes I did RTM but could not transpose what
>>> they where explaining to what I need to do.
>>>
>>> Thanks
>>> Scotty
>>
>> I'm not sure why you think you need variable variables (another sucky
>> idea on PHP's part, IMHO). Build your array with the field name as the
>> key and the value as the data. Then you can access it as
>>
>> foreach ($array as $key=>$value)
>>
>> If each key contains multiple values, then $value is just another array.
>>
>> Just be sure to validate your incoming data. Such constructions are
>> more susceptible to be hacked unless you're very careful.
>>
>
> Well I was thinking of variable variables because.....I ran out of ideas
> and it seemed like a way to go? ;)
>
> I will give what you suggest a go, seems like a simple enough approach.
>
> Thanks
> Scotty

The salient point here is - you KNOW what names SHOULD be on your form.
You may have several fields, but you know what fields are available
(even if you're only using a subset of those fields).

You need to control what you have on the form. What you DON'T want is a
hacker to post his/her own form with data which should not normally be
available.

OTOH, if you're creating field names dynamically because you don't know
how many you will have, you should be using an array. For instance, if
you're looking for restaurants and want to have checkboxes for various
nationalities, the nationalities available should be all be in the same
array.

The reason I think variable variables are sucky is because they almost
always lead to hard to understand and hard to maintain code. Data
validation becomes much more difficult and often leads to holes in the
code which can be used by hackers - especially when the info is coming
from the user.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Variable variables? [message #182289 is a reply to message #182280] Sat, 27 July 2013 14:45 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <kt0imo$2rg$1(at)dont-email(dot)me>,
Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:

> The reason I think variable variables are sucky is because they almost
> always lead to hard to understand and hard to maintain code.

Quite right.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
Re: Variable variables? [message #182290 is a reply to message #182289] Sat, 27 July 2013 14:46 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 27/07/13 15:45, Tim Streater wrote:
> In article <kt0imo$2rg$1(at)dont-email(dot)me>,
> Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:
>
>> The reason I think variable variables are sucky is because they
>> almost always lead to hard to understand and hard to maintain code.
>
> Quite right.
>
I thought that was PERL and regexps?

(I'll get my coat).


--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.
Re: Variable variables? [message #182291 is a reply to message #182290] Sat, 27 July 2013 14:53 Go to previous message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <kt0mf9$pl9$2(at)news(dot)albasani(dot)net>,
The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote:

> On 27/07/13 15:45, Tim Streater wrote:
>> In article <kt0imo$2rg$1(at)dont-email(dot)me>,
>> Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:
>>
>>> The reason I think variable variables are sucky is because they
>>> almost always lead to hard to understand and hard to maintain code.
>>
>> Quite right.
>>
> I thought that was PERL and regexps?

Yes, we'll be arresting them too.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Query with PHP and MySql
Next Topic: Dynamically changing links in a web page menu when a link is clicked
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 09 17:10:53 GMT 2024

Total time taken to generate the page: 0.04423 seconds