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.
Using arrays with multiple form elements [SOLVED]
Moderator: General Moderators
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Using arrays with multiple form elements [SOLVED]
Last edited by aceconcepts on Mon Nov 19, 2007 4:15 am, edited 1 time in total.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Quick hack but should get you started.
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.
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>";
}
}
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Don't use $_SERVER['PHP_SELF']. For reasons, search our forums. I would use basename(__FILE__) instead.
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London