简体   繁体   中英

mysql_num_rows result order

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM