Using arrays with multiple form elements [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Using arrays with multiple form elements [SOLVED]

Post 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.
Last edited by aceconcepts on Mon Nov 19, 2007 4:15 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Hmmmm. Where are the products saved? A database? XML?
rturner
Forum Newbie
Posts: 24
Joined: Sun Nov 04, 2007 1:39 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Don't use $_SERVER['PHP_SELF']. For reasons, search our forums. I would use basename(__FILE__) instead.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Thanks for all the posts. Most helpful.

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

Thanks.
Post Reply