Hello guys,
I have the following code and is working just fine!
Also i want to have a field with the total value of $row[cost_price] and another one with $row[selling_price].
I have tried everything regarding sum functions but nothing work.
Thank you in advance.
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['part_number'] . "</td>";
echo "<td>" . $row['describtion'] . "</td>";
echo "<td>" .'€'. $row['cost_price'] . "</td>";
echo "<td>" .'€'. $row['selling_price'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
$total_cost= $row['cost_price'] * $row['quantity'];
echo "<td>" .'€'. $total_cost . "</td>";
$total_selling= $row['selling_price'] * $row['quantity'];
echo "<td>" .'€'. $total_selling . "</td>";
echo "</tr>";
}
echo "Total ". $row['quantity']. " = $". $row['SUM(quantity)'];
PHP Array
Moderator: General Moderators
Re: PHP Array
$row is singular item, more over SUM() isn't a valid function call.
Dependent on the path you want to take, you could choose to do it in SQL or in PHP..
SELECT SUM(quantity) as totalQuantity FROM X
OR
Dependent on the path you want to take, you could choose to do it in SQL or in PHP..
SELECT SUM(quantity) as totalQuantity FROM X
OR
Code: Select all
While ( $row = fetch.. )
{
$quantity += $row['quantity'];
.. echo stuff here
}
echo "Total quantity: ".$quantity;Re: PHP Array
Thank you for your answer!
I'm sorry i was wrong. I want to have the sum of $total_cost and $total_selling of each row (after the multiplication i make)
$total_cost= $row['cost_price'] * $row['quantity'];
$total_selling= $row['selling_price'] * $row['quantity'];
I'm sorry i was wrong. I want to have the sum of $total_cost and $total_selling of each row (after the multiplication i make)
$total_cost= $row['cost_price'] * $row['quantity'];
$total_selling= $row['selling_price'] * $row['quantity'];
Re: PHP Array
$total_cost = 0;
$total_selling = 0;
$tot_cost = 0;
$tot_selling = 0;
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['part_number'] . "</td>";
echo "<td>" . $row['describtion'] . "</td>";
echo "<td>" .'€'. $row['cost_price'] . "</td>";
echo "<td>" .'€'. $row['selling_price'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
$total_cost= $row['cost_price'] * $row['quantity'];
echo "<td>" .'€'. $total_cost . "</td>";
$total_selling= $row['selling_price'] * $row['quantity'];
echo "<td>" .'€'. $total_selling . "</td>";
echo "</tr>";
$total_cost= $row['cost_price'] * $row['quantity'];
$total_selling= $row['selling_price'] * $row['quantity'];
$tot_cost = $tot_cost + $total_cost;
$tot_selling = $tot_selling + $total_selling;
}
echo "Total Cost = $"+$tot_cost."<br/>";
echo "Total Selling = $"+$tot_selling."<br/>";
$total_selling = 0;
$tot_cost = 0;
$tot_selling = 0;
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['part_number'] . "</td>";
echo "<td>" . $row['describtion'] . "</td>";
echo "<td>" .'€'. $row['cost_price'] . "</td>";
echo "<td>" .'€'. $row['selling_price'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
$total_cost= $row['cost_price'] * $row['quantity'];
echo "<td>" .'€'. $total_cost . "</td>";
$total_selling= $row['selling_price'] * $row['quantity'];
echo "<td>" .'€'. $total_selling . "</td>";
echo "</tr>";
$total_cost= $row['cost_price'] * $row['quantity'];
$total_selling= $row['selling_price'] * $row['quantity'];
$tot_cost = $tot_cost + $total_cost;
$tot_selling = $tot_selling + $total_selling;
}
echo "Total Cost = $"+$tot_cost."<br/>";
echo "Total Selling = $"+$tot_selling."<br/>";
Re: PHP Array
Try this code and execute it
Re: PHP Array
Code: Select all
$color="1";
while($rows=mysql_fetch_array($result)){
$total_selling= $rows['selling_price'] * $rows['quantity'];
$total_cost= $rows['cost_price'] * $rows['quantity'];
if($color==1){
echo "<tr bgcolor='#E9E9E9'>
<td>".$rows['part_number']."</td>
<td>".$rows['describtion']."</td>
<td>".$rows['cost_price']."</td>
<td>".$rows['selling_price']."</td>
<td>".$rows['quantity']."</td>
<td>".'€'. $total_cost ."</td>
<td>".'€'. $total_selling ."</td>
</tr>";
$color="2";
}
// When $color not equal 1, use this table row color
else {
echo "<tr>
<td>".$rows['part_number']."</td>
<td>".$rows['describtion']."</td>
<td>".$rows['cost_price']."</td>
<td>".$rows['selling_price']."</td>
<td>".$rows['quantity']."</td>
<td>".'€'. $total_cost ."</td>
<td>".'€'. $total_selling ."</td>
</tr>";
// Set $color back to 1
$color="1";
}
}
echo '</table>';
mysql_close();[/PHPNET] Re: PHP Array
Please see my new code above:dsnraju wrote:Try this code and execute it
$total_cost= $row['cost_price'] * $row['quantity'];
$total_selling= $row['selling_price'] * $row['quantity'];
after the above code i want the sum of all rows of each one (total_cost and total_selling)