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 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能