Re: Form Loop

From: Date: Sun, 19 Oct 2008 01:07:26 +0000
Subject: Re: Form Loop
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-282089@lists.php.net to get a copy of this message
Terry J Daichendt wrote: > I'm trying to create a form with a loop. I need to append a value to a > field name each time through the loop. For Instance: > > while ($row = mysql_fetch_assoc($result)) { > $x=1; > echo "<tr>"; echo "<td><input type='text' > id='qty' > name='quantity_' size='2' value='$row[qty]' > /></td>"; > echo "</tr>"; > $x++; > } > > the name value quantity needs the value of x appended to it. quantity_1, > quantity_2 etc. What is the correct syntax to achieve this, especially > the parsing to get it to work. I suspect the dot operator to append it > but I can't get the parsing down. > > Terry Daichendt echo '<td><input type="text" id="qty" name="quantity_' . $x . '" size="2" value="' . $row['qty']. '" /></td>'; However, I would use an array: echo '<td><input type="text" id="qty" name="quantity[' . $x . ']" size="2" value="' . $row['qty']. '" /></td>'; Depending upon your use, you can even leave out the index and let it increment. echo '<td><input type="text" id="qty" name="quantity[]" size="2" value="' . $row['qty']. '" /></td>'; -- Thanks! -Shawn http://www.spidean.com

« previous php.general (#282089) next »