Is something wrong in the while loop?

天大地大妈咪最大 提交于 2019-12-12 06:15:51

问题


I have a store_credits_orders table

and orders table.

The output that I want is similar to this but based on Store Credit Order Date and Time and Store Order Date and Time:

The code that I have tried so far:

<?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>';
        }
    }
}
?>

What I want to achieve is, whenever there is a purchase of store_credits, the information will get inserted in the store_credits_orders table. Now when the same user comes, places an order and redeems the store_credits (less than equal to, he has in his account), there will be no update in the database other than insertion in the orders table.

But when the user logs in, he should be able to see when he has purchased the store_credits and/or when he has redeemed the store_credits. All these events should be ordered by whichever event happens first, irrespective of purchase or redeem.


回答1:


I talked to @user3514160 and I got clarity what he want's to have. Basically data gets inserted correctly. Only thing he wants is to show data in correct way.

Your query should look like this

(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

This is needed to get all rows in one result. We accomplish this with MySQL UNION.

UNION needs both tables to have same amount of columns with same names. This is why we add empty columns with null AS columnName.

And PHP with query

<?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;
?>

Let me know, if there is some problems.



来源:https://stackoverflow.com/questions/29160104/is-something-wrong-in-the-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!