2017-03-01 3 views
0

配列の乗算に関する割り当てがあり、それを表形式で表示します。先生は配列形式のデータを与え、表形式で表示しますまた**商品数と価格**を掛け合わせてテーブルに表示します。すでに配列を表形式で表示していますが、QTYと価格を商品にすることができません。誰か助けて?どのように配列の計算を実行し、それを表形式で表示する


<!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> 
関連する問題