Jump to content

Making a nice pricing list


t.bo

Recommended Posts

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
Share on other sites

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  |  Category
0  |  x  |  5.00  |  cat
1  |  p  |  4.00  |  cat

Basically 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
Share on other sites

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
Share on other sites

try
[code]<?php
include('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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.