duanha3539 2018-12-05 16:32
浏览 14
已采纳

人口最多的文章

I have articles and each of them has views. And in sidebar i display the most visited articles. All is working, but with order is problem. For example i have 4 articles. 1 article has 10 views, 2 - 7 views, 3 - 6 views, 4 - 3 views. And order is

1) 1 article - 10
2) 2 article - 7
3) 3 article - 6
4) 4 article - 3

So we can see that all is ok. But when i add one more artilce and it has 1 views order change and 5th artilce goes to top but why. It has lower views then another articles? And when 5th article get 10 views, all is ok and order restored. But why just when order resores when last artilce has more then 10 views?

<?php 
    $dbname="php";
    $dbhost="localhost";
    $dbusername="root";
    $dbuserpassword="";
    $optins= array(PDO::MYSQL_ATTR_INIT_COMMAND=>'set NAMES utf8');

    try{
        $db= new PDO("mysql:host={$dbhost};dbname={$dbname};chartset=utf8",
        $dbusername,$dbuserpassword,$optins);
    } catch (PDOException $ex) {
        die("Fail to connect".$ex->getMessage());
    }
        
    // all post 
    $sql = "select * from tbl_blog order by page_view desc limit 5";
    $data=$db->prepare($sql);
    $data->execute();
    $allpost=$data->fetchAll();
    // Single Post

CREATE TABLE:

CREATE TABLE tbl_blog (
  id int(11) NOT NULL,
  blog_title text NOT NULL,
  description text NOT NULL,
  content text NOT NULL,
  page_view varchar(10) NOT NULL,
  category text NOT NULL,
  img varchar(225) NOT NULL,
  sub_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE = InnoDB DEFAULT CHARSET = utf8
</div>
  • 写回答

2条回答 默认 最新

  • dow46218 2018-12-05 16:44
    关注

    For me, it's seems that the alphabetic order take control of the page_view attribute.

    If your page_view attribute is define as varchar, it will be ordered like a character, for exemple:

    "3" "2" "12" "11" "10" "1".

    Check to put the page_view attribute as integer, to have 1 2 3 10 11 12

    Hope this help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?