Jump to content

Looping to print all data IF any data exists


josephman1988

Recommended Posts

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.