Category 1
- SubCat 1a
- SubCat 1b
- SubCat 1c
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, post_subcategory FROM post ORDER BY post_category ASC, post_subcategory 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.
// $current_stored_subcategory -- this will hold the name of the sub-category of the previous post, this will be compared to the sub-category of the current post in order to determine if the sub-category has changed.
// $is_very_first_sublist -- this will be used to determine if the post in question is the very, very first post in the category 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;
$current_stored_subcategory = '';
$is_very_first_sublist = 1;
// loop and fetch all the results
while ($row = mysql_fetch_assoc($result))
{
$post_category = $row['post_category'];
$post_subcategory = $row['post_subcategory'];
$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;
// reset the subcategory vars
$current_stored_subcategory = '';
$is_very_first_sublist = 1;
// 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";
print ''."\n";
print ''."\n";
}
// we must also print the new category as an heading and start our new
print ''.$post_category.'
'."\n\n";
print ''."\n";
}
// if this post does have the same category as the one stored in $current_stored_category
else
{
// alter this value so we know we're not at the start of a sublist
$is_very_first_sublist = 0;
}
// if this post has a different subcategory to the one stored in $current_stored_subcategory
if ($current_stored_subcategory <> $post_subcategory)
{
// set the stored subcategory to equal this post's subcategory
$current_stored_subcategory = $post_subcategory;
// because we're in a new subcategory we need to close off the last subcategory's - . However, we must check that we're not at the very, very beginning of the whole category, because if we are, there won't be a previous list to close off
if ($is_very_first_sublist == 0)
{
print '
'."\n";
print ''."\n";
}
// now we print out the subcategory item and start off its new list
print '- '.$post_subcategory'."\n";
print '
'."\n";
}
// 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 start of the whole thing anymore
$is_very_first_list = 0;
$is_very_first_sublist = 1;
}
// now we print a closing to finish off the last record
print '
'."\n";
print '
'."\n";
print '
'."\n\n";
}
// if no rows were returned from the query
else
{
// error message goes here
}
?>