Click to See Complete Forum and Search --> : PHP SQL help


ForeverIrise
12-30-2006, 01:18 AM
Well here is the scenario, (please not that I am very new to PHP and any other language out there... but I try to learn)

I have a Database (dbCoffee)

I have 2 tables in the database (tblBev, tblTypes)

Their structures are:

tblTypes
-typeID
-typeName

1 Freshbrewed
2 Expresso
3 Latte
4 Smoothies


tblBev
-bevID
-bevType
-bevName
-bevSMprice
-bevMEDprice
-bevLRGprice

1 1 'Breakfast Blend' '1.55' '1.75' '1.85'
2 1 'Ice Coffee' '1.75' '2.00' '2.25'
3 2 'Cappuccino' '2.65' '3.20' '3.50'
4 3 'Hot Chocolate' '1.50' '1.75' '2.00'
5 4 'Fruit Smoothie' '3.50' '4.25' '5.25'

tblBev.bevType uses value in tblTypes.typeID to identify the type of beverage it is identified in tblTypes.typeName.

What I'd like to do is populate an XML file by reading the data from the tables and put it in this format:

-Freshbrewed
Breakfast Blend 1.55 1.75 1.85
Ice Coffee 1.75 2.00 2.25

-Expresso
Cappuccino 2.65 3.20 3.50

-Latte
Hot Chocolate 1.50 1.75 2.00
.
.

etc.

So far I have this working, but It just displays EVERYTHING and doesn't give the type of beverage that it is.


<?php
header("Content-Type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
echo "<?xml-stylesheet type=\"text/xsl\" href=\"layout.xsl\" ?>\n";
require("scripts/db.php");
$connection = mysql_connect($host, $user, $password) or die ("Error with the connection to server. ");
$db = mysql_select_db($database, $connection) or die ("Could not connect to Database. ");
$query = "SELECT * FROM `tblBev` WHERE 1";
$result = mysql_query($query) or die ("Query could not be executed. ");
echo "<items>\n";
while ($row = mysql_fetch_assoc($result))
{
echo "<beverage>\n";
echo "<name>" . $row['bevName'] . "</name>\n";
echo "<smprice>" . $row['bevSMprice'] . "</smprice>\n";
echo "<medprice>" . $row['bevMEDprice'] . "</medprice>\n";
echo "<lrgprice>" . $row['bevLRGprice'] . "</lrgprice>\n";
echo "</beverage>\n\n";
}
echo "</items>";
mysql_close();
die();
?>


My XSL file looks a bit like this:


<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<body>
<table>
<tr>
<td></td><td>12 oz.</td><td>16 oz.</td><td>20 oz.</td>
</tr>
<xsl:for-each select="items/beverage">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="smprice"/></td>
<td><xsl:value-of select="medprice"/></td>
<td><xsl:value-of select="lrgprice"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>


This pretty much just gets it looking like this:
12 oz. 16 oz. 20 oz.
Breakfast Blend 1.55 1.75 1.85
Ice Coffee 1.75 2.00 2.25
Cappuccino 2.65 3.20 3.50
Hot Chocolate 2.25 2.50 2.75
Fruit Smoothie 3.50 4.25 5.25

What do I have to change to get it to display like I want it too? With the "Type" of beverage listed and each separated into its "Type".

I really appreciate you helping me with this, I understand that my thought process might be a bit elementary when it comes to this stuff but I am learning and I am trying. Thank you for your help.

-Cesar

NightShift58
12-30-2006, 02:33 AM
What if you changed:$query = "SELECT * FROM `tblBev` WHERE 1"; to:$query = "SELECT * FROM `tblBev` a, `tblTypes` b WHERE a.bevType = b.typeID";This would give you a recordset that would contain all fields from both tables, matched on bevType/typeID.

You could then modify your WHILE loop to add the beverage type: echo "<type>" . $row['typeName'] . "</type>\n";Finally, modify your XSL to reflect the new data.