Jump to content

Forum - ORDER BY `POSTS`


jackpf

Recommended Posts

Hi all,

I've made a forum, and I've decided to allow people to order threads by whatever, eg date, views, author etc.

 

Something like so...

	#order of threads
	if(!isset($_GET['order']))
	{
		$order = 'ID';
		$ascension = 'DESC';
	}
	else if(isset($_GET['order']))
	{
		$order = mysql_real_escape_string($_GET['order']);
		$ascension = 'ASC';
	}
#use
$sql = "SELECT * FROM `$tb` WHERE `Type`='thread' ORDER BY $order $ascension";

 

Anyway, I wanted to let people order by posts as well (as in the amount of replies the thread contains), but it currently works out the number of posts by simply counting the replies it's got; there isn't a dedicated column in my forum table.

 

I was just wondering if anyone has any suggestions on how to order by replies/posts without me having to make a dedicated column..? I've been wracking my brain and I can't figure it out...

 

Thanks for any help,

Jack.

Link to comment
https://forums.phpfreaks.com/topic/149543-forum-order-by-posts/
Share on other sites

You can order using a column you've defined in the query.

 

E.g.

 

SELECT count( posts ) as threadPosts FROM forum ORDER BY threadPosts ASC

 

So just check whether they want to order it by posts (like you've already done) and then the order in which it needs to display.

 

		if(!isset($_GET['order']))
	{
		$order = 'ID';
		$ascension = 'DESC';
	}
	else // No need for if else
	{
		$order = "threadPosts";
		$ascension = 'ASC';
	}

$sql = "SELECT count( posts ) as threadPosts FROM forum ORDER BY $order $ascension";

Link to comment
https://forums.phpfreaks.com/topic/149543-forum-order-by-posts/#findComment-785324
Share on other sites

I think you've misunderstood me so I'll try to explain better.

 

In your current query to get the threads are you counting the amount of posts per thread? If yes, you then rename that virtual column and order by that.

 

So essentially

SELECT count( posts ) as threadPosts FROM forum ORDER BY threadPosts ASC

is

SELECT count( posts ) FROM forum ORDER BY count( posts ) ASC

 

You don't need to have the column in the table, you just need to add a temporary one on the fly.

 

If this still doesn't make any sense, please post your code for retrieving the post count and I'll take another look.

Link to comment
https://forums.phpfreaks.com/topic/149543-forum-order-by-posts/#findComment-785395
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.