Jump to content


Photo

MYSQL Category re-vised to make sense


  • Please log in to reply
2 replies to this topic

#1 nazca

nazca
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 15 June 2006 - 08:28 PM

Hope this is better explanation than before.... big ooops [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

Here is the following code I am using now.
This table displays the following heading first:

Name Description Price

Under the heading the query displays all the food items ordered by sectionOrder for a particular restaurant. This is fine, but I want to be able to print category names with its corresponding food items under it such as appetizers, salads, main course, etc... So the food menu would say :

Appetizers
Quesadilla great mexican dish $3.99
Wings 10 drumsticks $6.99
Salads
Chef fresh greens $6.99
Caesar romaine greens $7.99
Main Course
etc, etc, etc

The code below just displays all the food items from a particular restaurant ordered by section:
Quesadilla " "
Wings " "
Chef Salad
Casear Salad
etc.


You can imagine if the menu is huge how annoying it would be to find a salad, or main course....

<table width="750" border="2" cellspacing="0" cellpadding="0" bordercolor="#FFFFFF">
<tr>
<td>
<?php
include "admin/db_conn_open.php";


$query = mysql_query("SELECT pName, pDescription, pPrice, pSection, sectionID, sectionOrder, sectionName FROM products, sections WHERE pSection=sectionID ORDER BY sectionOrder");

print ("<tr>");
print ("<td width='200' >Name</td>");
print ("<td width='490' >Description</td>");
print ("<td width='60' >Price</td>");
print ("</tr>");

while ($row = @mysql_fetch_array($query))
{
$variable1=$row["pName"];
$variable2=$row["pDescription"];
$variable3=$row["pPrice"];

//table layout for results

print ("<tr>");
print ("<td width='200' >$variable1</td>");
print ("<td width='490' >$variable2</td>");
print ("<td width='60' >$$variable3</td>");
print ("</tr>");
}
?>
</td>
</tr>
</table>




#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 15 June 2006 - 10:29 PM

Try

$query = mysql_query("SELECT pr.pName, pr.pDescription, 
    pr.pPrice, sc.sectionName 
    FROM products pr INNER JOIN sections sc
          ON pr.pSection = sc.sectionID 
    ORDER BY sc.sectionOrder");
    
print('<table width="750" border="2" cellspacing="0" cellpadding="0" bordercolor="#FFFFFF">');

print ("<tr>");
print ("<td width='200' >Name</td>");
print ("<td width='490' >Description</td>");
print ("<td width='60' >Price</td>");
print ("</tr>");

$last_section = '';
while (list($name, $desc, $price, $section) = @mysql_fetch_row($query))
{
    if ($last_section != $section) {
        print "<tr><td colspan='3'><H3>$section</H3></td></tr>";
    }
    print ("<tr>");
    print ("<td>$name</td>");
    print ("<td>$desc</td>");
    print ("<td>$price</td>");
    print ("</tr>");
    $last_section = $section;
}
print '</table>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 nazca

nazca
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 15 June 2006 - 11:01 PM

To Barand:

I tried the code and it works.... I have asked my C+ programmer friend and no luck
You save me a lot of time.......... Super Thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users