PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
articles
------------------
id name category
1 article1 1
2 article2 2
3 article3 2
4 article4 3
5 article5 3
6 article6 4
category
------------------
id name
1 category1
2 category2
3 category3
4 category4
5 category5
6 category6
I want to print these to the page like:
category1
article1
category2
article2
article3
category3
article4
article5
category4
article6
The problem I'm having is not ouputting the table. I can do that. It's getting the label "category x" to appear once before the grouping. Does anybody know how to do this? Is the answer in the sql? or is it in a some sort of code around in PHP?
Thanks
Last edited by neophyte on Tue Nov 02, 2004 4:06 pm, edited 1 time in total.
<?
$sql = "SELECT `categories` ORDER BY `id`";
$result = mysql_query($sql) or die(mysql_error());
while ($row=mysql_fetch_array($result))
{
$sql = "SELECT `articles` WHERE `id` = '".$row['id']."' ORDER BY `id`";
//output articles
}
?>
Not sure if this is what your talking about, or looking for an all sql method of this. If thats what you want, then I'm afraid I can't help you.
yes, but not an explicit JOIN, a simple one such as "SELECT table1.field1, table2.field1 ... WHERE table1.field_id1=table2.field_id2 GROUP BY table1.id3"
Well Phenom's way solved the immeadiate problem for me!
But I would like to try out this Group by thing patrikG suggested. I've tried Group By in the past but I always ended up with just one set (one category selection). I'll have to try it again and see if I get different results.