2017-03-01 61 views
0

我有一個關於數組乘法和表格形式顯示的任務,我的老師給我提供了數組形式的數據,並表示將它顯示在表格中形式,也乘以項目**數量和價格**,並在表中顯示....我已經以表格形式顯示數組,但我無法產生數量和價格?有人幫忙嗎?如何在數組上執行計算並以表格形式顯示


<!doctype html> 
    <html> 
    <head> 
    <meta charset="utf-8"> 
<title>Untitled Document</title> 
</head> 

<body> 
<?php 
$ProductsTest = array("1001" => array("Name" => "LCD","Desc" => "Sony","Qty" =>10,"Price" => 2000), 
"1002" => array("Name" => "Mouse","Desc" => "Dell","Qty" =>3,"Price" => 300), 
"1003" => array("Name" =>"Mouse","Desc" =>"a4Tech","Qty" =>3,"Price" =>300), 
"1004" => array("Name" =>"USB","Desc" =>"Kingstton","Qty" =>15,"Price" =>700), 
"1005" => array("Name" =>"HeadPhone","Desc" =>"a4Tech","Qty" =>7,"Price" =>3000)); 
$no_of_ele = count($ProductsTest); 
echo '<br><br>'; 
echo ' <table border="2", bgcolor="white",width="100%"> 
<tr> 
<th>Name</th> 
    <th>Brand</th> 
    <th>QTY</th> 
    <th>price</th> 
    <th>totalamount</th> 
    </tr>'; 
foreach($ProductsTest as $values) 
{ 
    echo '<tr>'; 
    foreach($values as $key) 
    { 
     echo '<td>'.$key.'</td>'; 
    } 
    echo '</tr>'; 
} 

echo '</table>'; 
?> 

</body> 
</html> 

這是我的輸出至今

回答

1
<?php 
$ProductsTest = array("1001" => array("Name" => "LCD","Desc" => "Sony","Qty" =>10,"Price" => 2000), 
"1002" => array("Name" => "Mouse","Desc" => "Dell","Qty" =>3,"Price" => 300), 
"1003" => array("Name" =>"Mouse","Desc" =>"a4Tech","Qty" =>3,"Price" =>300), 
"1004" => array("Name" =>"USB","Desc" =>"Kingstton","Qty" =>15,"Price" =>700), 
"1005" => array("Name" =>"HeadPhone","Desc" =>"a4Tech","Qty" =>7,"Price" =>3000)); 
$no_of_ele = count($ProductsTest); 
?> 
<br><br> 
<table border="2", bgcolor="white",width="100%"> 
<tr> 
<th>Name</th> 
    <th>Brand</th> 
    <th>QTY</th> 
    <th>price</th> 
    <th>totalamount</th> 
    </tr> 
<?php 
foreach($ProductsTest as $key => $value) 
{ 
    echo '<tr>'; 
    echo '<td>'.$value['Name'].'</td>'; 
    echo '<td>'.$value['Desc'].'</td>'; 
    echo '<td>'.$value['Qty'].'</td>'; 
    echo '<td>'.$value['Price'].'</td>'; 
    echo '<td>'.$value['Qty']*$value['Price'].'</td>'; 
    echo '</tr>'; 
} 
?> 
</table> 
+0

thnku非常適合我:) –

+0

太棒了!那麼請接受我的回答。 – Naincy

0
<!doctype html> 
    <html> 
    <head> 
    <meta charset="utf-8"> 
    <title>Untitled Document</title> 
    </head> 

    <body> 
    <?php 
    $ProductsTest = array(
    "1001" => array("Name" => "LCD", "Desc" => "Sony", "Qty" =>10, "Price" => 2000), 
    "1002" => array("Name" => "Mouse","Desc" => "Dell","Qty" =>3,"Price" => 300), 
    "1003" => array("Name" =>"Mouse","Desc" =>"a4Tech","Qty" =>3,"Price" =>300), 
    "1004" => array("Name" =>"USB","Desc" =>"Kingstton","Qty" =>15,"Price" =>700), 
    "1005" => array("Name" =>"HeadPhone","Desc" =>"a4Tech","Qty" =>7,"Price" =>3000) 
    ); 

    $no_of_ele = count($ProductsTest); 

    echo '<br><br>'; 

    if($no_of_ele > 0) 
    { 
    echo '<table border="2", bgcolor="white",width="100%"> 
    <tr> 
    <th>Name</th> 
    <th>Brand</th> 
    <th>QTY</th> 
    <th>price</th> 
    <th>totalamount</th> 
    </tr>'; 

    foreach($ProductsTest as $key_ProductsTest => $value_ProductsTest) 
    { 
    echo '<tr>'; 
    echo '<td>'.$value_ProductsTest["Name"].'</td>'; 
    echo '<td>'.$value_ProductsTest["Desc"].'</td>'; 
    echo '<td>'.$value_ProductsTest["Qty"].'</td>'; 
    echo '<td>'.$value_ProductsTest["Price"].'</td>'; 
    echo '<td>'. ($value_ProductsTest["Qty"] * $value_ProductsTest["Price"]) .'</td>'; 
    echo '</tr>'; 
    } 

    echo '</table>'; 
    } 
    else 
    { 
    echo "Sorry, no data found."; 
    } 


    ?> 

    </body> 
    </html> 
0

這是一個很好的soluction使用函數式編程,你可以映射陣列這個問題與返回其他陣列整體搭配

<!doctype html> 
     <html> 
     <head> 
     <meta charset="utf-8"> 
    <title>Untitled Document</title> 
    </head> 

    <body> 
    <?php 
    $lists = array("1001" => array("Name" => "LCD","Desc" => "Sony","Qty" =>10,"Price" => 2000), 
    "1002" => array("Name" => "Mouse","Desc" => "Dell","Qty" =>3,"Price" => 300), 
    "1003" => array("Name" =>"Mouse","Desc" =>"a4Tech","Qty" =>3,"Price" =>300), 
    "1004" => array("Name" =>"USB","Desc" =>"Kingstton","Qty" =>15,"Price" =>700), 
    "1005" => array("Name" =>"HeadPhone","Desc" =>"a4Tech","Qty" =>7,"Price" =>3000)); 

    $ProductsTest = array_map(function($item){ 
     $item['totalamount'] = $item['Qty'] * $item['Price']; 
     return $item; 
    },$lists); 
$no_of_ele = count($ProductsTest); 
    echo '<br><br>'; 
    echo ' <table border="2", bgcolor="white",width="100%"> 
    <tr> 
    <th>Name</th> 
     <th>Brand</th> 
     <th>QTY</th> 
     <th>price</th> 
     <th>totalamount</th> 
     </tr>'; 
    foreach($ProductsTest as $values) 
    { 
     echo '<tr>'; 
     foreach($values as $key) 
     { 
      echo '<td>'.$key.'</td>'; 
     } 
     echo '</tr>'; 
    } 

    echo '</table>'; 
    ?> 

    </body> 
    </html> 
相關問題