Jump to content

Categorizing


Omiddy

Recommended Posts

Hi,

I'm pretty sure that I am complicating thing for something I'm trying to do.

In my database, I have some posts, each with a category field.

what I'm trying to do is have a list of categories with each post that falls in that category listed underneath each category.

ie:

<h3>Category 1</h3>
<ul class="nav1">
            <li><a href="#">Title of post with category 1</a></li>
            <li><a href="#">Title of another post with category 1</a></li>
            <li><a href="#">Title of 3rd post with category 1</a></li>
</ul>
<h3>Category 2</h3>
<ul class="nav1">
            <li><a href="#">Title of post with category 2</a></li>
            <li><a href="#">Title of another post with category 2</a></li>
            <li><a href="#">Title of 3rd post with category 2</a></li>
</ul>

 

Can someone please tell me what the easiest way to do this is? I've tried different tutorials and methods i could think of, but they overcomplicated things.

 

any help would be appreciated,

thank you  : )

Link to comment
Share on other sites

I had todo this recently. Try something like this..

<?php 

$categories = mysql_fetch_array(mysql_query("SELECT DISTINCT `category` FROM (`blog_posts`)"));

foreach ($categories as $category){

echo '<li><a href="http://example.com/blog/category/'.$category['category'].'">'.$category['category'].'</a></li>';

}

 

 

 

 

 

Link to comment
Share on other sites

To do what the OP asked, would require that you retrieve the rows you want in the order that you want them. Then iterate over the result set and output it the way you want -

<?php
$query = "SELECT * FROM your_table WHERE your_where_condition_here ORDER BY category, title"; // order the titles under each category
if($result = mysql_query($query)){
if(mysql_num_rows($result)){
	// one or more rows in the result set
	$last_category = null; // remember the last category, initialize to a value that will never exist in the data
	while($row = mysql_fetch_assoc($result)){
		if($last_category != $row['category']){
			if($last_category != null){
				// close the previous <ul>
				echo "</ul>\n";
			}
			// the category changed, output the header
			echo "<h3>{$row['category']}</h3>\n<ul class=\"nav1\">\n";
			$last_category = $row['category']; // remember the new category
		}
		// output the data from the row
		echo "<li><a href=\"#\">{$row['title']}</a></li>\n";
	}
	echo "</ul>\n"; // close the last <ul>
} else {
	// query worked, but matched zero rows
	echo "Your query did not match any existing data";
}
} else {
// query failed due to an error
echo "Query failed: $query<br />" . mysql_error(); // for debugging purposes only...
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.