2

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>
4
  • Can you show your full code with $quantity initialization Commented Mar 25, 2013 at 7:34
  • so you want multiplication of $row['item_cost'] * $quantity and store its calculation in array for each iteration? Commented Mar 25, 2013 at 7:48
  • @Yogesh Yes, exactly. I'm taking the array to store calculations of every iteration, then myltiplying every calculation by each other and output it. Commented Mar 25, 2013 at 7:56
  • Then Prashant's answer is absolutely correct. Commented Mar 25, 2013 at 7:58

2 Answers 2

2
  1. $total .= $sum; this will not work change it to : $total[] = $sum;
  2. To get the product all elements of array $total use : after while loop array_product

ie :

$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));
$total = array ();  
while ($row = mysql_fetch_assoc($result)) {

$sum = ($row['item_cost'] * $quantity); echo $sum;      
$total[] = $sum;

}

echo array_product($total);

Ref: http://php.net/manual/en/function.array-product.php

Sign up to request clarification or add additional context in comments.

Comments

2

I think it should be

$sum = 1; 
while ($row = mysql_fetch_assoc($result)) {
    $sum = $row['item_cost'] * $sum; 
}
echo $sum;      

5 Comments

What about the $quantity ?
@PrasanthBendra there is no need for quantity.Because the multiplication will be of the rows selected from query. And main thing $quantity is never initialised here so it will create error here.
Wouldn't $sum be $row['item_cost'] * 1 at each row and echo $sum would only show the last calculation?
@bestprogrammerintheworld In each iteration it will assign the multiplication to $sum and in next iteration it will be used for multiplication.
Aha ok thanks! Sometimes the most basic things are not obvious enough :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.