Man! you are GR8!!!!!!!!!!!!!!!!!! I still can't believe my eyes! Could this code be so simple and short????? It works perfectly!!!! Below is the code i am using now:
Cheers for your help!!!
Code: Select all
$q = "SELECT * FROM items WHERE RestaurantID = '$restaurantid' ORDER BY Category, ItemName";
$r = mysql_query($q,$conn);
$lastCategory="";
while ($row = mysql_fetch_array($r)) {
if($rowї"Category"]!=$lastCategory) {
$lastCategory=$rowї"Category"];
echo("<font color='red'><b>".$rowї"Category"] . "</b></font><BR>");
}
echo $rowї'ItemID']."<br>";
echo $rowї'ItemName']."<br>";
echo $rowї'Description']."<br>";
echo "$".$rowї'Price']."<br><br>";
}
It works perfectly, but i have a doubt: Please consider the following secnario...
The items in the database are listed as follows:
ItemID-----ItemName------Description------Price------Category
1----------chk.pizza--------blabla------------10---------Pizza
2----------mut.burger------blabla------------10---------Burger
3----------beef pizza-------blabla------------12---------Pizza
4----------Sandwich--------blabla------------10---------Sandwich
5----------mut.pizza--------blabla------------15---------Pizza
6----------beef.burger-----blabla--------------8---------Burger
now what the code you have written does is...
Matches the $record['category'] with the variable $lastcategory, which is initially blank
As however the first category encountered is not null but "Pizza", the next step stores the Category value (Pizza) in the variable $lastcategory and then prints out the Category (Pizza). then movin' to the next step it print out all the other fields of the same row like ItemName, Description etc. then moves to the next row. Here it encounters the Category value, which is now "Burger" and as it doesnt match with the current value of the variable $lastcategory, which is "Pizza", the next step stores the value "Burger" in the variable $lastcategory and then prints out "Burger" and then all the other fields.
Now the value of the $lastcategory is "Burger". so why isnt it printing out "Pizza" again when it reads the third row as Category value "Pizza" is different from the current value of the variable $lastcategory?????????
Please dont mind if this is something simple that i am not able to figure out! I really didnt understand the code completely!
Would be really grateful if you can please clear my confusion!
Thanks a lot...Thanks once again!
Mairaj.