首页 > 解决方案 > 如何让前五个计算只出现

问题描述

我有一个狗展网站,并试图展示在一年中获得最多分数的前五个犬舍词缀。

我已经拆分了代码中的观点,并在两个表上使用了内部连接。一个是结果和其他狗

我在显示词缀名称及其旁边获得的分数时遇到问题。也无法根据最多积分获得前五名

警告:在第 59 行的 xxx 中遇到一个非数字值
注意:未定义的变量:第 68 行的 xxx 中的总数

下面是所有显示的内容,我认为它正在添加 if 语句。

3
6

对不起,不是很擅长,需要一些帮助。

<?php
        $query = "SELECT result.placement, result.award, result.class_name, dogs.affix
        FROM result
        INNER JOIN dogs
        ON result.resultID = dogs.dog_id
        ORDER BY dogs.affix LIMIT 5";

        $result = mysqli_query($connection, $query);

        while($row = mysqli_fetch_assoc($result)){


            $affix          = $row['affix'];
            $placement      = $row['placement'];
            $award          = $row['award'];

            if($award === 'BCC'){
                $points = $affix + 5;
            }
            if($award === 'DCC'){
                $points = $affix + 5;
            }
            if($award === 'RBCC'){
                $points = $affix + 4;
            }
            if($award === 'RDCC'){
                $points = $affix + 4;
            }
            if($placement === '1st'){
                $points = $affix + 3;
            }
            if($placement === '2nd'){
                $points = $affix + 2;
            }
            if($placement === '3rd'){
                $points = $affix + 1;
            }

            $total += $points;



            echo "<li> {$affix} {$total}</li>";

        }

        ?>

标签: phpmysql

解决方案


三个问题。

  1. ORDER BY dogs.affix LIMIT 5返回底部5 而不是顶部。应该是ORDER BY dogs.affix DESC LIMIT 5登顶。
  2. affix返回一个null或一个空字符串,这就是你得到第一个错误的原因。int not null default 0在您的数据库中定义此字段。
  3. $total 未初始化。在循环之前将其赋值为 0

推荐阅读