BTW:
I didn't notice that you were working with <select> fields, silly me, (that is what you meant by 'listboxes', right?) so often I miss these little important details in the posts. Ah, you probably realize that working with <select> fields is very similar, you have a select field where multiple values may be selected, the submitted data is automatically formatted in a numbered array corresponding to each selected value, in which case, the example that I provided would work identically as far as the update database example is concerned.
Code:
<?php
//form stuff
echo "<select name=\"row_id\" size=\"3\" multiple=\"multiple\">";
while ($data=mysql_fetch_array($data_result)) {
echo "<option value=\"{$data["row_id"]}\">{$data["row_id"]}</option>";
}
echo "</select>";
// more form stuff
multiple=\"multiple\" is BTW the XHTML compliant method of writing that attribute.
Upon submission, the user has clicked option 1 and option 3
The corresponding array should look like this:
$_POST["row_id"][0] = (value data for option 1)
$_POST["row_id"][1] = (value data for option 3)
Though I say this in theory only - I've never worked with multiple select boxes, but its an educated guess that they would output in this manner. If you want to be absolutely sure what the key value is, just echo it out when you're running through the loop. Which doesn't really matter anyway, the list() and each() functions will keep it all straight!
And you said they all have the same name? That you are going to have to change! If you have more than one form element with the exact same name the data from each new element will overwrite the last. If you are looping to recreate the <select> fields like this...
Lets say we have a database table of poems, each user may have multiple poems, so we'll generate a table of poems for each user and define a loop to go through the selected ones and set permission to yes.
The database table looks like this:
submission_id
user_id
poem_title
poem_body
lastaccesstime
permission
Code:
//form stuff
$users_result = mysql_query("SELECT * FROM poems");
while ($data = mysql_fetch_array($users_result)) {
echo "<select name=\"users[{$data["user_id"]}]\">";
$poems_result = mysql_query("SELECT * FROM poems WHERE user_id = '{$data["user_id"]}'");
while ($poems_data = mysql_fetch_array($poems_result)) {
echo "<option value=\"{$poems_data["submission_id"]}\">{$poems_data["poem_title"]}</option>";
}
echo "</select>";
}
// more form stuff
Now each users field is really a multi-dimensional array.
Each would look like this:
$_POST["users"][234][0] corresponds to option 1 for user '234'
$_POST["users"][234][1] corresponds to option 2 for user '234'
Two submissions were selected by user 234.
So to access this mess we're going to need a double loop:
while(list($user_key, $user_value) = each($_POST["users"])) {
if (is_array($_POST["users"][$user_key])) {
while(list($key, $value) = each($_POST["users"][$user_key])) {
$update = mysql_query("UPDATE poems SET permission = 'Y' WHERE submission_id = '{$value}' AND user_id = '{$user_key}'");
if (!$update) {
echo mysql_error();
echo mysql_errno();
} else {
echo "Record: #{$value} updated successfully!<br />";
}
}
}
}
I haven't tested this code but I think its theory is pretty sound. There may be some parse errors in there but I think it does the trick.
: )
Rich
:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::