Calculate total amount of rows for specific date - php

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;

Related

Rearrange table head data to match with table body

I want to arrange the table head with my table body, so the data fill have a same type, how can i doing it??
The black square is the correct one
This is my code
<table>
$syntax = 'SELECT customer,type, count(type) as num FROM view_detail GROUP BY type ORDER BY customer';
$rows = $config->prepare($syntax);
$rows->execute();
$result = $rows->fetchAll(PDO::FETCH_OBJ);
?>
</tr>
<td>No</td>
<td>Customer</td>
<?php for($x=0;$x<count($result);$x++){ ?>
<td><?php echo $result[$x]->type; ?></td>
<?php } ?>
<td>Total</td>
<?php $no = 1;
$query = 'SELECT customer,type, count(type) as tipe FROM view_detail GROUP BY customer ORDER BY customer';
$baris = $config->prepare($query);
$baris->execute();
$hasil1 = $baris->fetchAll(PDO::FETCH_OBJ);
for($i=0;$i<count($hasil1);$i++){
$sql = 'SELECT * FROM view_detail WHERE customer = "'.$hasil1[$i]->customer.'" ORDER BY customer';
$row = $config->prepare($sql);
$row->execute();
$hasil = $row->fetchAll(PDO::FETCH_OBJ);
?>
<tr>
<td><?php echo $no; $no++;?></td>
<td><?php echo $hasil[$i]->customer; ?></td>
<?php
for($y=0;$y<count($result);$y++){
if($y < count($hasil)){
?>
<td><?php echo $hasil[$y]->type; ?></td>
<?php }else{ ?>
<td>0</td>
<?php } } }?>
</tr>
</table>
Thanks
I want to make it like this
Edit:
Dynatrade -> MFN200, N50Z, and N70
Super Bloom -> N70, MFN100, MFN120, and MFN150

Query works in phpMyAdmin but not PHP

In phpMyAdmin I have a simple query:
SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1
BUT when I try to do this in my clear_recent.php:
<?php $result = $conn->query("SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1"); ?>
<?php foreach ($result->fetch_assoc() as $row): ?>
<?php while($row = $result->fetch_assoc()) { ?>
<tr>
<td><?php echo($title = $row["id"]); ?></td>
<td><?php echo($title = $row["pid"]); ?></td>
<td><?php echo($title = $row["user_id"]); ?></td>
<td><?php echo($title = $row["timestamp"]); ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
I get an error:
Invalid argument supplied for foreach() in /database/chron/clear_recent.php
Cannot for the life of my figure out what's wrong!!!! Please help!
You should check if the query returns any row. try the following code.
<?php
$result = $conn->query("SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1");
$rows = $result->fetch_assoc();
//check if some rows available.
if (count($rows) > 0) {
foreach ($rows as $row) {
while ($row = $rows) {
?>
<tr>
<td><?php echo($title = $row["id"]); ?></td>
<td><?php echo($title = $row["pid"]); ?></td>
<td><?php echo($title = $row["user_id"]); ?></td>
<td><?php echo($title = $row["timestamp"]); ?></td>
</tr>
<?php
}
}
}
?>
additional question: why you use while loop under the foreach loop? is it right?
You are getting this error because no rows return from this query.

Cannot echo the column SUM(column_name) as Total

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++;
}

How to showing report that group by archieve with PHP & MySQL?

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++;
}
?>

Issue with show all users in the table with pdo

I have the next issue, when I need the code show me all the users in the table always show me one data the first one or the last one if I change the ASC to DESC inside of SELECT..
I need to show me all users... can you please help me with this?
Here the code and the table with the row I need to show:
<?
include '../include/config.php';
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
foreach ($conn->query($sql) as $row) {
$id_paciente = $row['id_paciente'];
$id_tipo = $row['id_tipo'];
$nombre = $row['nombre'];
$apellido = $row['apellido'];
$ciudad = $row['ciudad'];
$telefono = $row['telefono'];
$foto = $row['foto'];
}
?>
<tr>
<th><?php echo $id_paciente; ?></th>
<td><img src="../<?php echo $foto;?>" class="image_thumbnail" /></td>
<td><?php echo $nombre; ?></td>
<td><?php echo $apellido; ?></td>
<td><?php echo $id_tipo; ?></td>
<td><?php echo $ciudad; ?></td>
<td><?php echo $telefono; ?></td>
You are echoing your variables outside of the loop.
So, move it inside:
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
foreach ($conn->query($sql) as $row) {
?>
<tr>
<th><?php echo $row['id_paciente'] ?></th>
<td><img src="../<?php echo $row['foto']?>" class="image_thumbnail" /></td>
<td><?php echo $row['nombre'] ?></td>
<td><?php echo $row['apellido'] ?></td>
<td><?php echo $row['id_tipo'] ?></td>
<td><?php echo $row['ciudad'] ?></td>
<td><?php echo $row['telefono'] ?></td>
<tr>
<? } ?>
well I get my answer with my problem...
now I see all the users, the code neccesary is:
<?
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
?>
for somebody want to
Best Regards!

Categories