Jump to content


Photo

Making a nice pricing list


  • Please log in to reply
4 replies to this topic

#1 t.bo

t.bo
  • Members
  • PipPipPip
  • Advanced Member
  • 57 posts

Posted 15 July 2006 - 12:26 PM

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 http://www.euroclini...tent=prijslijst.

The current output code (but far from finished) is
<?
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>";
}
?>

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.

#2 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 15 July 2006 - 01:09 PM

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.

<?
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>';
?>


#3 t.bo

t.bo
  • Members
  • PipPipPip
  • Advanced Member
  • 57 posts

Posted 15 July 2006 - 01:42 PM

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...

#4 Barand

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

Posted 15 July 2006 - 02:03 PM

try
<?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;
}
?>

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

#5 t.bo

t.bo
  • Members
  • PipPipPip
  • Advanced Member
  • 57 posts

Posted 15 July 2006 - 02:22 PM

Thanks a lot man. Only had to add order by catfield.

Works fine :-)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users