Re: variable variables i think... (solved)

From: Date: Thu, 22 Aug 2002 06:39:56 +0000
Subject: Re: variable variables i think... (solved)
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-113594@lists.php.net to get a copy of this message
Solved it -- I *can* do what I wanted without variable variables... it was human error. Justin French on 22/08/02 4:09 PM, Justin French (justin@indent.com.au) wrote: > Hi all, > > Having trouble with the logic behind this. > > I have a dynamic SKU, and a dynamic size_range array. > > Examples: > > $sku = '44044'; > $size_range = array('S', 'M', 'L'); > > > Which I use to build a pull down select box: > > <SELECT name="myselect"> > <? > foreach($sizes as $k => $v) > { > echo "<OPTION value=\"$v\">$v</OPTION>"; > } > ?> > </SELECT> > > > Note, the select's name is just a string now, not an array. Sinxe there are > many SKU's on the page, What I'd like to do is dynamically generate this > name, so that each SKU is part of an array. Eg: > > $selected_size['44044'] = 'M'; > $selected_size['44045'] = 'S'; > $selected_size['44046'] = 'L'; > etc etc > > This will make processing the data HEAPS easier. > > > So I need to dynamically generate the SELECT's name: > > <SELECT name="selected_size['<?=$sku?>']> > > Easy. > > > BUT, since this form may be spat back out to the user if fields were left > blank, etc etc, I like to do something like: > > <SELECT name="myselect"> > <? > foreach($sizes as $k => $v) > { > $myselect = $_POST['myselect']; > if($myselect == $v) { $sel = " selected"; } else { $sel = ""; } > echo "<OPTION value=\"{$v}\"{$sel}>{$v}</OPTION>"; > } > ?> > </SELECT> > > Which ensures that any values they did select will still be selected when > they get given the form again to make changes. > > Trouble is, with a dynamically assigned SELECT name, I have no idea how to > test for an existing value. > > I thought it'd be something like: > > <SELECT name="myselect['<?=$sku?>']"> > <? > foreach($sizes as $k => $v) > { > $myselect["$sku"] = $_POST['myselect']["$sku"]; > if($myselect["$sku"] == $v) { $sel = " selected"; } else { $sel = > ""; } > echo "<OPTION value=\"{$v}\"{$sel}>{$v}</OPTION>"; > } > ?> > </SELECT> > > But this isn't working. My guess is that I need variable variables, or some > other way of referencing the select's name. > > Any ideas welcome. > > > > Justin French >

« previous php.general (#113594) next »