I want to recapitulate report that grouped by month. But the results of sum is incorrect. It's just sum data in one month and show in all rows.
<?php
$query = mysql_query("SELECT * FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php
$Masuk= mysql_query("SELECT SUM(barang_in) AS masuk FROM barang GROUP BY arsip");
if($Masuk){
$data = mysql_fetch_assoc($Masuk);
echo $data['masuk'];
}
?></td>
<td><?php
$keluar= mysql_query("SELECT SUM(bijih_out+htm_out+pth_out) AS keluar FROM barang GROUP BY arsip");
if($keluar){
$data = mysql_fetch_assoc($keluar);
echo $data['keluar'];
}?></td>
<td><?php
$efisiensi= mysql_query("SELECT SUM((bijih_out+htm_out+pth_out)-barang_in) AS efisiensi FROM barang GROUP BY arsip");
if($efisiensi){
$data = mysql_fetch_assoc($efisiensi);
echo $data['efisiensi'];
}
?></td>
<td><?php
$persen= mysql_query("SELECT SUM(barang_in/(bijih_out+htm_out+pth_out))*1 AS persen FROM barang GROUP BY arsip");
$simbol = "%";
if($persen){
$data = mysql_fetch_assoc($persen);
echo number_format($data['persen'],2); echo $simbol;
}
?></td>
<?php
$i++;
}
?>
so that's my code. what's wrong with that? There are some columns for displaying income items, outcome items, efficiency ( difference of outcome and income items ) and percent of efficiency.
finally I found out the right code. Thankyou for #RandomSeed who helps me. This is my final code and it works!
<?php
$query = mysql_query("SELECT arsip, SUM(barang_in) AS income, SUM(bijih_out+htm_out+pth_out) AS outcome, SUM((bijih_out+htm_out+pth_out
)-barang_in) AS efficiency, SUM((barang_in / ( bijih_out + htm_out + pth_out))*100) AS percent FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php echo $data['income']; ?></td>
<td><?php echo $data['outcome']; ?></td>
<td><?php echo $data['efficiency']; ?></td>
<td><?php $simbol = "%"; echo number_format($data['percent'],0); echo $simbol; ?></td>
<?php
$i++;
}
?>
Related
I have this working.
$queryPR = "SELECT * FROM `raport` WHERE DATE(`timestamp`) = CURDATE() AND (lokacioni = 'PR')";
I need the total amount of rows calculated for specific date.
I need also to the SUM('pagesat') somehow but I have no idea how.
Find the whole code below.
I am trying to build something simple to show the income and outcome for 3 locations that we have.
<table class="responsive-table">
<thead>
<tr>
<th>Vetura</th>
<th>Targa</th>
<th>Dite</th>
<th>Pagesa</th>
<th>Cmimi Ditore</th>
<th>Vazhdim</th>
<th>Te Tjera</th>
<th>Shpenzime</th>
<th>Komente</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM `raport` WHERE DATE(`timestamp`) = CURDATE() AND (lokacioni = 'FR')";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td><?php echo $row['vetura']; ?></td>
<td><?php echo $row['targa']; ?></td>
<td><?php echo $row['dite']; ?></td>
<td><?php echo $row['pagesa']; ?>€</td>
<td><?php echo $row['cmimiditore']; ?>€</td>
<td><?php echo $row['vazhdim']; ?>€</td>
<td><?php echo $row['tetjera']; ?>€</td>
<td><?php echo $row['shpenzime']; ?>€</td>
<td><?php echo $row['koment']; ?></td>
</tr>
<?php
$pagesa = $row['pagesa'];
$tetjera = $row['tetjera'];
$vazhdime = $row['vazhdim'];
$shpenzime = $row['shpenzime'];
}?>
</tbody>
</table>
<?php
$totali = ($pagesa + $vazhdime + $tetjera);
$gtotal = ($totali - $shpenzime);
?>
<div class="card grey">
<hgroup class="totalDitorRaport">
<h2>Total</h2>
<h1><?php echo $gtotal; ?>€</h1>
</hgroup>
</div>
try this
get the sum of the row pagesa in the loop
$queryPR = "SELECT * FROM `raport` WHERE DATE(`timestamp`) = CURDATE() AND (lokacioni = 'PR')";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
$sum = 0;
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
$sum += $row['pagesa'];
}
echo $sum;
there are multiple column in my table ..
col1 col2 col3 price
500 700 100 10
501 700 100 20
502 700 100 30
503 700 100 10
4 70
I need to get count of col1 and display its sum.
and also display sum of price column...
But the main thing is i need to display this in last row....after all data...how can select this and echo in last row...
plz help me...
I need to echo exactly as i put the data in above table...
I need sql query and also need help to echo only sum of this two column in last row only......
SELECT *,IFNULL(col1,'SUM'), count(*) as count FROM coupon_entry WHERE Group By col1 WITH ROLLUP
<?php if (is_array($data)) { foreach($data as $row){ ?>
<tr>
<td><?php echo htmlspecialchars($row['col1']); ?></td>
<td><?php echo htmlspecialchars($row['col2']); ?></td>
<td><?php echo htmlspecialchars($row['col3']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
</tr>
<?php } }?>
One solution is to calculate the sum in PHP with variables :
<?php if (is_array($data)) {
$totalCol1 = 0;
$totalPrice = 0;
foreach($data as $row){
$totalCol1 += $row['col1'];
$totalPrice += $row['price'];
?>
<tr>
<td><?php echo htmlspecialchars($row['col1']); ?></td>
<td><?php echo htmlspecialchars($row['col2']); ?></td>
<td><?php echo htmlspecialchars($row['col3']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
</tr>
<?php }
<tr>
<td><?php echo $totalCol1;?></td>
<td></td>
<td></td>
<td><?php echo $totalPrice;?></td>
</tr>
}?>
Either you have to do 2 separate queries or else you have to do your calculations in PHP - either one is fairly simple although the PHP solution will probably be slightly (negligibly?) faster.
double-query:
$r = query('SELECT *
FROM yada-yada');
while ($row = fetch($r)) {
echo "<td>$row[col1]</td>\n";
echo "<td>$row[col2]</td>\n";
echo "<td>$row[col3]</td>\n";
echo "<td>$row[price]</td>\n";
}
$r2 = query('SELECT COUNT(*) as cnt, sum(col1) as sum_col1,
sum(price) as sum_price
FROM yada-yada...');
if ($row = fetch($r2)) {
echo "<td>$row['cnt']</td><td>$row['sum_col1']</td>...$row['sum_price']...\n";
}
calculate in PHP:
$cnt = $sum_col1 = $sum_price = 0;
$r = query('SELECT *
FROM yada-yada');
while ($row = fetch($r)) {
echo "<td>$row[col1]</td>\n";
echo "<td>$row[col2]</td>\n";
echo "<td>$row[col3]</td>\n";
echo "<td>$row[price]</td>\n";
$cnt++;
$sum_col1 += $row['col1'];
$sum_price += $row['price'];
}
echo "<td>$cnt</td><td>$sum_col1</td>...$sum_price...\n";
Below is my sql query to get count and sum of column.
SELECT COUNT(coupon) As Total,SUM(Price) AS TotalPrice FROM coupon_entry
And put this in seprate row below table...
<?php
foreach($tot as $tota)
?>
<tr style="background-color:#33CCFF;">
<td colspan="2" style="text-align:right; font-weight:bold;">Total Coupon</td>
<td><?php echo $tota['Total'];?></td>
<td style="text-align:right; font-weight:bold;">Total Amount in Rs</td>
<td><?php echo $tota['TotalPrice'];?></td>
</tr>
<?php }?>
This is my query:
$rs = pg_query("select sj.*, SUM(p.paid_amount) as Total, sj.subject_fee - SUM(p.paid_amount) as Balance
from tbl_subject as sj, tbl_payment_details as p, tbl_student as s, tbl_student_block as sb
where p.student_block_id = sb.student_block_id and s.student_id = sb.student_id and sj.subject_id = sb.subject_id and
sb.student_id = ".$_POST['student']." group by sj.subject_id order by sj.subject_id;");
displayRecordset($rs);
The problem is when I echo $r['Total'], it doesn't show column Total in my query.
This is my function:
$counter = 1;
while ($r = pg_fetch_array($rs, NULL, PGSQL_ASSOC))
{
$total = $r['Total'];
?>
<tr>
<td><?php echo $counter; ?></td>
<td><?php echo $r['subject_id']; ?></td>
<td><?php echo $r['subject_fee']; ?></td>
<td><?php echo $total; ?></td>
<td><?php echo $r['Balance']."Balance"; ?></td>
<td>
Edit
</td>
</tr>
<?php
$counter++;
}
After submitting a mysqli_query to select open/unfulfilled orders from a cart database the results is output to the screen with a standard WHILE LOOP.
while($row = mysqli_fetch_array($oresult)) { include('orders-open.php');}
orders-open.php is simply the internal part of a TABLE:
<tr style="color:#FFF;">
<td><?php echo $row['Buyer']; ?></td>
<td><?php echo $row['Cart_Date']; ?></td>
<td><?php echo $row['Item_Number']; ?></td>
<td><?php echo $row['Item_Title']; ?></td>
<td><?php echo $row['Item_FPrice']; ?></td>
<td><?php echo $row['Item_Qty']; ?></td>
</tr>
So here is my question, I want to apply a simple HTML "<HR>" tag between the records at the point where the value in $row['Buyer'] changes, so by example:
John Doe 9/11/13 123456 Item 1 $5.99 5
John Doe 9/11/13 123654 Item 2 $8.99 3
John Doe 9/9/13 321456 Item 3 $4.99 2
(HR - Horizontal Rule Tag here)
Mike Doe 9/7/13 123555 Item 1 $9.99 2
Mike Doe 9/7/13 123777 Item 2 $2.99 6
What would be the best way to write the conditional statement inside the WHILE LOOP to compare the $row[Buyer'] result to the previous $row['Buyer'] result?
$first_run = TRUE;
$previous_buyer = NULL;
while($row = mysqli_fetch_array($oresult)) {
if($first_run) {
$first_run = FALSE;
$previous_buyer = $row['Buyer'];
}
include('orders-open.php');
}
And then in your include file:
<?php if($previous_buyer != $row['Buyer']) {
echo '<HR width="100%">';
$previous_buyer = $row['Buyer'];
} ?>
<tr style="color:#FFF;">
<td><?php echo $row['Buyer']; ?></td>
<td><?php echo $row['Cart_Date']; ?></td>
<td><?php echo $row['Item_Number']; ?></td>
<td><?php echo $row['Item_Title']; ?></td>
<td><?php echo $row['Item_FPrice']; ?></td>
<td><?php echo $row['Item_Qty']; ?></td>
</tr>
$prev_buyer = '';
while($row = mysqli_fetch_array($oresult))
{
if($prev_buyer !== $row['Buyer'])
{
//Do Something
}
include('orders-open.php');
$prev_buyer = $row['Buyer']
}
Just as Elon said above - change this:
while($row = mysqli_fetch_array($oresult)) { include('orders-open.php');}
to
$old_buyer = null;
while($row = mysqli_fetch_array($oresult)) {
if ($row['Buyer'] != $old_buyer) {
echo '<hr>';
}
$old_buyer = $row['Buyer'];
include('orders-open.php');
}
You could do it like this:
$buyer = null ; //Cache buyer in a variable, because $row is reset in the loop.
while($row = mysqli_fetch_array($oresult)) {
include('orders-open.php');
if ($buyer !== $row["Buyer"]){
echo ($buyer !== null) ? "<hr/>" : "" ;
$buyer = $row["Buyer"] ;
}
}
<?php foreach((array)$query as $row):?>
<td><?php echo $row->date ?></td>
<td><?php echo $row->cost ?></td>
<?php endforeach ;?>
I want to sum all values in $row->cost when $row->date=02-01-2016, like this in a table
Thanks.
echo array_sum(array_filter(array_map(function($row){
if($row->date == '02-01-2016')
return $row->cost;
}, $array)));
You can use
array_map: to compare and skip the values for which cost is not needed to add in sum.
array_filter: and then to remove the blank indexes
array_sum: and finally to sum cost
Note: if you are getting results from database, you can simply do sum there (see below):
$db->query("SELECT SUM(costs) FROM table_name WHERE `date`='2016-01-02'");
echo $db->fetchColumn();
Try below code:
<?php
$total = 0;
foreach((array)$query as $row):
$date = $row->date;
$cost = $row->cost;
if(date("M-d-Y", strtotime($date))==date("M-d-Y")) {
$total = $total+$cost;
}
?>
<td><?php echo $date ?></td>
<td><?php echo $cost ?></td>
<?php endforeach; ?>
<td colspan="2"><?php echo $total; ?></td>
<?php
$tot = 0;
foreach( (array)$query as $row ) {
if( $row->date=='02-01-2016' ) $tot = $tot+$row->cost;
?>
<tr>
<td><?php echo $row->date ?></td>
<td><?php echo $row->cost ?></td>
</tr>
<?php } ?>
<tr><td colspan="2"><?php echo $tot; ?></td></tr>
obviously, the comparison works if $row->date is formatted as dd-mm-yyyy
<table border=1>
<?php
$tot = 0;
foreach($query as $row ) {
if( $row->date=='2016-01-02' ) $tot += $row->cost;
?>
<tr>
<td><?php echo $row->date ?></td>
<td><?php echo $row->cost ?></td>
</tr>
<?php } ?>
<tr><td></td><td><?php echo $total; ?></td></tr>
</table>
It will work for you exctly what u want.