display data from database
I want to display data from a table in my database based on other table. Like the example below..
eg:
Foods Type1
-----
| 1 | food1 |
-----
| 2 | food2 |
-----
| 3 | food3 |
-----
Foods Type2
-----
| 1 | food4 |
-----
| 2 | food5 |
-----
But instead i get a display like this:
Foods Type1
-----
| 1 | food1 |
-----
| 2 | food2 |
-----
| 3 | food3 |
-----
Foods Type2
-----
| 1 | food1 |
-----
| 2 | food2 |
-----
| 3 | food3 |
-----
| 1 | food4 |
-----
| 2 | food5 |
-----
Any idea how to solve this?
[678 byte] By [
yatt] at [2007-11-20 7:02:21]

# 1 Re: display data from database
Seeing your code would help us. Diagrams don't really help when trying to understand how someone is attempting to program. Please remember to use [code] tags. You would use them in the following manner.
[code]
your code here
[/code]
# 2 Re: display data from database
I use the code below
$query1 = mssql_query('some query');
$tab_header .=<<<EOD
<table width="100%" border="0" cellpadding="1" cellspacing="1">
<tr>
<td>Num.</td>
<td>Food Name</td>
</tr>
EOD;
$row_count=1;
while($row = mssql_fetch_array($query))
{
$foodtype = $row['foodType'];
$foodname = $row['foodName'];
if($ftype == '')
{
$ftype = $foodtype;
}
if($foodtype == $ftype)
{
$tab_details .=<<<EOD
<tr>
<td>$row_count</td>
<td>$foodname</td>
EOD;
$row_count++;
}
else
{
$tab_footer ="</table>\n";
$tab =<<<TAB
$tab_header
$tab_details
$tab_footer
TAB;
print $tab;//print the table
$ftype = $foodtype;
$row_count = 1;//start counting from 1
$tab_details .=<<<EOD
<tr>
<td>$row_count</td>
<td>$foodname</td>
EOD;
$row_count++;
}
}//end while
yatt at 2007-11-10 3:57:29 >

# 3 Re: display data from database
Are you getting duplicates, is that the problem? That would be an sql/database problem which you could solve by simply adding the distinct keyword to your statement, for example:
select distinct foods, Type1,Type2 from foods;
# 4 Re: display data from database
I would first read through the database using distinct (as mentioned by Nibinaear :thumb:). While doing this, create an array. Now loop through that array with the food types making a SQL query for each.
foreach($array_items as $item){
$query = mysql_query("SELECT * FROM `table` WHERE `foodType` = '" . $item . "'");
}