// current session id
$sid = session_id();
// get current cart session
$sql = "SELECT * FROM tbl_cart WHERE ct_session_id = '$sid'";
$result = dbQuery($sql);
// get all the items in the car category
$query = "SELECT pd_id FROM tbl_product WHERE cat_id ='28'";
$r = dbQuery($query);
//Cycle through the cart and compare each cart item to the cars to determine
if the cart contains a car in it.
while($cart = dbFetchAssoc($result)){
while($product = dbFetchAssoc($r)){
echo $cart['pd_id'] . " - ";
echo $product['pd_id']. "<br>";
}
}
dbFetchAssoc() is a custom database layer that is basically (mysql_fetch_assoc).
Im trying to take the rows from the query and use that information to compare. The code above with the echo statements is just echoing for debug purposes. Is there a particular reason that the while loop exits after the nested loop?
Yes. You need to run the query again, since each time you call
dbFetchAssoc($r)you’re advancing that cursor.Here is an optimised version which doesn’t hit the database as much. It’s specific to this particular question, though, and would be an equally bad choice if the queryset was particularly large – it’s run into memory problems instead of speed problems.
I haven’t tested this second code, but the idea should be clear enough.