簡體   English   中英

如何在一個表行中顯示多個記錄

[英]how to show multiple record in one table row

如何在一個表行中顯示多個記錄? 我不太擅長用單詞解釋,所以我用一個例子:

Work No. | Product No. | Product Name | Qty | Item No. | Item Name | Qty |
--------------------------------------------------------------------------
W00001   | P0000001    | Product_1    |  6  | I000001  | Item_1    |  2  |
         |             |              |     | I000002  | Item_2    |  2  |
         |             |              |     | I000003  | Item_3    |  2  |
--------------------------------------------------------------------------
                                                           Total   :  6  |
--------------------------------------------------------------------------
W00002   | P0000002    | Product_2    |  7  | I000001  | Item_1    |  3  |
         |             |              |     | I000004  | Item_4    |  4  |
--------------------------------------------------------------------------
                                                           Total   :  7  |
--------------------------------------------------------------------------

如您所見,產品是由多個物料制成的。 到目前為止,我可以減少上面示例的內容,但是我的問題是如何顯示上面示例的項目。 我想一排顯示項目。 如何顯示使用php?

如果您已經有2個表:一個用於產品,一個用於產品,則只需要循環拋出一個產品,對於每個產品,獲取該產品所需的所有項目並在那里顯示。

您的代碼需要看起來像這樣:

$products = get_products_from_db(); //How you normal get products from db.
foreach($products as $product) {
    $items = get_items_by_product_from_db($product['id']);   //get from db all items for a product.
    //foreach product we will need a new row.

    echo '<tr>';
    echo "<td>{$product['work_no']}</td>";    //and all the normal cells like this.
    //for item no. we will create a cell and inside it we will foreach items.
    echo "<td>";
    foreach($items as $item) {
        echo $item['item_no'] . '<br/>';//Or how it's called in db.
    }
    echo "</td>";
    //Exact the same with name and quantity.
    //For quantity also keep a counter so you can display the total
    echo '</tr>';
    //After this row create a new tr to display the total. I think you know how to do this.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM