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
https://forums.phpfreaks.com/topic/212729-categorizing/
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
https://forums.phpfreaks.com/topic/212729-categorizing/#findComment-1108137
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
https://forums.phpfreaks.com/topic/212729-categorizing/#findComment-1108314
Share on other sites

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.