Sorting news archives by date AND popularity

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Sorting news archives by date AND popularity

Post by robster »

Hi,

I've got my news archives working fine (thanks everyone that's helped).
What I'm finding now is I'm having trouble sorting by popularity.

I have a table for the news items, it has id, news, subject, image, link, popularity as its fields.

I have got the popularity incrimenting by one each time a news item is clicked on, works just fine :)

On the page that displays the news I ahve a lil menu on the left that lets users view news by date (asc/desc) or by popularity (asc/desc).

OK, the page that has the menu sends its variables like this:

Code: Select all

<a href="/news_archives.php?direction=ASC">Old stories First</a><br>
<a href="/news_archives.php?direction=DESC">New stories First</a><br>
<a href="/news_archives.php?direction=popplus">Most Popular First</a><br>
<a href="/news_archives.php?direction=popminus">Least Polular First</a><br>

I got the sort by date one working originally with this method:

Code: Select all

$travel   = $_GET['direction'];
	// Make sure user hasn't set the direction variable to something other than DESC and ASC
	if (($travel == "DESC") || ($travel == "ASC"))
	{
   	mysql_select_db($dbname);
	$sql = "SELECT * FROM newscontent ORDER BY id $travel";
	$content = mysql_query($sql);
	}
	else { }  // Do nothing because the user has altered the value of direction

So now I want to get the sort by popularity section and my feeble knowlege of PHP led me to try this:

Code: Select all

$travel   = $_GET['direction'];


	if ($travel = "popplus") 
	{
	$dir = "ASC";
  	mysql_select_db($dbname);
	$sql = "SELECT * FROM newscontent ORDER BY popularity $dir";
	$content = mysql_query($sql);
	}
	else { }  // Do nothing because the user has altered the value of direction 

	if ($travel = "popminus") 
	{
	$dir = "DESC";
  	mysql_select_db($dbname);
	$sql = "SELECT * FROM newscontent ORDER BY popularity $dir";
	$content = mysql_query($sql);
	}
	else { }  // Do nothing because the user has altered the value of direction 

	if ($travel = "ASC") 
	{
	$dir = "ASC";
  	mysql_select_db($dbname);
	$sql = "SELECT * FROM newscontent ORDER BY id $dir";
	$content = mysql_query($sql);
	}
	else { }  // Do nothing because the user has altered the value of direction 


	if ($travel = "DESC") 
	{
	$dir = "DESC";
  	mysql_select_db($dbname);
	$sql = "SELECT * FROM newscontent ORDER BY id $dir";
	$content = mysql_query($sql);
	}
	else { }  // Do nothing because the user has altered the value of direction
This didn't work (of course you say! ;) ). I'm wondering if someone could help enlighten me. I thought I was REALLY clever thinking up this method... I guess not :(

:)

Thanks for all your help everyone, what a fantastic community this is.

Rob
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Post by Fredix »

WHAT didn't work?
I guess the

Code: Select all

<?php
 $sql = "SELECT * FROM newscontent ORDER BY id $travel"; 
?>
part.
That's because PHP interpreter tries to ORDER BY id $travel and not DESC
to solve that problem try somthing like

Code: Select all

<?php
 $sql = "SELECT * FROM newscontent ORDER BY id '".$travel."'"; 
?>
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

slight misunderstanding I think. Yes, that bit worked fine. That gave the ability to sort by date. It worked a treat :)

The bit that didn't work was the next, large piece of code I posted where I tried to sort by date AND popularity.

Any ideas? It's the one with the 4 if statements...

:)

Rob
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

perhaps an elseif mighe be the go... I'll look into that and get back for any other newbies who need help later on (learn from my experience (or lack of) etc)...
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

ok, it works now, the elseif did it, as well I was using a single = rather than a double ==.

Here is the code that sorts either most popular or least popular along with newest or oldest first...

Code: Select all

$travel   = $_GET['direction'];


	if ($travel == "popplus") 
	{
	$dir = "DESC";
  	mysql_select_db($dbname);
	$sql = "SELECT * FROM newscontent ORDER BY popularity $dir";
	$content = mysql_query($sql);
	}

	elseif ($travel == "popminus") 
	{
	$dir = "ASC";
  	mysql_select_db($dbname);
	$sql = "SELECT * FROM newscontent ORDER BY popularity $dir";
	$content = mysql_query($sql);
	}

	elseif ($travel == "ASC") 
	{
	$dir = "ASC";
  	mysql_select_db($dbname);
	$sql = "SELECT * FROM newscontent ORDER BY id $dir";
	$content = mysql_query($sql);
	}

	elseif ($travel == "DESC") 
	{
	$dir = "DESC";
  	mysql_select_db($dbname);
	$sql = "SELECT * FROM newscontent ORDER BY id $dir";
	$content = mysql_query($sql);
	}
	else { }  // Do nothing because the user has altered the value of direction
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

yes, the problem was the single = symbol, when u do that u are assigning $travel a value, and when doing that, if there's an error or something returns false while assigning $travel the value the expression inside the if or elseif statement won't happen
Post Reply