简体   繁体   中英

Is something wrong in the while loop?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM