PHP and MySQL task

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mairaj
Forum Newbie
Posts: 11
Joined: Mon Sep 20, 2004 2:40 am

PHP and MySQL task

Post by mairaj »

Hello everybody, I am pretty new to PHP and need some help. My problem follows...

I have a MySQL table "Items" which is structured as follows...

ItemID-----ItemName------Description------Price------Category
1----------chk.pizza--------blabla------------10---------Pizza
2----------mut.pizza--------blabla------------15---------Pizza
3----------beef pizza-------blabla------------12---------Pizza
4----------chk.burger------blabla------------10---------Burger
5----------mut.burger------blabla------------10---------Burger
6----------beef.burger-----blabla--------------8---------Burger


now i want to display the items as follows...

---------------------HTML PAGE---------------------------------------

Pizzas:

chk.pizza $10
mut.pizza $15
beef pizza $12


Burgers:

chk.burger $10
mut.burger $10
beef burger $8

-------------------------------------------------------------------------

The thing is the categories aren't pre determined and i have to make the blocks according to the categories available in the database and display the related items under each category. I have been trying to work this out since last 4 days but to no avail :cry: I really need help and would really appreaciate any!

Hope i get replies...

Cheers.
Mairaj.
ldomingues
Forum Commoner
Posts: 41
Joined: Fri Aug 06, 2004 1:15 pm
Location: Portugal

Post by ldomingues »

You can do something like this:

Code: Select all

<?php

$lastCategory="";

//retrieve data from you db:
//select * from Items order by category, ItemName;

//while....

if($record["category"]!=$lastCategory)
{
  $lastCategory=$record["category"];
  echo($record["category"] . "<BR>");
}

//diplay item

//end while

?>
(maybe that table needs some normalization... How about a item_category table?)
mairaj
Forum Newbie
Posts: 11
Joined: Mon Sep 20, 2004 2:40 am

Post by mairaj »

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)) &#123;

	if($row&#1111;"Category"]!=$lastCategory) &#123;
	$lastCategory=$row&#1111;"Category"];
	echo("<font color='red'><b>".$row&#1111;"Category"] . "</b></font><BR>");
	&#125;

	echo $row&#1111;'ItemID']."<br>";
	echo $row&#1111;'ItemName']."<br>";
	echo $row&#1111;'Description']."<br>";
	echo "$".$row&#1111;'Price']."<br><br>";
&#125;
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.
ldomingues
Forum Commoner
Posts: 41
Joined: Fri Aug 06, 2004 1:15 pm
Location: Portugal

Post by ldomingues »

If you're sorting by category, data will never appear like this:

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


But like this:
ItemID-----ItemName------Description------Price------Category
6----------beef.burger-----blabla--------------8---------Burger
2----------mut.burger------blabla------------10---------Burger
1----------chk.pizza--------blabla------------10---------Pizza
3----------beef pizza-------blabla------------12---------Pizza
5----------mut.pizza--------blabla------------15---------Pizza
4----------Sandwich--------blabla------------10---------Sandwich

So that will not happen.

Does that answers your question?
mairaj
Forum Newbie
Posts: 11
Joined: Mon Sep 20, 2004 2:40 am

Post by mairaj »

Oh ok! Absolutely, its all clear now!!!

Thanks sooooo much for your time and help.

Have a nice day!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

If you do not want to organize them by category

you can do

Code: Select all

<?php

$usedcats= array();

//retrieve data from you db:

//select * from Items order by category, ItemName;

//while....

if(!in_array($record["category"],$usedcats))
{
  $usedcats[] = $record["category"];
  echo $record["category"] . "<BR>";
}

//diplay item

//end while
?>
Post Reply