Page 1 of 1

Sorting news archives by date AND popularity

Posted: Sat Jul 19, 2003 4:55 am
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

Posted: Sat Jul 19, 2003 5:23 am
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."'"; 
?>

Posted: Sat Jul 19, 2003 5:43 am
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

Posted: Sat Jul 19, 2003 6:35 am
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)...

Posted: Sat Jul 19, 2003 7:04 am
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

Posted: Sat Jul 19, 2003 3:35 pm
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