dongwu4834 2014-11-03 07:25
浏览 43
已采纳

堆叠在回显多个循环的结构中

This is my php code for echoing contents from database.

<?php
include ("config.php");
$results = $mysqli->query
("SELECT transaction_id FROM orders_list WHERE customer_name = 'Klaudia' ORDER BY id LIMIT 100");           
if ($results) {
    while($obj = $results->fetch_object()) {
        echo '<div id="thisklop">';
        echo '<ul class="ulung">Id_cart';           
        echo '<li>'.$obj->transaction_id.'</li>';
        echo '</ul>';
                $thisthat=$obj->transaction_id;         
                $otherresults = $mysqli->query
                            ("SELECT transaction_id, items, quantity, one_product_price FROM orders_history
                              WHERE transaction_id = '$thisthat'");         
                if ($otherresults) {
                    while($obj = $otherresults->fetch_object()) {
                        echo '<div>';
                        echo '<ul class="ulung">Products';
                        echo '<li>'.$obj->items.'</li>';
                        echo '</ul>';
                        echo '</div><br>';

                        echo '<div>';
                        echo '<ul class="ulung">Quantity';
                        echo '<li>'.$obj->quantity.'</li>';
                        echo '</ul>';
                        echo '</div><br>';

                        echo '<div>';
                        echo '<ul class="ulung">Invoices';
                        echo '<li>'.$obj->one_product_price.'</li>';
                        echo '</ul>';
                        echo '</div><br>';
                    }
                }                                       
        echo '</div>';
    }
}
?>

What I want the result is like this:

[UPDATE TABLE]

 -----------------------------------------------------------------------
 id_cart         products        quantity       invoices     status
 -----------------------------------------------------------------------
                 this               2             $20        
  0001           that               1             $20        pending
                 those              2             $20        
 -----------------------------------------------------------------------
                                 Total Invoices:  $60   
 -----------------------------------------------------------------------

                 this               2             $20        
  0002           that               1             $20        approved
                 those              2             $20        
 -----------------------------------------------------------------------
                                 Total Invoices:  $60   

From the table above, the id_cart is in looping, and then inside the id_cart there are also content which are in looping.

I am stuck on the structure I am echoing it. The structure is in a mess

enter image description here

  • 写回答

1条回答 默认 最新

  • douwen1915 2014-11-03 07:57
    关注

    First, get all data in one query (faster) and then collect your data in an array first. Finally, construct the output according to the array:

    <?php
    include ("config.php");
    $results = $mysqli->query
    ("
        SELECT  DISTINCT    orders_history.transaction_id,
                            orders_history.items,
                            orders_history.quantity,
                            orders_history.one_product_price
        FROM                orders_history, orders_list
        WHERE               orders_history.transaction_id = orders_list.transaction_id
        AND                 orders_list.customer_name = 'Klaudia'"
    
    );
    
    $orders = array();     
    $html = '';     
    if ($results) {
        while($obj = $results->fetch_object()) {
            $orders[$obj->transaction_id][$obj->items] = array('quantity' => $obj->quantity, 'invoices' => $obj->one_product_price);
        }
    
            $html .= '<table><tr>';
            $html .= '<th>id_cart</th>';
            $html .= '<th>products</th>';
            $html .= '<th>quantity</th>';
            $html .= '<th>invoices</th></tr>';
            foreach ($orders AS $order_id => $order) {
                $html .= '<tr><td rowspan="' . count($order) . '">' . $order_id . '</td>';
                $row = 1;
                foreach ($order AS $item => $data) {
                    if ($row > 1) { $html .= '</tr><tr>'; }
                    $html .= '<td>' . $item . '</td>';
                    $html .= '<td>' . $data['quantity'] . '</td>';
                    $html .= '<td>' . $data['invoices'] . '</td>';
    /* INSERTED CODE FOR STATUS */
                    if ($row == 1) {
                      $html .= '<tr><td rowspan="' . count($order) . '">' . $status . '</td>';
                    }
    /* END OF INSERTION */
                    $row++;
                }
                $html .= '</tr>';
            }
            $html .= '</table>';
        } 
        echo $html;
        ?>
    

    Note: This is written untested out of my head, might have some errors but the main path should be clear.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi