user3383794
user3383794

Reputation: 17

display mutidimentional array result as table

I will like to display the result of my multidimensional array in a table like this Thank you for any help.

| product ID     | Product Name | Quantity         | Price
--------------------------------------------------------------------
|$item['part_id']| $product_name| $item['quantity']| $item['price']
--------------------------------------------------------------------
|$item['part_id']| $product_name| $item['quantity']| $item['price']
......

here is the result of my array

    $result = mysqli_query($con, $sql );
    if ($result) {
    echo 'Number of item selected: ' . mysqli_affected_rows($con)."<br/>";

foreach($_SESSION["cart_array"]as $item){
$i++;
$item_id=$item['part_id'];
$sql = mysqli_query($con,"SELECT * FROM oproduct_description         
     WHERE product_id='$item_id' LIMIT 1");
While($row=mysqli_fetch_array($sql)){
$product_name=$row["name"];
}   

    echo'itemID    '.$item['part_id'].""; 
    echo'product name   '.$product_name."";
    echo 'item Quantity   '.$item['quantity']."";
    echo 'item Price   '.$item['price']."<br/>";

    }      

} 

Upvotes: 1

Views: 36

Answers (1)

user2886325
user2886325

Reputation: 56

Well instead of echoing out I would create a table array and use a function call to output the table.

$arrayVariable[] = array(
        'itemID' => $item['part_id'],
        'productName' => $product_name,
        'itemQuantity' => $item['quantity'],
        'itemPrice' => $item['price']);

function tableFunction($a){
echo '<table><tr>| product ID     | Product Name | Quantity         | Price</tr>';
foreach($a as $rowData){
    echo "<tr>|$rowData['itemID']| $rowData['productName']| $rowData['itemQuantity']| $rowData['itemPrice']</tr>";
}
echo '</table>';

}

Upvotes: 1

Related Questions