开发者

php mysql parallel array checkboxes

I have an array of checkboxes that I edit at once to set up a 'tinyint' field. the problem comes in when i uncheck the checkbox and post the vales to mysql. since it posts an array of checkboxes and another parallel array of values to edit, unchecking a checkbox results in the 0 value been ignored by PHP_POST and hence the checkbox array will be less by the number 开发者_如何学Cof unchecked values in the form while the array to be edited will have all the records in the form.

here is the submit code

while($row=mysql_fetch_array($result))
{

$checked = ($row[active]==1) ? 'checked="checked"' : '';
...

echo "<input type='hidden' name='TrID[]' value='$TrID'>";
echo "<input type='checkbox' name='active1[]' value='$row[active]''$checked' >";
...

and the processing php script

$userid = ($_POST['TrID']);
$checked= ($_POST['active']);

$i=0;

foreach ($userid as $usid) 
{

if ($checked[$i]==1){
$check = 1;
}
else{
$check = 0;
}

$qry1 ="UPDATE  `epapers`.`clientelle` SET  `active` =  '$check' WHERE  `clientelle`.`user_id` =  '$usid' ";
$result = mysql_query($qry1);   
$i++;

}


You could try something like:

$i = 0;
while($row=mysql_fetch_array($result))
{

$i++;
$checked = ($row[active]==1) ? 'checked="checked"' : '';
...

echo "<input type='hidden' name='TrID[{$i}]' value='$TrID'>";
echo "<input type='checkbox' name='active1[{$i}]' value='$row[active]''$checked' >";
...

The checkboxes should then at least be in the proper place in the array...


You can use the key of the first array to fetch the correct element of the second...

$userid = ($_POST['TrID']);
$checked= ($_POST['active']);

foreach ($userid as $key => $usid) 
{

if ($checked[$key]==1){
$check = 1;
}
else{
$check = 0;
}

$qry1 ="UPDATE  `epapers`.`clientelle` SET  `active` =  '$check' WHERE  `clientelle`.`user_id` =  '$usid' ";
$result = mysql_query($qry1);   

}

EDIT If this doesn't work, try to add numeric indexes to the element names in the HTML code. Like this:

echo "<input type='hidden' name='TrID[".$i."]' value='$TrID'>";
echo "<input type='checkbox' name='active1[".$i++."]' value='$row[active]''$checked' >";

You'd obviously have to set $i to zero before the loop.


Checkboxes shouldn't have the same name, but each one should be named differently. You could solve your problem by modifying the first page like this:

$i = 0;
while($row=mysql_fetch_array($result))
{

$checked = ($row[active]==1) ? 'checked="checked"' : '';
...

echo "<input type='hidden' name='TrID[$i]' value='$TrID'>";
echo "<input type='checkbox' name='active1[$i]' value='$row[active]''$checked' >";
$i++;
...

And accordingly adjust the second script...


I have my own script I use to accomplish this... see if you can adapt it or if I'm anywhere on track with what you're trying to do.

Schema: Rows of products each with a checkbox that User can check in order to signify that that product needs to be added to the cart in order to see the price.

Code:
<td><input type="checkbox" name="addforprice[<?php echo $record['id']; ?>]" /></td>

Then when the form is submitted:

if($_POST['addforprice']){
foreach(array_keys($_POST['addforprice']) as $var){
$data['add_for_price'] = 1;
$update = $db->query_update(TABLE_PRODUCTS, $data, "id=".$var);
echo "addforprice:".$var.' '.$data['add_for_price'].'
';
unset($data['addforprice']);
}
}

$record['id'] is the database id generated when printing out the rows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