nikefido Posted February 28, 2008 Share Posted February 28, 2008 I don't see this issue with PHP ever - I have a set of variables with names "pic1" "pic2"..."pic10"..."pic32" (they aren't created dynamically, but they might as well have been with their naming scheme...) anyway - I was wondering if there was a way to refer to them dynamically - in a "for loop"...that way I don't have to list out every single variable "by hand". for($i = 1; $i <= 32; $i++) { echo $pic . $i; //is it this simple?? $output .= '<p>Picture Name: '.$pic.$i.'</p>'; //could this work? } Please let me know if this is unclear! Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 28, 2008 Share Posted February 28, 2008 almost you can assign variable names with variables eg: <?php $pic1 = "Hello"; $variablename = "pic1"; echo($$variablename); ?> this will echo "Hello" eg: <?php for($i=1; $i<=32; $i++) { $name = "pic".$i; echo $$name; // it is this simple! $output .= '<p>Picture Name: '.$$name.'</p>'; // Yes it could } hope this helps ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 28, 2008 Share Posted February 28, 2008 Accessing a variable variable is 2 to 3 times slower than accessing an array element. Whenever possible, an array should be used. Putting same type data that is part of a set with more than about three elements into an array will always been more efficient than assigning it to sequentially named variables. Quote Link to comment Share on other sites More sharing options...
nikefido Posted February 28, 2008 Author Share Posted February 28, 2008 well it's form data coming from a list of 32 sets of radio buttons (each set containg 3 radio options). I have in the past tried to name the radio buttons after an array as you would with checkboxes. Perhaps that would work (i don't remember that working before, however). Do you know if you can do that with radio options? - each set has to have a different name, so you run into obvious problems. In any case, the application of this will not be heavy in numbers of users so the speed is not a huge factor. Nor will server load, as it will be used less than once a day. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 28, 2008 Share Posted February 28, 2008 Make your radio buttons like this (this also lends itself to a dynamically generated form using a loop instead of typing/copy-pasting) - <input type="radio" name="pic[1]" value="1"> Choice 1 <br /> <input type="radio" name="pic[1]" value="2"> Choice 2 <br /> <input type="radio" name="pic[1]" value="3"> Choice 3 <br /> <br /> <input type="radio" name="pic[2]" value="1"> Choice 1 <br /> <input type="radio" name="pic[2]" value="2"> Choice 2 <br /> <input type="radio" name="pic[2]" value="3"> Choice 3 <br /> ... repeat as needed (or use a code loop to dynamically generate all the picks) Then you can simply iterate through all the values - foreach($_POST['pic'] as $key => $value) { echo "Pic$key was: $value<br />"; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.