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.

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

报告相同问题?

悬赏问题

  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测