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, 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 '."\n"; print ''."\n"; print ''."\n"; } // we must also print the new category as an

heading and start our new