Re: Displaying groups from SELECT
| From: | Gürhan Özen | Date: | Sat, 07 Jun 2003 03:07:13 +0000 |
| Subject: | Re: Displaying groups from SELECT | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-28390@lists.php.net to get a copy of this message | ||
On Fri, 2003-06-06 at 21:49, Becoming Digital wrote:
> I'm wearing the stupid hat today, so please pardon this. I know I must be
> overlooking something.
>
> I have a small catalogue with two tables (categories, products) from which I'm
> trying to display items. I'm trying to print the contents as below without
> using two queries, but I'm having a difficult time with it.
>
> cat1
> prod1
> prod2
> cat2
> prod1
> prod2
> etc.
>
> I think this came up fairly recently, but I cannot for the life of me figure out
> what search terms would answer this question. As you can see from the message
> subject, I don't even know how to refer to my problem. Thanks a lot for all
> your help.
>
> Edward Dudlik
> Becoming Digital
> www.becomingdigital.com
>
Hi Ed,
The magic word is "DISTINCT" :)
$query="SELECT DISTINCT(category) AS cat_name FROM table_name";
$result=mysql_query($query);
print "<ul>";
while ($row=mysql_fetch_array($result)) {
print "<li>".($row["cat_name"])."";
$query1="SELECT productname FROM tablename WHERE
category=".($row["cat_name"])."";
$result1=mysql_query($query1);
while ($row1=mysql_fetch_array($result1)) {
print "<li>".($row1["productname"])."";
}
print "</ul>";
}
print "</ul>";
I hope this helps..