views:

19

answers:

1

i have this php algorithm ranking function(hacker news),

function calculate_score($votes, $item_hour_age, $gravity=1.8){
    return ($votes - 1) / pow(($item_hour_age+2), $gravity);
}

and i have this table in mysql:

post{id, post_text, votes, date}:

i was wondering how i can pass these mysql data paramters to that function, to determine the order they go in, PS. $item_hour_age, is how many hours the post is been posted

+2  A: 

Well you already have $votes, so you only need to change $date into hours then:

 $item_hour_age = (time() - $date) / 60 / 60;   // minutes+seconds

Assemble the output in a result array and sort it:

 foreach ($pdo->fetchAll() as $article) {

     $score = calculate_score(...);

     $r[$score] = $article;
 }
 krsort($r);

Afterwards loop over the article list from $r and output them.

mario
thanks, but how do i order my results using this funtion? when i pass the parameters
getaway
You'll have to assemble them, use the score as array index...
mario
thats what i dont get, im new to php
getaway
See edit. Instead of `$pdo->fetchAll()` you might have to use the old mysql_fetch_array or whatever you are using to get your posts.
mario
just one more question, how do i limit to display the best 10 articles
getaway
You need a counter in the output loop. Use `if ($n++ >= 10) { break; }` for simplicity. Works in for and while loops, but initialize `$n=0;` beforehand.
mario