php - 堆栈在回显多次循环的结构中

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

最佳答案

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

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

注意:这是我未经测试而写的,可能有一些错误,但主要路径应该很清楚。

关于php - 堆栈在回显多次循环的结构中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26709575/

相关文章:

html - Sharepoint 中的顶部菜单导航

javascript - 为什么 div 的测量宽度与浏览器测量不同?

html - 不同对齐绝对位置

javascript - Mobile Safari 有时不会触发点击事件

php - 正则表达式。如何在一行中处理所有 HTML 标签?

php - 如何生成好的盐——我的函数是否足够安全?

php - 如何在 Symfony 2 中使用 ajax 登录?

php - MySQL、PHP、PDO "could not find driver"错误

javascript - onClick ="history.go(0)"在 mozilla 中不起作用,但在 IE 中起作用。如何解决 mozilla 浏览器中的此问题

javascript - 如何将 `google map`到 `div`直接加载到容器中?