Jump to content

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

Yeah, but...the thing is, I don't have a column for threadPosts. It simply works it out by counting all the replies from the table. So I can't actually order it by threadPosts because it doesn't exist...

 

Thanks for your help though.

 

Anyone at all..?

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

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.