Hi
Well, I’ve cracked the problem. HTML Forms can’t deal directly with arrays of data. So if
arrays have to be sent, they have to be sent element by element as NAME:DATA pairs. All NAMES
must be unique.
To explain it, Suppose we have a 4-column name & address table shown here as a HTML form with a single submit button
with a value of ‘updating’:-
chkbox ID first last address
X 9 Michael Greveson 109, Glen Rd
… other records…
X 39 Bradwell Johnson 37 Edgar St
updating
The ‘X’ at the start of every row is a checkbox; if it is ticked the checkbox acquires an integer
number equal to the record’s ID. One or more of the check boxes may be ticked thus generating a HTML
input statement for example:-
<input type=‘checkbox’ name=‘chk.$id’ value=‘$id’>
When the submit button is clicked, a name-value pair is sent to the server for each of the checked
rows of the table. If the 2 rows above were checked the name-value pairs are “chk9=>9, chk39=>39”.
From the received $_POST array, an array $idary can be extracted containing, in this example,
the elements : (0=>9, 1=>39) (note the actual names of the checkboxes can be any unique strings,
we only need the elements of the function array_values($_POST)).
The next step is to initiate a update-data entry form to capture the data (2 sets of 4 data
items in the example) to generate SQL INPUT statements for each of the 2 records.
A 2 row x 4 column array ( e.g. $updata) is needed so that, for example the element $updata[0][4]
is the the NEW value of Mr Greveson’s address (currently ‘109, Glen Rd’). Similarly, $updata[1][2] is
the value replacing ‘Bradwell’.
Since a HTML form can only send name-pairs where the name is unique, it is necessary to add
a suffix to the names of the fields. Thus I’ve added a suffix :‘$idary[0]’ (for the first record to be edited) and
'$idary[1]’ (for the 2nd) to the field names. Thus a typical input statement in the update-data entry form is:-
$ind : <input type=“Text” name=“‘. $ind.’_‘.$iuset.’” value="'. $row[$iuset][$ind]
(Where $ind = field-name[$i}, $i between 0 and field count;
$iuset = the index number of the set of update-data - [0 or 1 in the example]
$row[$iuset] = $row[$iuset] = mysql_fetch_array($result); [ i.e. the row of data items
in the $iuset’th row being updated].)
In a bit more detail the skeleton code for the modifyrecord form is:-
function modifyrecform2($idary){ // $idary array contains list of IDs for records to be edited
print(‘<form method=“post” action="’ . $SELF. ‘">’);
for ($iuset = 0; $iuset < count($idary); $iuset++) { // iterate over all sets (0 and 1 in example)
$idx = $idary[$iuset];
if ($idx) { // $irec’th row in the update set
$sql = “SELECT * FROM $tblname WHERE id=$idx”;
$result = mysql_query($sql);
$row[$iuset] = mysql_fetch_array($result);
}
print (‘<input type=hidden name="’ . $fldnames[0] . ‘_’.$iuset .‘" value="’ . $idx . ‘">’);
print(“<h3> Updating Record $idx</h3>”);
for ($i=1; $i <= $colcount -1; $i++) {
$ind = $fldnames[$i];
print("$ind");
echo '<input type="Text" name="'. $ind.'_'.$iuset.'" value="'. $row[$iuset][$ind] .'">';
}
} // for $irec
print (‘<input type=“Submit” name=“submit” value=“updating”>’);
echo “/form>”;
Note that update-data entry forms are provided for each update set (2 in this case), but only one
submit button
When the edits are made and the submit button clicked, the received $_POST array is:-
$_POST = array(‘id_0’=>‘9’,‘first_0’=>‘Michael’,‘last_0’=>‘Greveson’,‘address_0’=>‘109,Glen Rd’,
‘id_1’=>‘39’,‘first_1’=>‘Bradwell’,‘last_1’=>‘Johnson’,‘address_1’=>‘37 Edgar St’,
‘submit’=>‘updating’) [except that some of the data here will have been edited.]
This is parsed with the following code to generate a 2-row array ($data):-
$updata = array();
foreach( $POST as $key => $value) {
if ( ereg('[0-9]+', $key, $regs )){
$of = ltrim($regs[0], '');
$b = ereg_replace("[0-9]+", “”, $key);
$updata[$of][$b] = $value;
}else{;
The code removes the suffices to the ‘NAME’ elements in the 2 row array:-
$updata = array(0 ,
array (‘id’=>‘9’,‘first’=>‘Michael’,‘last’=>‘Greveson’,‘address’=>‘109, Glen Rd’)
1, array(‘id’=>‘38’,‘first’=>‘Bradwell’,‘last’=>‘Johnson’,‘address’=>‘37 Edgar St’ ))
SQL UPDATE statements can easily be constructed for the 2 records from the elements of
array_values($data[0]) and array_values($data[1]) e.g. :-
$sql = “UPDATE $tblname SET “first=‘Michael’,last’”=”‘Greveson’,address=‘109, Glen Rd’" WHERE id=9";
and $sql = “UPDATE $tblname SET “first=‘Bradwell’,last’”=”‘Johnson’,address=‘37 Edgar St’" WHERE id=39";
I’ve found this useful for updating 10 records in tables ( of members of a club) with 900 records and 16 fields. Its very much faster to make edits to records 5 at a time rather than singly.
chelse