t.bo Posted July 15, 2006 Share Posted July 15, 2006 Hello all,Im making a dynamic price list. Each product has its own title, price and category. Loading these in a database is no problem but I have a problem in making a correct display. The display should be as follows :The first category is displayed as a title and below you can see the products with their price who belong to that category. Below the next category is displayed as a title with all the products of that category and so forth.An example of the static price list (and the way it should be displayed) can be viewed at [url=http://www.euroclinix.com/index2.php?content=prijslijst]http://www.euroclinix.com/index2.php?content=prijslijst[/url].The current output code (but far from finished) is [code]<?include('dbconnect.php');$sql = mysql_query("select * from pricelist");while($row = mysql_fetch_array($sql)){$title = $row["titlefield"];$id = $row["idfield"];$price = $row["pricefield"];$categorie = $row["catfield"]:echo "<h2>$categorie</h2>";}?>[/code]Now every categorie is just displayed multiple times (of course). So each category should be displayed once as a title with the corresponding products below it.Hope somebody can help me out.Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/14667-making-a-nice-pricing-list/ Share on other sites More sharing options...
xyn Posted July 15, 2006 Share Posted July 15, 2006 Hii I would use this way, it's just as effective, and i think it's much easier.Say your database is this:ID | Title | Price | Category0 | x | 5.00 | cat1 | p | 4.00 | catBasically change the array into a row. then you echo the table example.[code=php:0]<?echo '<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%"> <tr> <td width="25%" align="center">ID</td> <td width="25%" align="center">TITLE </td> <td width="25%" align="center">PRICE</td> <td width="25%" align="center">CATEGORY</td> </tr>';include('dbconnect.php');$sql = mysql_query("select * from pricelist");while($row = mysql_fetch_row($sql)){echo '<tr> <td width="25%" align="center">'.$row[0].'</td> <td width="25%" align="center">'.$row[1].'</td> <td width="25%" align="center">'.$row[2].'</td> <td width="25%" align="center">'.$row[3].'</td> </tr>';}echo '</table>';?>[/code] Link to comment https://forums.phpfreaks.com/topic/14667-making-a-nice-pricing-list/#findComment-58479 Share on other sites More sharing options...
t.bo Posted July 15, 2006 Author Share Posted July 15, 2006 But then category is displayed after every product and I really should have a category title....Someone told me to make a table with all the categories, and a table with the products that contains a field "categoryID". And then i should make the 2 tables correspond and create the output that I wanted. But I don't know how to do that... Link to comment https://forums.phpfreaks.com/topic/14667-making-a-nice-pricing-list/#findComment-58481 Share on other sites More sharing options...
Barand Posted July 15, 2006 Share Posted July 15, 2006 try[code]<?phpinclude('dbconnect.php');$sql = mysql_query("select * from pricelist");$prevCat='';while($row = mysql_fetch_array($sql)){ $title = $row["titlefield"]; $id = $row["idfield"]; $price = $row["pricefield"]; $categorie = $row["catfield"]: // has category changed ? // if so, print it if ($categorie != $prevCat) { echo "<h2>$categorie</h2>"; } echo $title, ' ', $price, '<br/>'; $prevCat = $categorie;}?>[/code] Link to comment https://forums.phpfreaks.com/topic/14667-making-a-nice-pricing-list/#findComment-58494 Share on other sites More sharing options...
t.bo Posted July 15, 2006 Author Share Posted July 15, 2006 Thanks a lot man. Only had to add order by catfield.Works fine :-) Link to comment https://forums.phpfreaks.com/topic/14667-making-a-nice-pricing-list/#findComment-58498 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.