Category 1
Category 2
Category 3
*/
// set query (you'll have to write this yourself, this is just an example)
$query = "SELECT DISTINCT post_title, post_category FROM post ORDER BY post_category ASC, post_title ASC";
// run query
$result = @mysql_query($query);
// if rows were returned from the query
if (($result) && (mysql_num_rows($result) > 0))
{
// variables explained:
// $current_stored_category -- this will hold the name of the category of the previous post, this will be compared to the category of the current post in order to determine if the category has changed.
//$is_very_first_list -- this will be used to determine if the post in question is the very, very first post in the whole data set, if it isn't then we'll use that knowledge to print tags that will close off the list that was printed before it.
// set our variables to their initial values
$current_stored_category = '';
$is_very_first_list = 1;
// loop and fetch all the results
while ($row = mysql_fetch_assoc($result))
{
$post_category = $row['post_category'];
$post_title = $row['post_title'];
// if this post has a different category to the one stored in $current_stored_category
if ($current_stored_category <> $post_category)
{
// set the stored category to equal this post's category
$current_stored_category = $post_category;
// because we're in a new category we need to close off the last category's . However, we must check that we're not at the very, very beginning of the whole thing, because if we are there won't be a previous list to close off
if ($is_very_first_list == 0)
{
print ''."\n";
}
// we must also print the new category as an heading and start our new
print ''.$post_category.'
'."\n\n";
print '';
}
// now we print out the post item
print '- '.$post_title.'
'."\n";
// now we alter this variable value so we know we're not at the very, very start of the whole thing anymore
$is_very_first_list = 0;
}
// now we print a closing to finish off the last record
print '
'."\n\n";
}
// if no rows were returned from the query
else
{
// error message goes here
}
?>