Warning: Invalid argument supplied for foreach() in

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Warning: Invalid argument supplied for foreach() in

Postby littleimps » 22. June 2010 11:52

Hi,

I am doing a shopping cart and found to have this error with i cant solve at the moment. I have been stuck for days. Can anyone help me to solved the problem i am facing. Million Thanks in advance.

The error found was in the foreach:
foreach($_SESSION['countID'] as $ProdId => $Quality).

Code: Select all

<?php

session_start();
If($_SESSION['countID']=="")
{
   header( "refresh:5;url=index.html" );
}
else
{
      
    $ProQty   = $_POST["selectQty"];
   
    if(isset($_POST["ProdISDN"]))
       $ProdId = $_POST["ProdISDN"];
       else
       $ProdId = 1;
       
    if(isset($_POST["AddCart"]))
       $Action = $_POST["AddCart"];
       else
       $Action = "Empty";
       
       
    switch($Action)
    {
      case"Add";
         if(isset($_SESSION['countID'][$ProdId]))
             $_SESSION['countID'][$ProdId]++;
          else
             $_SESSION['countID'][$ProdId]=1;
       break;
       
       case"Remove":
          if(isset($_SESSION['countID'][$ProdId]))
          {
            $_SESSION['countID'][$ProdId]--;
            if($_SESSION['countID'][$ProdId] == 0)
               unset($_SESSION['countID'][$ProdId]);
         }
       break;
       
       case "Empty":
          unset($_SESSION['countID'][$ProdId]);
          break;

   }
   
}
   If(isset($_SESSION['countID']))
   {
    echo "Welcome:".$_SESSION['countID'];
    echo "<br>Qty:".$ProQty;
    echo "<br>ISDN:".$ProdId;
   
    require ("connect.php" );
 
   //retrieve record
   
   
   echo '<h1 align = "center" >View Cart</h1>
<table width="500" border="1" align="center">';
      $Total = 0;
      
      foreach($_SESSION['countID'] as $ProdId => $Quality)
      {
      $retrieveTravelRec = mysql_query ("Select * FROM bookdatabase.product_table WHERE ProdID = $ProdId") or die (mysql_error());

      $numberOfRows = mysql_num_rows($retrieveTravelRec);
      
      $PName = $numberOfRows['ProdName'];
      $PPrice = $numberOfRows['ProdPrice'];
      $Cost = $PPrice * $Quality;
      
      $Total = $Total +$Cost;
      
      echo '<tr>';
       echo '<td width="100">'.$Pname.'</td>';
       echo '<td width="50">$'.$PPrice.'</td>';
       echo '<td width="50">Quantity:
         <select name="selectQty" id="selectQty">
        <option value="0" selected="selected">0</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
    </select></td>';
       echo '<td width="50">$'.$Cost.'</td>';
       echo '</tr>';

      }
   echo '<tr>';
   echo '<td align "right">'.$Total.'</td>';
   echo '</tr>';
   echo '</table>';
      
    }
    else
       echo "Cart is empty";
     
?>


littleimps
 
Posts: 7
Joined: 01. June 2010 17:28

Re: Warning: Invalid argument supplied for foreach() in

Postby Nobbie » 22. June 2010 12:29

You dont know how to debug scripts?

Insert echo's, prints and so and find out, what's going on. In that special case, you might insert a

print_r($_SESSION);

in order to find out, if the Session Array contains the expected values.
Nobbie
 
Posts: 8779
Joined: 09. March 2008 13:04

Re: Warning: Invalid argument supplied for foreach() in

Postby littleimps » 22. June 2010 12:42

Hi,

I am new to PHP programming, therefore i still have yet to master debuggin yet. I am less than a month old in PHP.

Most of my code is base on reading(google), youtube, etc. what ever i need, i go and read them up.

But as for this error, i cant find any solutions. Pls help.

I did the debug, as the result is ==> Array ( [countID] => 2 )

Thanks.
littleimps
 
Posts: 7
Joined: 01. June 2010 17:28

Re: Warning: Invalid argument supplied for foreach() in

Postby littleimps » 22. June 2010 13:10

I did a debuggin and the error is still the same.

Code: Select all
$Quality = 10;
print_r($_SESSION);
echo "<br>XProduct:".$ProdId;
echo "<br>XQty:".$Quality;


The Outputs was:
Array ( [countID] => 2 )
XProduct:20
XQty:10
Warning: Invalid argument supplied for foreach() in
littleimps
 
Posts: 7
Joined: 01. June 2010 17:28

Re: Warning: Invalid argument supplied for foreach() in

Postby Nobbie » 22. June 2010 14:53

littleimps wrote:The Outputs was:
Array ( [countID] => 2 )


Ok, that's it. That means, that $_SESSION['countID'] = 2, and 2 is only a single integer value, but in the foreach() statement you need an Array as argument (2 is not an Array).
Nobbie
 
Posts: 8779
Joined: 09. March 2008 13:04

Re: Warning: Invalid argument supplied for foreach() in

Postby littleimps » 22. June 2010 15:23

Nobbie wrote:
littleimps wrote:The Outputs was:
Array ( [countID] => 2 )


Ok, that's it. That means, that $_SESSION['countID'] = 2, and 2 is only a single integer value, but in the foreach() statement you need an Array as argument (2 is not an Array).


Sorry, i dont really get it. Can you show me how it can be done?
Thanks.
littleimps
 
Posts: 7
Joined: 01. June 2010 17:28

Re: Warning: Invalid argument supplied for foreach() in

Postby Nobbie » 22. June 2010 17:44

No.

I dont know your script, I can only tell you that $_SESSION['countID'] is NOT an array and therefore you cannot use it in the foreach()-Statement. It's your job to find out, why you expect to have an array in $_SESSION['countID'] and why there is no array.
Nobbie
 
Posts: 8779
Joined: 09. March 2008 13:04


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 43 guests