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