我有一個數據庫輸出循環。如何互相乘以未知數量的php數組值?
我需要相乘值$total
,結果賦給另一個變量和輸出它。我應該怎麼做?
$quantity
來自$_POST
。
$items = implode(",",array_keys($_POST));
mysql_query('SET NAMES utf8');
$query = 'SELECT * FROM
items
WHERE
item_id IN (' . $items . ')';
$result = mysql_query($query, $db) or die (mysql_error($db));
echo '
<table id="items">
<tr class="head">
<td>Name</td>
<td>Cost</td>
<td>Quantity</td>
<td>Subtotal</td>
</tr>
';
$total = array();
while ($row = mysql_fetch_assoc($result)) {
echo '<tr class="targetfields">';
echo '<td>' . $row['item_name'] . '</td>
<td>' . $row['item_cost'];
//Getting item id
foreach ($_POST as $itemid=>$quantity)
{
//Displayin' quantity
if ($itemid == $row['item_id']){
echo '</td><td><input name="' . $row['item_id'] . '" class="input-small" size="2" maxlength="2" type="text" value="';
echo "{$quantity}";
echo '" readonly></td>
<td>'; $sum = ($row['item_cost'] * $quantity); echo $sum;
echo '</td>';
$total.= $sum;
}
}
echo '</tr>';
}
?> <tr>
<td class="sum" colspan="4"> Total:
<?php
?> </td>
</tr>
</table>
你能告訴您的完整代碼'$ quantity'初始化 – 2013-03-25 07:34:23
@Yogesh這是 – 2013-03-25 07:44:52
所以你要的'$行乘法[「ITEM_COST」] * $數量'並將其計算存儲在每個迭代中的數組中? – 2013-03-25 07:48:49