I have a database output loop.
I need to multiply values in $total, assign the result to another variable and output it. How should I do this?
$quantity comes from $_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>
$quantityinitialization$row['item_cost'] * $quantityand store its calculation in array for each iteration?Prashant'sanswer is absolutely correct.