Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a loop which increments each form input name

Status
Not open for further replies.

schoch

Technical User
Jan 17, 2007
13
0
0
AU
Hi, I want to loop a form name. Incrementing it so that each field has the same input name with a number.
It just isn't working, I am new to php so really need some help!
Code:
 <?php

for ($cnt=1; $cnt<=; $i++)
{
  echo "$cnt";
}
?>        
    
    <tr> <td> <?php do { ?>
        <input name="day<?php echo $cnt; ?>" value="<?php echo $row_days['day_name']; ?>">
        </td>
 <td> <input name="mail_drop<?php echo $cnt; ?>" type="text" id="mail_drop" value="" size="10"></td>
   <td><input name="phone_canvas<?php echo $cnt; ?>" type="text" id="phone_canvas" size="10" value=""> </td>
   <td><input name="emails_sent<?php echo $cnt; ?>" type="text" id="emails_sent" size="10" value=""></td>
   <td><input name="appointments_made<?php echo $cnt; ?>" type="text" id="appointments_made" size="10" value=""></td>
   <td><input name="presentations<?php echo $cnt; ?>" type="text" id="presentations" size="10" value=""></td>
   <td><input name="bills_analysed<?php echo $cnt; ?>" type="text" id="bills_analysed" size="10" value=""></td>
   <td><input name="orders<?php echo $cnt; ?>" type="text" id="orders" size="10" value=""></td>
   <td><input name="deliveries<?php echo $cnt; ?>" type="text" id="deliveries" size="10" value=""></td>
   <td><input name="referrals<?php echo $cnt; ?>" type="text" id="referrals" size="10" value=""></td>
 <?php } while ($row_days = mysql_fetch_assoc($days)); ?>
 </tr>
 
if you have 20 forms

Code:
for ($i=0; $i<20; $i++){
 echo '<input type="text" name="somecontrol_'.$i.'" />';
 //etc
}
 
PHP has a feature where you can have form values appear as the elements of an array.

If you have a POST-method form which contains fields named:

<form type="text" name="foo[1]">
<form type="text" name="foo[2]">
<form type="text" name="foo[3]">
<form type="text" name="foo[4]">
<form type="text" name="foo[5]">

Then $_POST['foo'] will itself be an array containing 5 elements. This method generally makes referencing the values easier.

Outputting the fields in a loop can then be something like:

Code:
for ($i=0; $i<20; $i++){
 echo '<input type="text" name="somecontrol['.$i.']" />';
 //etc
}



Want the best answers? Ask the best questions! TANSTAAFL!
 
so if I use the array foo. Will they be inserted into the database with separate ids?
 
The for loop doesn't seem to be working? What have I done wrong?
Code:
 <?php do { ?>
       <tr> <td>    

       <input name="day<?php echo $cnt; ?>" value="<?php echo $row_days['day_name']; ?>">
 <?php } while ($row_days = mysql_fetch_assoc($days)); ?>
        </td> <td>
<?php
for ($i=0; $i<20; $i++){
 echo '<input type="text" name="mail_drop['.$i.']" />';
 echo '<input type="text" name="phone_canvas['.$i.']" />';
}
?>
   </td>
 </tr>

  
	<tr><Td><input name="Submit" type="submit" value="submit" /></Td></tr>
  </table>
    <input type="hidden" name="MM_insert" value="form1">
</form>
 
First of all, do...while loops are not appropriate for use with mysql_fetch_* calls. The call needs to happen before the first run through the loop, not after.

Second, "doesn't seem to be working" is vague. What, exactly, isn't working?

Third, I see this line:

<input type="hidden" name="MM_insert" value="form1">

from which I infer you're using DreamWeaver. I recommend strongly against it.


Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top