i have page simple list of items pulled mysql table. each of items falls 1 of 2 categories. in database have "status" column. items either in status 1 or status 0. 1 each group.
on each of items on page i've added checkbox primary key row in table. within each item in given category on page. have hidden field $_post['status'] set correspond group it's in. code below, have status array , check_list (which primary key) merging correctly key , value pairs.
i want people able check checkboxes in both groups , have status change 1 button click moving item 1 group other. have 3 items i've been bouncing around between 2 groups.
everything in group 1 works fine. can move 1 item @ time until empty, 2 items @ time, , 3 @ once no issue. group 0 not working. can move 3 items @ time no issue, can move 2 items simultaneously no problem, can move 1 item 1 @ time once. second time around, nothing budges. if select both of remaining items in group, first 1 moves.
it's strange behavior. think has way if statements executed in foreach loop, i'm not sure how solve it.
function combine_arr($a, $b) { $acount = count($a); $bcount = count($b); $size = ($acount > $bcount) ? $bcount : $acount; $a = array_slice($a, 0, $size); $b = array_slice($b, 0, $size); return array_combine($a, $b); } if(isset($_post['move'])) { if(!empty($_post['check_list'])) { $combined = combine_arr($_post['check_list'], $_post['status']); foreach ($combined $key => $value) { if($value == 1) { mysqli_query($con, "update items set status=0 id=$key"); } if($value == 0) { mysqli_query($con, "update items set status=1 id=$key"); } } } }
here relevant html on page starting button initiates action...
<form action="index.php" method="post" id="itemcheck"> <button form="itemcheck" class="button_blue" type="submit" name="move" value="move">move<span>selected</span></button> </form>
...then 1 of checkboxes in first group...
<input type="hidden" form="itemcheck" name='status[]' value='1'> <input type="checkbox" form="itemcheck"name="check_list[]" value="<?php echo $row['id']; ?>">
...and checkbox in second group identical except value of hidden field.
<input type="hidden" form="itemcheck" name='status[]' value='0'> <input type="checkbox" form="itemcheck"name="check_list[]" value="<?php echo $row['id']; ?>">
the way you're combining 2 parameter arrays won't work. checked boxes submitted, $_post['check_list'][$n]
doesn't correspond $_post['status'][$n]
. should change html name of status
input includes row id:
<input type="hidden" form="itemcheck" name="status[<?php echo $row['id']; ?>" value="0">
then can combine them this:
function combine_arr($a, $b) { $result = array(); foreach ($a $val) { $result[$val] = $b[$val]; } return $result; }
Comments
Post a Comment