josephman1988 Posted November 10, 2008 Share Posted November 10, 2008 Hey there! Ok, I have a news table, and using the join function I put them in categories. I want to know how I would go about a loop where if the category has at least one news item associated to it print the category name and all associated news items under it, if not, dont print the category. QUERY $newsarchive = mysql_query(" SELECT news.n_id, news.n_title, news.n_date, games.g_title, news.g_id FROM games LEFT JOIN news ON games.g_id = news.g_id ORDER BY news.g_id "); If you need more detail please just ask, and thanks in advanced! Joe. Link to comment https://forums.phpfreaks.com/topic/132169-looping-to-print-all-data-if-any-data-exists/ Share on other sites More sharing options...
Mchl Posted November 10, 2008 Share Posted November 10, 2008 How does the data returned by query look like? There's no column called category, so it's a bit hard to tell anything. Link to comment https://forums.phpfreaks.com/topic/132169-looping-to-print-all-data-if-any-data-exists/#findComment-686945 Share on other sites More sharing options...
premiso Posted November 10, 2008 Share Posted November 10, 2008 www.php.net/mysql_fetch_assoc() www.php.net/foreach <?php $newsarchive = mysql_query(" SELECT news.n_id, news.n_title, news.n_date, games.g_title, news.g_id FROM games LEFT JOIN news ON games.g_id = news.g_id ORDER BY news.g_id "); while ($newsArray = mysql_fetch_assoc($newsarchive)) { foreach ($newsArray as $key => $val) { echo "Key: " . $key . " Val: " . $val . "<br />"; } } ?> I think that is what you are looking for. Link to comment https://forums.phpfreaks.com/topic/132169-looping-to-print-all-data-if-any-data-exists/#findComment-686947 Share on other sites More sharing options...
josephman1988 Posted November 10, 2008 Author Share Posted November 10, 2008 Thanks But still because its looping over its still printing the category header for each entry rather then assigning all entries to one category. Ho would i do this, thanks. Link to comment https://forums.phpfreaks.com/topic/132169-looping-to-print-all-data-if-any-data-exists/#findComment-687190 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.