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.
Related
I have a problem for my program.
This my script:
$x=1;
$no=1;
$tot=0;
$bobot=0;
$jumlah=4;
for($i=1;$i<=$jumlah;$i++) {
<tr>
<th> echo $no++; </th>
for($a=$i;$a<=$jumlah;$a++) {
echo "<td>".$x/$a."</td>";
}
<td>Count</td>
</tr>
}
and this is my result
result page
I want to replace the words "count" with the sum of each column in the same row. and my question, how I can add all column in one rows.
example : row 1 = 1 + 0.5 + 0,33 + 0.25 = 2,08
Thank you for helping, I am sorry if my English so bad.
Try something like this
initialize $count as zero before the inner loop
and in inner loop increment county
<?php
$x=1;
$no=1;
$tot=0;
$bobot=0;
$jumlah=4;
for($i=1;$i<=$jumlah;$i++) {
?>
<tr>
<th> <?php echo $no++;?> </th>
<?php
$count=0;
for($a=$i;$a<=$jumlah;$a++) {
echo "<td>".$x/$a."</td>";
$count=$count+($x/$a);
}
?>
<td><?=$count;?></td>
</tr>
<?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
How do I sum grouped values from a table? For example I will sum the name?
this values :
2.997,
7.497
return : result!
I already have a function which delivers the whole sum this works fine. But I have don't a clue how to solve this problem ![enter image description here][1]
here is my function for the whole sum!
function calculateSum() {
var sum = 0;
$(".overviewTaxes").each(function() {
var value = $(this).text();
// add only if the value is number
if(!isNaN(value) && value.length != 0) {
sum += parseFloat(value);
}
});
$( ".globalTaxSum" ).html(sum);
}
<tbody>
<?php foreach ($overviews as $overview): ?>
<tr>
<td >
<?php echo $overview["forename"] . " " . $overview["surname"]; ?>
</td>
<td>
<?php echo $overview["article"]; ?>
</td>
<td>
<?php echo $overview["articleprice"]; ?>
</td>
<td class="overviewTaxes">
<?php
$ps = $overview["articleprice"] * 0.3; //flat rate
echo round($ps, 3) ;
?><br/>
</td>
</tr>
<?php endforeach; ?>
</tbody>
Thanks for help.
Best regards
Marc Kevin
I will be generating a HTML table with data pulled from MySQL.The number of rows in my MySQL table are not fixed.
<?php
while($row=mysql_fetch_assoc($result))
{ ?>
<tr>
<td><?php echo $row['col1'];?></td>
<td><?php echo $row['col2'];?></td>
</tr>
<?php } ?>
Now how do I have the table rows and table data elements assigned unique id ??
Another loop to generate them won't work as I can't set an exit condition for the new loop as number of rows are not fixed.
Please guide me as to how to go forward about it. I can only use Javascript and not JQUERY.
Why can't you do something like this ?
<?php
$i = 1;
while($row=mysql_fetch_assoc($result))
{ ?>
<tr id="row<?php echo $i;?>">
<td id="cell-left-<?php echo $i;?>"><?php echo $row['col1'];?></td>
<td id="cell-right-<?php echo $i;?>"><?php echo $row['col2'];?></td>
</tr>
<?php
$i++;
} ?>
Please note, I have added ids row, cell-left- and cell-right- by myself. You may change them as per your requirements.
You can use a counter when iterating through the rows, maybe something like this:
<?php
$rowCount = 0;
while($row=mysql_fetch_assoc($result))
{
$rowCount++;
?>
<tr id="<?php echo 'row' . $rowCount;?>">
<td><?php echo $row['col1'];?></td>
<td><?php echo $row['col2'];?></td>
</tr>
<?php
}
?>
You can now select an element with
var rowID = 1;
document.getElementById("row" + rowID);
Hope this helps.
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)