Omiddy Posted September 7, 2010 Share Posted September 7, 2010 Hi, I'm pretty sure that I am complicating thing for something I'm trying to do. In my database, I have some posts, each with a category field. what I'm trying to do is have a list of categories with each post that falls in that category listed underneath each category. ie: <h3>Category 1</h3> <ul class="nav1"> <li><a href="#">Title of post with category 1</a></li> <li><a href="#">Title of another post with category 1</a></li> <li><a href="#">Title of 3rd post with category 1</a></li> </ul> <h3>Category 2</h3> <ul class="nav1"> <li><a href="#">Title of post with category 2</a></li> <li><a href="#">Title of another post with category 2</a></li> <li><a href="#">Title of 3rd post with category 2</a></li> </ul> Can someone please tell me what the easiest way to do this is? I've tried different tutorials and methods i could think of, but they overcomplicated things. any help would be appreciated, thank you : ) Quote Link to comment https://forums.phpfreaks.com/topic/212729-categorizing/ Share on other sites More sharing options...
petroz Posted September 7, 2010 Share Posted September 7, 2010 I had todo this recently. Try something like this.. <?php $categories = mysql_fetch_array(mysql_query("SELECT DISTINCT `category` FROM (`blog_posts`)")); foreach ($categories as $category){ echo '<li><a href="http://example.com/blog/category/'.$category['category'].'">'.$category['category'].'</a></li>'; } Quote Link to comment https://forums.phpfreaks.com/topic/212729-categorizing/#findComment-1108137 Share on other sites More sharing options...
Rifts Posted September 7, 2010 Share Posted September 7, 2010 I don't think he wants to use SELECT DISTINCT though because that will only produce ONE of each category and he wants them all. Quote Link to comment https://forums.phpfreaks.com/topic/212729-categorizing/#findComment-1108170 Share on other sites More sharing options...
petroz Posted September 7, 2010 Share Posted September 7, 2010 Either way, he will see all the categories... if he doesnt use distinct, he will see duplicates if he has em... Quote Link to comment https://forums.phpfreaks.com/topic/212729-categorizing/#findComment-1108306 Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2010 Share Posted September 7, 2010 To do what the OP asked, would require that you retrieve the rows you want in the order that you want them. Then iterate over the result set and output it the way you want - <?php $query = "SELECT * FROM your_table WHERE your_where_condition_here ORDER BY category, title"; // order the titles under each category if($result = mysql_query($query)){ if(mysql_num_rows($result)){ // one or more rows in the result set $last_category = null; // remember the last category, initialize to a value that will never exist in the data while($row = mysql_fetch_assoc($result)){ if($last_category != $row['category']){ if($last_category != null){ // close the previous <ul> echo "</ul>\n"; } // the category changed, output the header echo "<h3>{$row['category']}</h3>\n<ul class=\"nav1\">\n"; $last_category = $row['category']; // remember the new category } // output the data from the row echo "<li><a href=\"#\">{$row['title']}</a></li>\n"; } echo "</ul>\n"; // close the last <ul> } else { // query worked, but matched zero rows echo "Your query did not match any existing data"; } } else { // query failed due to an error echo "Query failed: $query<br />" . mysql_error(); // for debugging purposes only... } ?> Quote Link to comment https://forums.phpfreaks.com/topic/212729-categorizing/#findComment-1108314 Share on other sites More sharing options...
Omiddy Posted September 8, 2010 Author Share Posted September 8, 2010 PFMaBiSmAd you are a genious. that is exactly what i needed. thanks everyone ! Quote Link to comment https://forums.phpfreaks.com/topic/212729-categorizing/#findComment-1108848 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.