Forum Moderators: coopster

Message Too Old, No Replies

Problem updating my cart

Problem with foreach($_POST)

         

Lebman

7:41 am on Apr 24, 2004 (gmt 0)

10+ Year Member



I would like to get some help on a problem that I am facing. If anyone has a solution I would appreciate it. What I am trying to do is to update my cart after viewing the cart. Unfortunately I have not been successful so far, and I am getting the following error:

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\show_order.php on line 21

My code looks like this:
(line 21 is the foreach line, it seems as if it does not like $_POST['qty'], but I could be wrong).

if(isset($_POST['submit5']))
{
foreach ($_POST['qty'] as $key => $value)
{
if ( ($value == 0) AND (is_numeric ($value)) )
{
unset ($_SESSION['cart'][$key]);
} elseif ( is_numeric ($value) AND ($value > 0) )
{
$_SESSION['cart'][$key] = $value;
}
}

later on in my code I have the following code to create an input element of text type:

echo " <tr>

<td align=\"left\"><a name=\"".$item."\" href=\"show_item.php?itemid={$row['item_id']}\">{$row['name']}</a><br> {$row['description']}</td>

<td align=\"left\"> <input type=\"text\" name=\"qty[{$row['item_id']}]\" value=\"{$_SESSION['cart'][$row['item_id']]}\"></td>
</tr>\n";

Thanks,
Lebman

jatar_k

5:01 pm on Apr 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Lebman,

It would seem the the value you are passing to foreach isn't an array or isn't a pair.

Have you tried echoing the value of $_POST['qty']? It looks like $_POST['qty'] would be a single value which is not what foreach is looking for.

Elijah

5:13 pm on Apr 24, 2004 (gmt 0)

10+ Year Member



On thing I have found helpful in working with mith multi-dimensional arrays it to print it out so I can see exactly how it looks.
Try this if you would like:

echo '<pre>';
print_r($_POST);
echo '</pre>';

Lebman

3:38 pm on Apr 25, 2004 (gmt 0)

10+ Year Member



I tried
echo '<pre>';
print_r($_POST['qty']);
echo '</pre>';

and I do not see anything printed out. I looked at other code and saw similar code used elswhere.

All suggestions are welcomed

Thx,
Lebman

jatar_k

4:00 pm on Apr 25, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well, if nothing printed out then it looks like it may not be set, or may not be an array, which would be a good reason why foreach is complaining as well.

Try the same code but with
echo '<pre>';
print_r($_POST);
echo '</pre>';

and then see if you can see the value of qty.

Lebman

4:15 am on Apr 26, 2004 (gmt 0)

10+ Year Member



I tried that also, but I do not see anything for qty.
I compared it to some sample code, it looks the same. Actually I do get the same result with that sample code, even though I thought it should work properly.

Any other ideas/ or maybe suggestions to workaround that?

thx

jatar_k

4:42 pm on Apr 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



At this point since qty doesn't seem to be set you need to find out why. Take a look at the form posting the qty and make sure the fieldname is exactly the same as the varname.

Also take a look at where it is being set and try to figure out why it isn't working. It looks like the answer will be farther back in the chain of events and once you find it you can work your way forward again.