473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Nested PHP loops

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
2 1498
Luuk
1,047 Expert 1GB
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
Hi Guys

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

Thanks
Apr 30 '12 #3

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

Similar topics

3
by: Oleg Leschov | last post by:
Could there be means of exiting nested loops in python? something similar to labelled loops in perl.. I consider it irrating to have to make a flag for sole purpose of checking it after loop if...
0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
17
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html Why is C# 500% slower than C++ on Nested Loops ??? Will this problem be solved in...
10
by: Roshawn | last post by:
Hi, I am experimenting with nested For...Next loops. My code looks like this: Dim i as Byte Dim itm as Byte For i = 0 to 9 For itm = 0 to 9 'code omitted
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
3
by: tzuriel | last post by:
Hello all, I think nested loops will do what I want, but I can't seem to get them right. I have two tables, members and casts. I run a query as follows: $sql_query="SELECT DISTINCT...
3
by: Luna Moon | last post by:
My friend has three nested loops, each has 10000, 1000, 100 iterations, respectively. What should be the most efficient way of layout out the three nested loops? for (i=0; i<10000; i++) for...
4
by: toddlahman | last post by:
I am using two while loops that are nested. The first loop (post name) returns the full column of results, but the second (post modified) only returns the first row of the column. Is there another...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.