cordoprod Posted February 10, 2009 Share Posted February 10, 2009 Hello! I have a user management system, and i am struggling with the friends section. I have a DB called friends. The structure is like this: category, friend, friend2, friend_text, friend2_text What i want to do, but can't figure out, is to list all the entries, but by grouping it into category. Like this: category1 -- friend friend friend category2 -- friend friend friend If you understand? I just want to know how the SQL is going to look like. Quote Link to comment https://forums.phpfreaks.com/topic/144647-solved-phpmysql-grouping-into-category/ Share on other sites More sharing options...
premiso Posted February 10, 2009 Share Posted February 10, 2009 SELECT category, friend, friend2, friend_text, friend2_text FROM friends GROUP BY category That should do it. Instead of a group by, however, you could just use ORDER BY category, and that should work. Quote Link to comment https://forums.phpfreaks.com/topic/144647-solved-phpmysql-grouping-into-category/#findComment-759013 Share on other sites More sharing options...
cordoprod Posted February 10, 2009 Author Share Posted February 10, 2009 Yes i have tried that, but should i output it with while(mysql_fetch and so on.... as usual then? What do i need to put inside the while()? Quote Link to comment https://forums.phpfreaks.com/topic/144647-solved-phpmysql-grouping-into-category/#findComment-759020 Share on other sites More sharing options...
premiso Posted February 10, 2009 Share Posted February 10, 2009 <?php while ($row = mysql_fetch_assoc($result)) { if ($last_cat != $row['category']) { echo "<br />" . $row['category'] . "<br />--<br />"; $last_cat = $row['category']; } echo $row['friend'] . "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/144647-solved-phpmysql-grouping-into-category/#findComment-759025 Share on other sites More sharing options...
cordoprod Posted February 10, 2009 Author Share Posted February 10, 2009 Thanks, i'll give it a try. Quote Link to comment https://forums.phpfreaks.com/topic/144647-solved-phpmysql-grouping-into-category/#findComment-759033 Share on other sites More sharing options...
cordoprod Posted February 10, 2009 Author Share Posted February 10, 2009 THANKS!!!!!!!!!!!! I have been struggling with this for a loooooooooong time! Quote Link to comment https://forums.phpfreaks.com/topic/144647-solved-phpmysql-grouping-into-category/#findComment-759055 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.