first of all: THANK YOU for taking your time to sit down and write an example. this is a great starting point for me. now i can modify and change this into something that i need.
but the update doesn work.
foreach ($orders as $key => $value)
{
if (!$value && in_array($key, $_POST['order_packad']))
{
$on[] = $value;
$orders[$key] = 1;
}
if ($value && !in_array($key, $_POST['order_packad']))
{
$off[] = $value;
$orders[$key] = 0;
}
}
if ($on) {mysql_query('UPDATE orders SET order_packad = 1 WHERE orders_id IN(' . implode(',', $on) . ')') or exit(mysql_error());}
if ($off) {mysql_query('UPDATE orders SET order_packad = 0 WHERE orders_id IN(' . implode(',', $off) . ')') or exit(mysql_error());}
}
in this section, isnt the orderID in the $key and the 1 or 0 in the $value??
$orders is something like this?
$key $value
13 0
31 0
41 1
but what does this code do?
if (!$value && in_array($key, $_POST['order_packad']))
{
$on[] = $value;
$orders[$key] = 1;
}
doesnt it try to set the key to 1? the key should remain the orderID key for the update to work?
Lars
changed it to:
foreach ($orders as $key => $value)
{
if (!$value && in_array($key, $_POST['order_packad']))
{
$on[] = $key;
$orders[$value] = 1;
}
if ($value && !in_array($key, $_POST['order_packad']))
{
$off[] = $key;
$orders[$value] = 0;
}
}
and now the 41 that was previously set in the DB disapears when not checked, and pressing submit. so the update set to 0 seems to work. but i cant get any new orders to be merked.. im on it :-)
Lars
hmmm...changed it back.....
i put a print_r($orders) just above the for each lopp and heres whats outputted when pressing a few boxes and submit:
Array ( [13] => 0 [18] => 0 [31] => 0 [32] => 0 [38] => 0 [39] => 0 [40] => 0 [41] => 0 [42] => 0 [43] => 0 [44] => 0 [45] => 0 [46] => 0 [47] => 0 )
lars
made a few print_r just before the sql update section:
foreach ($orders as $key => $value)
{
if (!$value && in_array($key, $_POST['order_packad']))
{
$on[] = $value;
$orders[$key] = 1;
}
if ($value && !in_array($key, $_POST['order_packad']))
{
$off[] = $value;
$orders[$key] = 0;
}
}
echo "$orders ";
print_r($orders);
echo "<br>$on " ;
print_r($on);
echo "<br>$off " ;
print_r($off);
if ($on) {mysql_query('UPDATE orders SET order_packad = 1 WHERE orders_id IN(' . implode(',', $on) . ')') or exit(mysql_error());}
if ($off) {mysql_query('UPDATE orders SET order_packad = 0 WHERE orders_id IN(' . implode(',', $off) . ')') or exit(mysql_error());}
and heres what i get. (press a few and submit)
Array Array ( [13] => 0 [18] => 0 [31] => 0 [32] => 0 [38] => 0 [39] => 0 [40] => 0 [41] => 0 [42] => 0 [43] => 0 [44] => 0 [45] => 0 [46] => 0 [47] => 0 )
Array Array ( )
Array Array ( )
the data submitted from the form is catched in the $order_packad = $_POST['order_packad']; so just to see WHAT comes to I print_r the $order_packad as the first array: when pressing 3 buttons i get this result:
order_packad Array ( [0] => 1 [1] => 1 [2] => 1 )
orders Array ( [13] => 0 [18] => 0 [31] => 0 [32] => 0 [38] => 0 [39] => 0 [40] => 0 [41] => 0 [42] => 0 [43] => 0 [44] => 0 [45] => 0 [46] => 0 [47] => 0 [48] => 0 )
on Array ( )
off Array ( )
so- the checkbox sends not the orderid, it sends only the values 1 for pressed buttons. and not their id... kind of hard to compare the 2 different types of keys?
I would like to make the checkbox send id as well or actually the id is probably the only thing neede , for the in_array check?
foreach ($orders as $key => $value)
{
echo '<input type="checkbox" name="order_packad[]" value="1"';
if ($value) {echo ' checked';}
echo '> ' . $key . '<br>';
}
perhaps if i output the $key in either name="order_packad[]" OR value="1" ? like:
input type="checkbox" name="order_packad[<? echo $key; ?>]" value="1"'
hmmm I cant make this work.
i realize that the in_array doesnt compare the keys...it searches the values in the array for a match. so i cant use the keys to hold my orderID.
and I cant pass the orderid through the checkbox value. it needs to be 0 or 1.
Lars