954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to fetch multiple records based on category

hello guys,
please give me any idea about solving this problem.
I have two tables

categories:
id .... category .... parent
________________________________
1 .... general .... 0
2 .... news .... 0

news:
id .... headline .... category_id
________________________________
1 .... headline1 .... 1
2 .... headline2 .... 2
3 .... headline3 .... 2
4 .... headline4 .... 1

and I have this MYSQL code
SELECT * FROM news INNER JOIN categories ON news.category_id=categories.id group by category_id,date

now I want to fetch rows based on category
so I get result like this:

general category:
headline1
headline4

news category:
headline2
headline3

please help me with this guys :)

wetube
Newbie Poster
5 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
SELECT * FROM news INNER JOIN categories ON news.category_id=categories.id ORDER BY category_id,date
//from the loop onwards...
$output ="";
$cat = "";
while($d = mysql_fetch_array($result)){
  if($d['category'] != $cat){
    if($output !="")$output .= "\n</ul>";
    $output . ="\n<h3>${d['category']}</h3>\n<ul>";    
  }
  $output .= "\n\t<li>{$d['headline']}<li>";
  $cat = $d['category'];
}
if($output != "")$output .= "\n</ul>";
...
echo $output;


Not tested - off top of head.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: