Jump to content


Photo

sort order by variable?


  • Please log in to reply
2 replies to this topic

#1 xtiancjs

xtiancjs
  • Members
  • PipPipPip
  • Advanced Member
  • 47 posts

Posted 26 April 2006 - 01:21 AM

Hi all,

Am wondering how to sort my results on a page by a variable. My sql code now is this:


mysql_select_db($database_broker, $broker);
$query_brokers = "SELECT * FROM brokers";
$query_limit_brokers = sprintf("%s LIMIT %d, %d", $query_brokers, $startRow_brokers, $maxRows_brokers);
$brokers = mysql_query($query_limit_brokers, $broker) or die(mysql_error());
$row_brokers = mysql_fetch_assoc($brokers);


The variable I have written looks like this :


$valrate1 = (($row_brokers['broker_rating']/$row_brokers['broker_num_votes'])+
($row_brokers['broker_rating_two']/$row_brokers['broker_num_votes_two'])+
($row_brokers['broker_rating_three']/$row_brokers['broker_num_votes_three']))/3;


basically, i want to add an ORDER BY $valrate1 to the sql query, have tried a few different things with no luck. Am not sure where the variable code should go , any ideas would be great.
xtian

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 26 April 2006 - 01:57 AM

Show us what you've tried...

Ken

#3 xtiancjs

xtiancjs
  • Members
  • PipPipPip
  • Advanced Member
  • 47 posts

Posted 26 April 2006 - 02:03 AM

Hi This is what I had:


mysql_select_db($database_broker, $broker);
$query_brokers = "SELECT * FROM brokers ORDER BY '$valrate1' DESC";
$query_limit_brokers = sprintf("%s LIMIT %d, %d", $query_brokers, $startRow_brokers, $maxRows_brokers);
$brokers = mysql_query($query_limit_brokers, $broker) or die(mysql_error());
$row_brokers = mysql_fetch_assoc($brokers);
$valrate1 = (($row_brokers['broker_rating']/$row_brokers['broker_num_votes'])+
($row_brokers['broker_rating_two']/$row_brokers['broker_num_votes_two'])+ ($row_brokers['broker_rating_three']/$row_brokers['broker_num_votes_three']))/3;





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users