Jump to content

dynamically created variables


nikefido

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/93531-dynamically-created-variables/
Share on other sites

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
?>

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.

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.

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 />";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.