0

Hi everyone i have one question for my dashboard rank panel.

the number of page views I want to sort from smallest to largest.

For example: a total of 100 times a page, but the other page ... 75-74-73-68-45-30 80 times.

I want to get older and smaller than the sequencing of numbers.

My php code is this. post_view is for how many people visit my post .

<?php include("connect.php");
$select_posts = "SELECT * FROM posts  LIMIT 0,9";
$run_posts = mysql_query($select_posts);
while($row=mysql_fetch_array($run_posts)){
$post_id = $row['post_id'];
$post_title = $row['post_title'];
$post_date = date('d-m-y');
$post_author = $row['post_author'];
$post_view = $row['post_view'];
?>

<div class="div_name">
<div class="page-id"><?php echo $post_id; ?></div>
<div class="post_title"><?php echo $post_title; ?></div>
<div class="post-view"><?php echo $post_view; ?> </div>
</div>
<?php } ?>

1 Answers1

2

Use SQL's ORDER BY in the query:

SELECT * FROM posts ORDER BY post_view ASC LIMIT 0,9

If this field is not set to a numerical, you can use the following syntax

SELECT * FROM posts ORDER BY cast(post_view as int) ASC LIMIT 0,9
Community
  • 1
  • 1
kero
  • 10,647
  • 5
  • 41
  • 51
  • How is going from the number of biggest to smallest –  Nov 28 '13 at 20:56
  • Use `DESC` instead of `ASC` – kero Nov 28 '13 at 20:57
  • I tried it but not for me. I want it one post page visit 20 times other post visit 28 times another post is visited 15 times . I want ranking 28 20 15 . –  Nov 28 '13 at 21:00
  • 1
    `DESC` should do exactly that. – kero Nov 28 '13 at 21:02
  • but it shows like this : 1. 9 - 2. 6 - 3. 6 - 4. 33 - 5. 28 - 6. 2 –  Nov 28 '13 at 21:07
  • Most have watched 33 times but its not first number –  Nov 28 '13 at 21:08
  • 1
    Have you properly set the field to a numeric type? – kero Nov 28 '13 at 21:09
  • [This](http://stackoverflow.com/a/17762667/1557526) helps? If it doesn't please update your question with the database structure – kero Nov 28 '13 at 21:15
  • @kingkero is probably right on this one, you probably have your post_view as `VARCHAR` or something, in that case use `ORDER BY CAST(post_view AS DECIMAL(5)) DESC LIMIT 0,9` – Tin Tran Nov 28 '13 at 21:18
  • @kingkero https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-prn2/1403693_733838836644424_806580954_o.jpg This is my dashboard screenshot –  Nov 28 '13 at 21:18