简体   繁体   中英

how to show multiple record in one table row

How to show multiple record in one table row ? I'm not good explain using words, so i'll use an example :

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

as you can see, product is made from multiple items. So far, i can make more less like example above but my question is how to display item like example above. I want to show items in one row. How to show that using php ?

If you already have 2 tables: one for products and one for items you simply have to loop throw the products one and for each product get all the items needed for that product and display there.

Your code needs to look something like this:

$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.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM