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 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制