1

This is a question regarding the mysql_num_rows command. Basically what I want to ask if it's possible to sum the results up and order them in a descending order.

 $fetchrank = mysql_query("SELECT * FROM leden WHERE level = 6 OR level = 106");
 while ($getrank = mysql_fetch_array($fetchrank, MYSQL_ASSOC)) {

    $getranked = mysql_query("SELECT nieuws_id FROM nieuws_berichten WHERE member_id='".$getrank['member_id']."'");
    $critical = mysql_num_rows($getranked);
    $posts = $critical;

    echo"".stripslashes(substr($getrank['gebruikersnaam'],0,25))." has ".$posts." posts!";
}

echo"</div>";

}

This actually shows it what I want, but I want it to order the num_rows results now, from highest to lowest. Is that possible through an array? so I can use a PHP command or is there another way?

I am aware that mysql_num_rows is outdated, however at the moment I'll be working with this old framework and perhaps in the near future we'll be changing to another.

  • mysql_num_rows returns a single number. What do you mean with "order mysq_num_rows"? – Lelio Faieta Apr 19 '15 at 13:57
  • The previous query selects an amount of users, each user has it's own number of posts and that's where the mysql_num_rows comes in to place to count up the posts.. However I want to sort the posts from highest to lowest now. Is that possible by any chance? – Ahmed Beni Touzine Apr 20 '15 at 18:17
  • As I told you before you can do it with PHP when you use sort functions or you can do it in MYSQL when you add order_by to your query. I don't know what is your table structure so I suppose that you can add ORDER_BY member_id to get the result. – Lelio Faieta Apr 21 '15 at 07:26

1 Answers1

0

If I understand right what you want to do actually you can do:

$fetchrank = mysql_query("SELECT * FROM leden WHERE level = 6 OR level = 106");

$totalposts = 0;
$getranks = mysql_fetch_array($fetchrank, MYSQL_ASSOC);
$getranks = rsort($getranks);
foreach ($getranks as $getrank) {

$getranked = mysql_query("SELECT nieuws_id FROM nieuws_berichten WHERE member_id='".$getrank['member_id']."'");
$critical = mysql_num_rows($getranked);
$posts = $critical;
$totalpostst = += $posts;
echo"".stripslashes(substr($getrank['gebruikersnaam'],0,25))." has ".$posts." posts!";
}
echo $totalposts;
echo"</div>";

to get the total amount of posts retrieved. To sort them you have to sort the array using asort or rsort (depending on the direction). See the man page for this. Sort is to be applied on the array. So I changed the code accordingly. Adapt

$getranks = rstort($getranks);

to your need based on the manual. You can see array content using

echo'<pre>';
print_r($getranks);
echo '</pre>';

to see what have changed inside the array.

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74