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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.