while循环中有什么问题吗?

时间:2015-03-20 05:39:33

标签: php mysql arrays while-loop

我有一个store_credits_orders表

enter image description here

和订单表。

enter image description here

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

enter image description here

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

<?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(小于等于,他在他的账户中),除了在订单表中插入外,数据库中不会有更新。

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

1 个答案:

答案 0 :(得分:0)

我和@ 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;
?>

如果有问题,请告诉我。