By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

Nested PHP loops

P: 50
Hi Guys

I'm from an ASP SQLserver background trying to learn PHP MySQL etc. I have a problem with PHP fOR LOOPS as follows, on my proposed site I have six different catalogues which the user can choose from, there is also the users basket (cart) which is saved for a period of time after the user logs off, in case they return. What I am trying to do is when the user requests a new calalogue is to loop through the ProductID items in the basket and compare it to the ProductID in the catalogue if there is a match set the InBasket column to 1. When the catalogue is displayed a short message is displayed under the catalogue item advising that this item is currently in their basket and also to NOT display the buy button, these goods are not the sort you would want to buy more than one of.

Data from MySQL

$_SESSION['Catalogue'][] = array(
'ProductID' => $row['ProductID'],
'CatName' => $row['CatName'],
'DepartmentID' => $row['DepartmentID'],
'CatPrice' => $row['CatPrice'],
'CatLocation' => $row['CatLocation'],
'DepartmentName' => $row['DepartmentName'],
'Discount' => $row['Discount'],
'InBasket' => $row['InBasket']);

$_SESSION['CartItems'][] = array(
'BasketID' => $row['BasketID'],
'ProdName' => $row['ProdName'],
'ProductID' => $row['ProductID'],
'Quantity' => $row['Quantity'],
'ProdPrice' => $row['ProdPrice'],
'Location' => $row['Location'],
'Discount' => $row['Discount'],
'countrytax' => $row['countrytax']);


for ($i = 0; $i < count($_SESSION['CartItems']); $i++):
{
for ($x = 0; $x < count($_SESSION['Catalogue']); $x++):
{
If ($_SESSION['CartItems'][$i]['ProductID'] == $_SESSION['Catalogue'][$x]['ProductID'])
{
$_SESSION['Catalogue'][$x]['InBasket'] = 1;
}
}
}

The InBasket column never gets updated and yet I am not getting an error on the code!!

Any ideas where I'm going wrong
Apr 30 '12 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,035
Add some code to your script to make debugging easier,
i.e.:

Expand|Select|Wrap|Line Numbers
  1. for ($i = 0; $i < count($_SESSION['CartItems']); $i++):
  2. {
  3.   for ($x = 0; $x < count($_SESSION['Catalogue']); $x++):
  4.   {
  5.     If ($_SESSION['CartItems'][$i]['ProductID'] == $_SESSION['Catalogue'][$x]['ProductID'])
  6.     {
  7.       $_SESSION['Catalogue'][$x]['InBasket'] = 1;
  8.     } else {
  9.       echo "$_SESSION['CartItems'][$i]['ProductID'] and $_SESSION['Catalogue'][$x]['ProductID'] do not match";
  10.     }
  11.   }
  12. }
  13.  
  14.  
Apr 30 '12 #2

P: 50
Hi Guys

I've sorted the problem, I had a "=" in my code should have been "=="

Thanks
Apr 30 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.