【问题标题】:Is something wrong in the while loop?while 循环有问题吗?
【发布时间】:2015-05-23 11:35:38
【问题描述】:

我有一个 store_credits_orders 表

和订单表。

我想要的输出与此类似,但基于Store Credit Order Date and TimeStore Order Date and Time

到目前为止我尝试过的代码:

<?php
$table = '';
$queryToGetStoreCredit = "SELECT * FROM store_credits_orders WHERE SCO_CustEmailAdd = '".$_SESSION["Customer"]["email"]."'";
$validate->Query($queryToGetStoreCredit);
if ($validate->NumRows() >= 1) {
    while ($rows_sco = $validate->FetchAllDatas()) {
        $used = $i = 0;
        $table .= '<tr>';
        $table .= '<td>'.$rows_sco["SCO_OrderCode"].'</td>';
        $table .= '<td>--</td>';
        $table .= '<td>'.$rows_sco["SCO_OrderDate"].'</td>';
        $table .= '<td>--</td>';
        $table .= '<td>'.$rows_sco["SCO_Purchase_Amount"].'</td>';
        $table .= '<td>'.$rows_sco["SCO_Credit_Alloted"].'</td>';
        $table .= '<td>'.$used.'</td>';
        $table .= '<td>'.( $rows_sco["SCO_Credit_Alloted"] - $used ).'</td>';
        $table .= '</tr>';

        $validate2 = new Validation();
        $queryToGetOrder = "SELECT * FROM orders WHERE CustEmailAdd = '".$rows_sco["SCO_CustEmailAdd"]."'";
        $validate2->Query($queryToGetOrder);
        while ($row = $validate2->FetchAllDatas()) {
            $table .= '<tr>';
            $table .= '<td>--</td>';
            $table .= '<td>'.$row["OrderCode"].'</td>';
            $table .= '<td>--</td>';
            $table .= '<td>'.$row["OrderDate"].'</td>';
            $table .= '<td>--</td>';
            $table .= '<td>--</td>';
            $table .= '<td>'.$row["AppliedCredits"].'</td>';
            $table .= '<td>'.($rows_sco["SCO_Credit_Alloted"] - $row["AppliedCredits"]).'</td>';
            $table .= '</tr>';
        }
    }
}
?>

我想要实现的是,每当购买 store_credits 时,信息都会插入到store_credits_orders 表中。现在,当同一个用户来下订单并兑换store_credits(小于等于,他在他的帐户中)时,除了在orders表中插入之外,数据库中不会有任何更新。

但是当用户登录时,他应该能够看到他何时购买了 store_credits 和/或他何时兑换了 store_credits。所有这些事件都应按先发生的事件排序,无论购买或兑换。

【问题讨论】:

  • 你的更新查询在哪里?你的问题也不清楚?
  • 哪个更新查询?为了什么?
  • 您问“我需要在兑换和/或购买时更新商店积分。”?我要问的那个
  • 这需要在运行时完成...不会为此执行任何查询..
  • @halfer 感谢先生告知此事。我不知道这一切。我会确保下次不会发生这种情况。

标签: php mysql arrays while-loop


【解决方案1】:

我与@user3514160 交谈过,我很清楚他想要什么。基本上数据被正确插入。他唯一想要的就是以正确的方式显示数据。

您的查询应如下所示

(SELECT
  SCO_Id,
  SCO_OrderCode,
  SCO_CustEmailAdd,
  SCO_Purchase_Amount,
  SCO_Credit_Alloted,
  SCO_OrderDate,
  SCO_OrderIP,
  null AS OrderId,
  null AS OrderCode,
  null AS CustEmailAdd,
  null AS AppliedCredits,
  null AS OrderDate,
  'store_credits_orders' AS tableName
FROM `store_credits_orders` AS credits_orders WHERE `SCO_CustEmailAdd` = '".$_SESSION["Customer"]["email"]."')
UNION
(SELECT
  OrderId,
  OrderCode,
  CustEmailAdd,
  AppliedCredits,
  OrderDate
  null AS SCO_Id,
  null AS SCO_OrderCode,
  null AS SCO_CustEmailAdd,
  null AS SCO_Purchase_Amount,
  null AS SCO_Credit_Alloted,
  OrderDate AS SCO_OrderDate,
  null AS SCO_OrderIP,
  'store_orders' AS tableName
FROM `store_orders` AS orders WHERE `SCO_CustEmailAdd` = '".$_SESSION["Customer"]["email"]."')
ORDER BY `SCO_OrderDate` ASC

