PHP totalling column and simplifying Sessions - php

I'm trying to total a column in a table of prices and it's not working out for me. I've looked at a ton of solutions such as array_sum() but it's not displaying anything for me. This is a cart page where I'm posting items to a session and then displaying in a for loop. I'm not sure if my sessions are overly complicated or what. Here is my session and post code:
session_start();
if( $_SERVER['REQUEST_METHOD'] == 'POST') {
$_SESSION['item'][] = array($_POST['item'], $_POST['price']);
}
?>
And here is my for loop section of code:
for($i=0; $i < count($_SESSION['item']); $i++){
$prices = $_SESSION['item'][$i][1];
$sum = 0;
$sum += $_SESSION['item'][$i][1];
print $sum;
?>
<tr>
<td class="left"> <?php print_r($_SESSION['item'][$i][0]) ?></td>
<td class="center"><?php echo "$ " . $_SESSION['item'][$i][1] ?></td>
</tr>
<?php
}
?>
<tr>
<td class="center">Total:</td>
<td></td>
</tr>
<?php
}
?>
</table><br>
Any help would be greatly appreciated!

Replace your current forloop with the following:
You were assigning $sum=0; inside loop so every time loop executed, your $sum variable would be assigned to zero and hence you would lose its value.
Sidenote: You should consider printing sum outside of loop.
$sum = 0;
for($i=0; $i < count($_SESSION['item']); $i++){
$prices = $_SESSION['item'][$i][1];
$sum += $_SESSION['item'][$i][1];
print $sum;
?>

At first your code must be like this:
$sum = 0;
for($i=0; $i < count($_SESSION['item']); $i++){
$prices = $_SESSION['item'][$i][1];
$sum += $prices;
?>
<tr>
<td class="left"> <?php print_r($_SESSION['item'][$i][0]) ?></td>
<td class="center"><?php echo "$ " . $_SESSION['item'][$i][1] ?></td>
</tr>
<?php
}
?>
<tr>
<td class="center">Total: <?=$sum?></td>
<td></td>
</tr>
<?php
}
?>
</table><br>
And the second if you want to sum column of prices from database then you can use mysql SUM function
Reference for mysql SUM

Related

How can I echo only last value from the results

How can I echo only the last value from the results.
I'm using the following code but it is displaying like the example below.
<?php
$total = 0;
foreach($products as $product){
?>
<tr>
<td>
<?php
$total_item = $product['quantity'] * $product['sale_price'];
$total += $total_item;
echo formatcurrency($total);
?>
</td>
</tr>
<?php
}
?>
This is what i'm getting but i want only the last value. $526.00
enter image description here
Please output the total value outside of the foreach loop. it would be more simply.
<?php
$total = 0;
foreach($products as $product){
?>
<tr>
<td>
<?php
$total_item = $product['quantity'] * $product['sale_price'];
$total += $total_item;
?>
</td>
</tr>
<?php
}
echo formatcurrency($total);
?>
Best Regards

How to calculate price?

Why doesn't my `$total' display any value when I run the code?
Initialize out side of the while loop
<?php
$total=0;
while ($row = ...
AND compute the $total as a running total inside ?PHP tag the echo it outside the loop to display the accumulated total
$total = $total + $row['price1'] + $row['price1'];
Try this,
<?php
$total=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["bookname"]; ?> </td>
<td><?php echo $row["price1"]; ?> </td>
<td><?php echo $row["cdname"]; ?> </td>
<td><?php echo $row["price2"]; ?> </td>
<td><?php echo $row["total"]; ?> </td>
</tr>
<?php $total = $total + $row['price1'] + $row['price2']; } ?>
Remember : $row['price1'] and $row['price2'] always be an integer values.
first you may want to put your total inside a php block then echo it
Initialize $total before the while loop. And after computation echo it outside the while loop.

How do I display the output of a "while loop" in a table?

I don't even have an idea of where to start for this. I need to display 100 numbers in a table and would like to use a while loop to do so. Is there a "shortcut" to doing this?
For a table you need some tags table, tr and td. The tr and td are in while loop and the value $i will print inside the td.
<table>
<?php
$i = 1;
while($i != 101){?>
<tr><td><?php echo $i++;?></td></tr>
<?php }?>
</table>
You can use while, for, foreach for your convenience, like below code
<table>
<thead>
<tr>
<th class="header">Number</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
while($i != 101){
?>
<tr>
<td><?php echo $i; ?></td>
</tr>
<?php
$i++;
} ?>
</tbody>
</table>
You can dipslay 100 numbers simply in tbale like this:
<?php
// Using For loop
echo "<table>";
for ($i = 1;$i <= 100; $i++) { // use for loop
echo "<tr><td>".$i."</td></tr>";
}
echo "</table>";
// OR
// Using while loop
$i = 1;
echo "<table>";
while($i <= 100) {
echo "<tr><td>".$i."</td></tr>";
$i++;
}
echo "</table>";
?>
You can use a while loop:
while ($i <= 100) {
echo "<td><tr>" . $i++ . "</tr></td>";
}

How can i sum dynamic array

My code is below
<?php
foreach($query2->result() as $row2)
{
$sub_head_id = $row2->sub_head_id;
?>
<tr>
<?php
$query3 = $this->db->select('farm_id')->get('spf_farm_info'); $she_am_total = 0;
foreach($query3->result() as $row3)
{
$farm_id_inner = $row3->farm_id;
?>
<td>
<?php
$s_h_e_amount = $this->report_model->get_amount($farm_id_inner,$head_id,$sub_head_id);
?>
</td>
<?php
}
?>
<td>
<?php
$total += $s_h_e_amount;
?>
</td>
</tr>
<?php
}
}
?>
Basically i want to know how can i solve it,
<tr>
<?php
$query3 = $this->db->select('farm_id')->get('spf_farm_info'); $she_am_total = 0;
foreach($query3->result() as $row3)
{
?>
<td>
<?php
echo "Total :";
echo $total;
?>
</td>
<?php
}
?>
<td>
Grand Total:
</td>
</tr>
Here, Table column will increase dynamically, and will get different value every td(may be 2/3/4/..) from get_amount function.
Now i want to sum every tr's value according to column.
here it will be sum dynamically which will be show.
Please help me how can i solve properly
Simple SOlution is Take Four Variables
$Cola_total=0;
$Colb_total=0;
$Colc_total=0;
$Cold_total=0;
in your loop sum these Columns
foreach($array as $val){
echo "<tr><td>$val[0]</td> <td>$val[1]</td> <td>$val[2]</td><td>$val[3]</td></tr>";
$Cola_total=+$val[0];
$Colb_total=+$val[1];
$Colc_total=+$val[2];
$Cold_total=+$val[3];
}
and after the loop put the sum in last row
echo "<tr><td>$Cola_total</td> <td>$Colb_total</td> <td>$Colc_total</td><td>$Cold_total</td></tr>";
it may give you some idea
Here , since using Codeigniter, it will be used that select_sum(). then will get proper result.

How to sum up values inside a array variable?

I have retrieved some values from database. In my view file I have the following:
$row['fee_amount'];
Now, I want to sum up all the values inside $row['fee_amount']; and then show it.
I know I could sum up when querying the database, but I am interested to learn how to add using PHP .
Would you please kindly teach me how to do it?
EDIT
<?php if(count($records) > 0) { ?>
<table id="table1" class="gtable sortable">
<thead>
<tr>
<th>S.N</th>
<th>Fee Type</th>
<th>Fee Amount</th>
</tr>
</thead>
<tbody>
<?php $i = 0; foreach ($records as $row){ $i++; ?>
<tr>
<td><?php echo $i; ?>.</td>
<td><?php echo $row['fee_type'];?></td>
<td><?php echo $row['fee_amount'];?></td>
</tr>
<?php } ?>
</tbody>
<tr>
<td></td>
<td>Total</td>
<td>
I WANT TO DISPLAY THE SUMMATION RESULT HERE ADDING UP VALUES INSIDE THIS>>> <? $row['fee_amount']; ?>
</td>
</tr>
</table>
<?php } ?>
In your view file, with your foreach loop, add a $sum variable next to your $i counter and add the amount per each iteration (similar to like you increase $i):
<?php
$i = 0;
$sum = 0;
foreach ($records as $row)
{
$i++;
$sum += $row['fee_amount']; ?>
(I put this over multiple lines to make it more readable).
After the foreach has finished, $sum contains the total amount:
<td>Total: <?php echo $sum; ?></td>
That simple it is. You only need a new variable ($sum) and do the calculation.
Use a loop
$sum = 0;
while($row...){
$sum += $row['fee_amount']
}
echo $sum;
You could use;
$someValue = 0;
foreach($row["fee_amount"] as $value) {
$someValue = $someValue + $value;
}
using this php function, if $row['fee_amount'] is an array ^_^
for example:
$a = array(2, 4, 6, 8);
array_sum($a)

Categories