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

时间:2014-11-03 07:25:19

标签: php html css

这是我用于回显数据库内容的php代码。

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

我想要的结果是这样的:

[更新表]

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

从上表中,id_cart处于循环中,然后在id_cart内部还有循环中的内容。

我被困在结构上我正在回应它。结构一团糟

enter image description here

1 个答案:

答案 0 :(得分:0)

首先,在一个查询中获取所有数据(更快),然后首先在数组中收集数据。最后,根据数组构造输出:

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

注意:这是我未经测试的,可能有一些错误,但主要路径应该是明确的。