php - mySQL Order by Most Commented and Least Commented -


i'm trying order list of items based on amount of comments each topic shown below:

$page = $_get['page'];  $query = mysql_query("select * topic cat_id='$page' limit $start, $per_page");  if (mysql_num_rows($query)>=1) { while($rows = mysql_fetch_array($query)) { $number = $rows['topic_id']; $title = $rows['topic_title']; $description = $rows['topic_description'];  //get topic total $sqlcomment = mysql_query("select * comments topic_id='$number'"); $commentnumber = mysql_num_rows($sqlcomment); // trying order output echo topic total asc or desc echo " <ul> <li><h4>$number. $title</h4> <p>$description</p> <p>$topictime</p> <p>$commentnumber</p> </li> </ul> "; } } else { echo "<p>no records available.</p><br>"; } 

what best way order each echo $num_rows (asc/desc values)? note: i've updated full code - trying order output $commentnumber

the first query should be:

select t.*, count(c.topic_id) count topic t left join comments c on c.topic_id = t.topic_id t.cat_id = '$page' group t.topic_id order count limit $start, $per_page 

you can $commentnumber with:

$commentnumber = $rows['count']; 

you don't need second query @ all.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -