【问题标题】:How to combine array rows and make it variable如何组合数组行并使其可变
【发布时间】:2017-05-26 22:07:24
【问题描述】:

我正在使用这种 w3schools 方式从数据库中获取数据:

$sql = "SELECT num_of_reservations FROM table WHERE date = '$date";
$result = $conn->query($sql);

while($row = $result->fetch_assoc()) {
    echo $row["num_of_reservations"];
}

我的数据库如下所示:

id - date - num_of_reservations - name
1 - 2017-02-02 - 3 - somebody
2 - 2017-02-02 - 5 - somebody
3 - 2017-02-02 - 7 - somebody

如果我想从数据库中回显行,这可以正常工作,但现在我需要制作允许您仅在同一天的预订少于 15 个时进行预订的表格。我当前设计中的问题是每个 num_of_reservations 行都将转到不同的数组(因为我有 while 循环),我需要将它们设为 + (3+5+7) 以便我可以比较,是 num_of_reservations 如果是,我会显示购票屏幕,如果不是,我会告诉您改天选择。

【问题讨论】:

    标签: php mysql arrays loops


    【解决方案1】:

    您可以使用另一个查询来获取所有预订的总和:

    $sql = "SELECT SUM(num_of_reservations) AS sum_of_reservations FROM table WHERE date = '$date'";
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();
    
    if ($row['sum_of_reservations'] <= 15) {
        //display ticket buying screen.
    } else {
        //tell to pick another day.
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-05
      • 2019-12-20
      • 2016-02-06
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多