Re: Form Loop

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, 2008-10-19 at 14:10 +0530, Sudheer 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>";
> ?>
> <td><input type="text" id="qty[<?php echo $x; ?>]" name="quantity[<?php 
> echo $x; ?>]"  size="2" value="$row[qty]"/></td>
> <?php
> 
> >     echo "</tr>";
> >     $x++;
> > }
> >
> > the name value quantity needs the value of x appended to it. 
> > quantity_1, quantity_2 etc.
> I recommend using array notation instead of appending $x to the element 
> name. If you are using POST, when the form is submitted the values are 
> available in the $_POST['quantity'] array. You can use foreach to 
> iterate through it.
> 
> Example:
> <?php
> $values = $_POST['quantity'];
> foreach ($values as $value) {
>     echo $value;
> }
> ?>
> 
> 
> Regards,
> 
> -- 
> 
> With warm regards,
> Sudheer. S
> Business: http://binaryvibes.co.in, Community: http://lampcomputing.com, Personal: http://sudheer.net
> 
> 
There is no real need to use IDs for form elements unless you are
attaching labels to them. If you're using JavaScript to check for
certain values, then you can iterate the elements of the form in array
notation also. One more thing, you don't need to put explicit values
into form elements that use array notation, it's enough to put 

<input type="text" name="quantity[]" size="2" value="{$row['qty']}"/>

Note the curly braces, you must use these if the HTML is being output by
PHP, or it will not know that the variable you are trying to use is an
array element.


Ash
www.ashleysheridan.co.uk


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux