PHP Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
denebx3
Forum Newbie
Posts: 6
Joined: Sun Oct 30, 2011 2:31 pm

PHP Array

Post by denebx3 »

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)'];
ouchiko
Forum Commoner
Posts: 35
Joined: Sun Oct 09, 2011 6:54 pm
Location: London

Re: PHP Array

Post by ouchiko »

$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

Code: Select all

While ( $row = fetch.. )
{
 $quantity += $row['quantity'];

.. echo stuff here
}

echo "Total quantity: ".$quantity;
denebx3
Forum Newbie
Posts: 6
Joined: Sun Oct 30, 2011 2:31 pm

Re: PHP Array

Post by denebx3 »

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'];
dsnraju
Forum Newbie
Posts: 10
Joined: Sun Oct 30, 2011 3:39 am

Re: PHP Array

Post by dsnraju »

$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/>";
dsnraju
Forum Newbie
Posts: 10
Joined: Sun Oct 30, 2011 3:39 am

Re: PHP Array

Post by dsnraju »

Try this code and execute it
denebx3
Forum Newbie
Posts: 6
Joined: Sun Oct 30, 2011 2:31 pm

Re: PHP Array

Post by denebx3 »

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]  
denebx3
Forum Newbie
Posts: 6
Joined: Sun Oct 30, 2011 2:31 pm

Re: PHP Array

Post by denebx3 »

dsnraju wrote:Try this code and execute it
Please see my new code above:

$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)
Post Reply