这是在一个结果中获取所有行所必需的。我们通过MySQL UNION 完成此操作。

UNION 需要两个表具有相同数量的同名列。这就是我们使用null AS columnName 添加空列的原因。

PHP 与查询

<?php
$table = '<table>';

// Query to get all rows from both tables
$queryToGetStoreCredit = 
"(SELECT
  SCO_Id,
  SCO_OrderCode,
  SCO_CustEmailAdd,
  SCO_Purchase_Amount,
  SCO_Credit_Alloted,
  SCO_OrderDate,
  SCO_OrderIP,
  null AS OrderId,
  null AS OrderCode,
  null AS CustEmailAdd,
  null AS AppliedCredits,
  null AS OrderDate,
  'store_credits_orders' AS tableName
FROM `store_credits_orders` AS credits_orders WHERE `SCO_CustEmailAdd` = '".$_SESSION["Customer"]["email"]."')
UNION
(SELECT
  OrderId,
  OrderCode,
  CustEmailAdd,
  AppliedCredits,
  OrderDate
  null AS SCO_Id,
  null AS SCO_OrderCode,
  null AS SCO_CustEmailAdd,
  null AS SCO_Purchase_Amount,
  null AS SCO_Credit_Alloted,
  OrderDate AS SCO_OrderDate,
  null AS SCO_OrderIP,
  'store_orders' AS tableName
FROM `store_orders` AS orders WHERE `SCO_CustEmailAdd` = '".$_SESSION["Customer"]["email"]."')
ORDER BY `SCO_OrderDate` ASC";

$validate->Query($queryToGetStoreCredit);
if ($validate->NumRows() >= 1) {
    // Starting balance. This could be some other number, for example if viewing some certain period of orders etc.
    $balance = 0;
    while ($row = $validate->FetchAllDatas()) {

        // Add to balance
        if($row['tableName'] == 'store_credits_orders' && (int)$row['SCO_Credit_Alloted'] > 0){
          $balance += (int)$row['SCO_Credit_Alloted']
        }
        // Remove from balance
        else if($row['tableName'] == 'store_orders' && (int)$row['AppliedCredits'] > 0){
          $balance -= (int)$row['AppliedCredits'];
        }

        $table .= '<tr>';
        $table .= '<td>'.$row["SCO_OrderCode"].'</td>';
        $table .= '<td>'.$row["OrderCode"].'</td>';
        $table .= '<td>'.(($row['tableName'] == 'store_credits_orders') ? $row["SCO_OrderDate"] : '').'</td>';
        $table .= '<td>'.$row["OrderDate"].'</td>';
        $table .= '<td>'.$row["SCO_Purchase_Amount"].'</td>';
        $table .= '<td>'.$row["SCO_Credit_Alloted"].'</td>';
        $table .= '<td>'.$row["AppliedCredits"].'</td>';
        $table .= '<td>'.$balance.'</td>';
        $table .= '</tr>';
    }
}

$table .= '</table>';

echo $table;
?>

如果有问题请告诉我。

【讨论】:

  • 在 sql 联合中明确声明返回的列而不是使用 '*'(返回所有列)总是一个好主意。原因是如果向任一表添加新列,则查询将失败。此外,您还依赖于两个表中的适当列的顺序相同。
  • 是的,编辑了我的答案。 @RyanVincent,你说的依赖列的相同顺序是什么意思?
  • 如果我们为第二个查询的最后两列设置 null,SCO_Credit_Alloted 将得到 OrderDate 的值,这是我们不想要的。目前所有列名都被保留。
猜你喜欢
  • 2012-05-06
  • 1970-01-01
  • 2011-06-19
  • 2011-03-04
  • 1970-01-01
  • 1970-01-01
  • 2016-06-06
  • 2018-11-05
  • 2013-12-10
相关资源
最近更新 更多