Page 1 of 1

Using arrays with multiple form elements [SOLVED]

Posted: Fri Nov 16, 2007 8:57 am
by aceconcepts
Hi,

I have a list of products which include code, name, description fields.

What i want to be able to do is edit details of each record and then update all records by submitting the form.

How do I go about setting up the arrays for this update?

Thanks.

Posted: Fri Nov 16, 2007 11:14 am
by feyd
I'd probably use a multidimensional array approach. Each record would dump into their own array, and the record as a whole would be added to a unifying array of all the records so simply looping is possible. A record of the original state of the data will be needed so you can update the records correctly.

Posted: Fri Nov 16, 2007 11:29 am
by aceconcepts
Yes, this sounds like an ideal solution.

My problem is how do I create an array for each record i.e. code, name, description in the same array as the data for each record should be updated in an associative fashio (if that makes sense).

I have attempted this and all I get is the last record data.

Posted: Fri Nov 16, 2007 11:58 am
by Jonah Bron
Hmmmm. Where are the products saved? A database? XML?

Posted: Fri Nov 16, 2007 11:35 pm
by rturner
Quick hack but should get you started.

Code: Select all

<?php

if ($_SERVER['REQUEST_METHOD'] == "GET") {

print <<<eod
<html>
<head>
</head>
<body>
eod;
?>
<form name="items" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
<tr>
        <td>
        Item Code
        </td>
        <td>
        Description
        </td>
        <td>
        Cost
        </td>
</tr>
<?php
$x=0;
$item1 = array('item' => "U2AU34",
                'description' => "Untreated Lumber",
                'cost' => 12.52);

$item2 = array('item' => "Z1Z3Y1",
                'description' => "Pallet",
                'cost' => 6.22);

$master = array($item1, $item2);

foreach ($master as $array) {
        $x++;
        print "<tr>";
        print "<td><input type=text name=\"item[]\" value=$array[item] readonly></td>";
        print "<td><input type=\"text\" name=\"description[]\" value=\"$array[description]\"></td>";
        print "<td><input type=\"text\" name=\"cost[]\" value=\"$array[cost]\"></td>";
print "</tr>\n";
}
print "</table>";
print "<input type=\"submit\" value=\"Submit\"></form></body></html>";


}else{

$items = $_POST['item'];
$descriptions = $_POST['description'];
$costs = $_POST['cost'];


for ($x=0; $x < count($items); $x++) {
        print "$items[$x]" . " : " . $descriptions[$x] . " : " . $costs[$x] . "<br>";
}

}

?>
Usually a class would be defined to handle one item at a time but if I heard you right you want to dump multiple items on the same form and be able to modify and write back all of them. Hope this helps get you started.

Posted: Sat Nov 17, 2007 9:07 am
by RobertGonzalez
Don't use $_SERVER['PHP_SELF']. For reasons, search our forums. I would use basename(__FILE__) instead.

Posted: Mon Nov 19, 2007 4:14 am
by aceconcepts
Thanks for all the posts. Most helpful.

I went with rturner's code. Works very well...i love the thinking.

Thanks.