Php

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.

$_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

Well personally, I wouldnt do it in a nested loop.

[FPHP]FOREACH[/FPHP] the catalog, pulling the ProductID’s into a temporary array.
Now FOREACH the Cartitems, and do a [FPHP]in_array[/FPHP] check.
Should be slightly lighter on your page. (rather than having to reference an object x^y times)

as for why things are breaking down… guessing it’s that you’re using colons and braces… I’d suggest dropping them and sticking with just the braces.

Thanks for that, however, how do I get the column ‘InBasket’ to contain true in the correct row.

do [FPHP]array_search[/FPHP] instead and use the return value (if not FALSE)


$key = array_search($current['ProductID'],$prodids);
if($key !== FALSE) { //Note: !== is required here; != will reject index 0!
  $_SESSION['Catalogue'][$key]['InBasket'] = 1;
}

Hi Guys

I’ve sorted the problem, I had a “=” in my code should have been “==”

Thanks