php - Creating a Loop To get info from form Where the amount of fields can vary -


i have form allows users register database. can register group of 6 @ time. webpage displays 2 forms. first (usernumberform) drop down list user picks how many names register. gets posted , when page reloads 2nd form (userinfoform) runs while loop populate correct number of fields user enter information in. contains hidden field value usernumberform stored. when userinfoform gets posted first thing have adduser.php (the page posts to) check number of users , store in variable called usernum. have switch statement based on variable rest of information form , sanitize data.

case 2:      $firstname1=$_post['firstname1'];     $firstname2=$_post['firstname2'];      $firstname1 = stripslashes($firstname1);     $firstname1 = mysql_real_escape_string($firstname1);     $firstname2 = stripslashes($firstname2);     $firstname2 = mysql_real_escape_string($firstname2); break;  case 3:      $firstname1=$_post['firstname1'];     $firstname2=$_post['firstname2'];     $firstname3=$_post['firstname3'];      $firstname1 = stripslashes($firstname1);     $firstname1 = mysql_real_escape_string($firstname1);     $firstname2 = stripslashes($firstname2);     $firstname2 = mysql_real_escape_string($firstname2);             $firstname3 = stripslashes($firstname3);     $firstname3 = mysql_real_escape_string($firstname3);  break; //... , on 

as can see starting lot of code. hoping there way set this:

$x = 1; while($x < $usernum) { $firstname.$x=$_post['firstname.$x']; $firstname.$x = stripslashes($firstname.$x); $firstname.$x = mysql_real_escape_string($firstname.$x); ++$x; } 

obviously doesn't work, format looking for. don't understand variable variables yet, or maybe i'm making more complicated needs , there way stick in array.

yes while statement can used. remember append $x outside string when accessing variable $_post - i.e. $_post['firstname' . $x]; (though use double-quoted strings, since the variables expanded: $_post["firstname$x"];). looping condition needs updated less or equal (i.e. <=) - otherwise last name not processed.

so update condition :

 while($x < $usernum) 

to:

while($x <= $usernum) 

then instead of using $firstname.$x variable, use single name - e.g. $firstname. long consistent, used iteration separately.

$x = 1; while($x <= $usernum) {     $firstname = $_post['firstname' . $x];     $firstname = stripslashes($firstname);     $firstname = mysql_real_escape_string($firstname);     //use $firstname insert database, or store in temporary structure     ++$x; } 

but for statement used. can reduce number of lines needed, variable iterate (i.e. $x) started , incremented in 1 line.

for ($x = 1; $x <= $usernum; $x++) {     $firstname = $_post['firstname' . $x];     $firstname = stripslashes($firstname);     $firstname = mysql_real_escape_string($firstname);     //use $firstname insert database, or store in temporary structure } 

and taking 1 step further, foreach statement can used range(). structure, variable doesn't need incremented manually , there no need looping condition (i.e. $x <= $usernum).

foreach (range(1, $usernum) $x) {     $firstname = $_post['firstname' . $x];     $firstname = stripslashes($firstname);     $firstname = mysql_real_escape_string($firstname);     //use $firstname insert database, or store in temporary structure } 

or maybe i'm making more complicated needs , there way stick in array

yes store names in array, this:

$firstnames = array(); foreach (range(1, $usernum) $x) {     $firstnames[$x] = $_post['firstname' . $x]; } //use $firstnames later add values database  

see demonstration of in this phpfiddle.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -