Jump to content

dynamic variables?


interpim

Recommended Posts

So, here is the problem I need to find a solution for...

 

I have a form, which entry into is repetitive, although not all parts of the form will be required to be filled in.

 

basically i have the following values.

 

alt1, class1

alt2, class2

alt3, class3

alt4, class4 etc...

 

so I want to cycle through these, and assign these to variables when the page is processed, if they contain data.

How can I build a variable name using for each of these without physically typing a block of code for each numbered name since the type of data will be the same.

 

So far I have this

 

$j = 1;
while($j < 10){
$var = "alt" . $j;

#This is where I cannot figure out how to get the value in $var to actually be the variable...


$j++;
}

 

Am I on the right track?  and if so, what am I missing here?

Link to comment
https://forums.phpfreaks.com/topic/122226-dynamic-variables/
Share on other sites

<form method='post' action='build_userpage.php'>
SERVER:  <input type='text' name='server'>
Main Character: <input type='text' name='main'>Class: <input type='text' name='main_class'>
Alt: <input type='text' name='alt1'>Class: <input type='text' name='alt1_class'>
Alt: <input type='text' name='alt2'>Class: <input type='text' name='alt2_class'>
Alt: <input type='text' name='alt3'>Class: <input type='text' name='alt3_class'>
Alt: <input type='text' name='alt4'>Class: <input type='text' name='alt4_class'>
etc.....
<input type='submit' value='create'></form>

Link to comment
https://forums.phpfreaks.com/topic/122226-dynamic-variables/#findComment-631070
Share on other sites

Change to

<form method='post' action='build_userpage.php'>
SERVER:  <input type='text' name='server'>
Main Character: <input type='text' name='main'>Class: <input type='text' name='main_class'>
Alt: <input type='text' name='alt[1]'>Class: <input type='text' name='alt1_class'>
Alt: <input type='text' name='alt[2]'>Class: <input type='text' name='alt2_class'>
Alt: <input type='text' name='alt[3]'>Class: <input type='text' name='alt3_class'>
Alt: <input type='text' name='alt[4]'>Class: <input type='text' name='alt4_class'>
etc.....
<input type='submit' value='create'></form>

Then you can use

<?php
for ($i=1;$i<5;$i++)
   echo $_POST['alt'][$i] . '... ';
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/122226-dynamic-variables/#findComment-631089
Share on other sites

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.