【问题标题】:Sort Data in PHP在 PHP 中对数据进行排序
【发布时间】:2020-10-01 03:41:35
【问题描述】:

以下代码:

<?php
echo "<h3>Result</h3>";
$attribute1 = mysqli_query($conn, "SELECT min(c1) as min1, max(c2) as max1, max(c3) as max2 from tb_alternative");
$atr1 = mysqli_fetch_array($attribute1);
    $sql3 = mysqli_query($conn, "SELECT * from tb_alternative");
    while($r3 = mysqli_fetch_array($sql3, MYSQLI_ASSOC)){
        $point = ((($atr1['min1']/$r3['c1'])*$bobot[0])+
        (($r3['c2']/$atr1['max1'])*$bobot[1])+
        (($r3['c3']/$atr1['max2'])*$bobot[2]));

?>
<tr>
        <td><?php echo $r3['id']?></td>
        <td><?php echo $r3['alternative']?></td>
        <td><?php echo $point?></td>
</tr>
<?php            
    }
?>

结果:

如何按总分对数据进行排序?,总分是计算出来的,不是从数据库中计算出来的

【问题讨论】:

  • php.net/manual/en/function.usort.php。或者在查询中进行计算并在那里排序。
  • 我认为您正在寻找 SQL 排序。
  • 不能在 MySQL 中完成,因为$bobot 不是 SQL 结果的一部分
  • @SlavaRozhnev 当然可以。在查询中包含 $bobot 的值。

标签: php html web mysqli


【解决方案1】:

您可以对结果进行排序,但在mysqli_fetch 循环中显示结果时不能。

  1. 将您的查询结果提取到一个数组并在那里计算您的点
  2. 使用usort() 对数组进行排序
  3. 使用独立循环显示您的数据(如foreach

【讨论】:

    【解决方案2】:

    你有两个选择。如果您将 PHP 和 HTML 分开,它们都将工作得最好。第一个选项使用window functions,第二个使用PHP usort()函数。

    在 SQL 中使用窗口函数

    您可以使用MAX() OVER(),它将为您提供每行中每列的最大值。假设$bobot 中的值不是来自 SQL 查询,您需要将它们传递给 SQL 以在计算中使用。然后,您可以在单个 SQL 查询中执行整个计算并使用 ORDER BY 进行排序

    例如:

    $stmt = $conn->prepare('SELECT id, alternative, 
            (min(c1) OVER()/c1) * ? + (c2/max(c2) OVER()) * ? + (c3/max(c3) OVER()) * ? AS point
        FROM tb_alternative
        ORDER BY point');
    $stmt->bind_param('sss', $bobot[0], $bobot[1], $bobot[2]);
    $stmt->execute();
    $data = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
    
    echo "<h3>Result</h3>";
    echo "<table>";
    foreach ($data as $r3) :
    ?>
        <tr>
            <td><?= $r3['id'] ?></td>
            <td><?= $r3['alternative'] ?></td>
            <td><?= $r3['point'] ?></td>
        </tr>
    <?php
    endforeach;
    ?>
    

    使用 PHP usort()

    如果由于某种原因您不能使用窗口函数,或者您更喜欢在 PHP 中进行更复杂的计算,那么如果您将所有值提取到一个数组中并在计算后对数组进行排序,您也可以这样做。

    $atr1 = $conn->query("SELECT min(c1) as min1, max(c2) as max1, max(c3) as max2 from tb_alternative")->fetch_assoc();
    $sql3 = $conn->query("SELECT * from tb_alternative");
    $data = [];
    foreach ($sql3 as $row) {
        $data[] = [
            'id' => $row['id'],
            'alternative' => $row['alternative'],
            'point' => ($atr1['min1'] / $row['c1'] * $bobot[0]) + ($row['c2'] / $atr1['max1'] * $bobot[1]) + ($row['c3'] / $atr1['max2'] * $bobot[2])
        ];
    }
    
    usort($data, fn ($a, $b) => $b <=> $a);
    // or if you are still using PHP 7.3:
    usort($data, function ($a, $b) {
        return $b <=> $a;
    });
    
    echo "<h3>Result</h3>";
    echo "<table>";
    foreach ($data as $r3) :
    ?>
        <tr>
            <td><?= $r3['id'] ?></td>
            <td><?= $r3['alternative'] ?></td>
            <td><?= $r3['point'] ?></td>
        </tr>
    <?php
    endforeach;
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      • 2015-10-01
      相关资源
      最近更新 更多