<?php
$conn = mysqli_connect("localhost", "root", "", "konkal");
$result = mysqli_query($conn,"
SELECT *
FROM tb_hitung
JOIN tb_makanan
ON tb_hitung.id = tb_makanan.id
WHERE ket_waktu = 'pagi'
");
while( $row = mysqli_fetch_assoc($result));
?>
<tr>
<th>Gambar Makanan</th>
<th>Nama Makanan</th>
<th>Jenis Makanan</th>
<th>Waktu Makan</th>
<th>Jumlah Kalori</th>
</tr>
<tr>
<td><img src="<?php echo $row['gambar'] ?>" width="100"></td>
<td><?php echo $row["nama"] ?></td>
<td><?php echo $row["kategori"] ?></td>
<td><?php echo $row["ket_waktu"] ?></td>
<td><?php echo $row["kkal"]," kkal" ?></td>
</tr>
Table tb_hitung:
id kkal ket_waktu
12 360 kkal pagi
2 810 kkal malam
24 108 kkal siang
9 286 kkal pagi
Table tb_makanan:
id name kategori keterangan gambar
2 Ampela Lauk 270 Amepela.jpg
3 Usus Lauk 473 Usus
4 Bebek Lauk 300 Bebek
I expect the output to display results where the query filters ket_waktu = 'pagi'. I am only seeing the first row of table (the headers).
PART 1
Join point tb_hitung.id = tb_makanan.id only creates one "match".
For example: tb_hitung id 12 should match to tb_makanan id 12. but tb_makanan has ZERO ROWS with id 12 (no match).
There is a single match...it is on hitung 2 and makanan 2...but the where clause filters to hitung of "pagi". So given this data, the query will have ZERO ROWS.
PART 2
As #barmar mentions...the semicolon ends your while loop without ever looping and the header row should not be within what appears to be your loop. I'll clarify all this chit-chat with one example of an appropriate loop for this purpose:
<?php
$conn = mysqli_connect("localhost", "*******", "", "*******");
// keep your query in a variable for quick debugging
$query = "SELECT * FROM tb_hitung JOIN tb_makanan ON tb_hitung.id = tb_makanan.id";
// i removed the WHERE clause since it was filtering all rows
$result = mysqli_query($conn, $query);
// print your header row OUTSIDE the loop
?>
<tr><th>Gambar</th><th>Nama</th><th>Jenis</th><th>Waktu</th><th>Jumlah</th></tr>
<?php
// now do the loop of the data...
// WHILE should open with an OPENING BRACKET (not end with a semicolon)
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><img src="<?php echo $row['gambar'] ?>" width="100"></td>
<td><?php echo $row["nama"] ?></td>
<td><?php echo $row["kategori"] ?></td>
<td><?php echo $row["ket_waktu"] ?></td>
<td><?php echo $row["kkal"]," kkal" ?></td>
</tr>
<?php
// now we must END THE WHILE LOOP
}
Related
$query = mysqli_query($conn,SELECT `date`,`name`,`surname`, COUNT(*) AS abc FROM `trial` WHERE `name`='asd' GROUP BY `date`,`name`,`surname` ORDER BY `date` DESC);
<?php while ($row = mysqli_fetch_array($query)) : ?>
<td><?php echo $row['abc']; ?></td>
<td><?php echo $row['Can I print a value returned from a different query, not abc? ']; ?></td>
<?php endwhile; ?>
Can I run a separate query for the second row instead?
Combine them into a single query that gets both counts for each surname.
<?php
$query = mysqli_query($conn,"
SELECT `date`,`surname`, SUM(name = 'asd') AS asd_count, SUM(name = 'opr') AS opr_count
FROM `trial`
WHERE `name`= IN ('asd', 'opr')
GROUP BY `date`,`surname`
ORDER BY `date` DESC");
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)): ?>
<tr>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['surname']; ?></td>
<td><?php echo $row['asd_count']; ?></td>
<td><?php echo $row['opr_count']; ?></td>
</tr>
<?php endwhile; ?>
See multiple query same table but in different columns mysql
You also need to start a new <tr> for each row returned by the query.
enter image description hereenter image description hereI have a database table having multiple cols and data in many rows. I want to get sum of all data of all the data in each row and place in a col 'Total'. However, Grand total is showing in each row and not the total of each row.
I am presenting the code as below
<?php
include 'config.php';
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$add=mysqli_query($con,"SELECT Col1, Col2, Col3, Col4,
sum(Col1 + Col2 + Col3 + Col4) AS Total, SUM(Col1), SUM(Col2),
SUM(Col3), SUM(Col4)
from DistrictData WHERE District = 'District1' ");
while($row=mysqli_fetch_array($add))
{
$Total = $row['Total'];
$SUM1=$row['SUM(Col1)'];
$SUM2=$row['SUM(Col2)'];
$SUM3=$row['SUM(Col3)'];
$SUM4=$row['SUM(Col4)'];
$Total2= $SUM1 + $SUM2+ $SUM3+ $SUM4 ;
}
}
$sql="SELECT * FROM DistrictData WHERE District = 'District1'"; $counter = 0;
$result = $con->query($sql);
?>
<body>
<table >
<tr>
<td>Sr No.</td>
<td>Deptt</td>
<td>Data Description</td>
<td>Unit</td>
<td>Year</td>
<td>Blocks in District</td>
</tr>
<tr>
<td>Total</td>
<td>Col1</td>
<td>Col2</td>
<td>Col3</td>
<td>Col4</td>
</tr>
<?php
while($rows = $result->fetch_assoc())
{
?>
<tr>
<td><?php echo ++$counter; ?></td>
<td><?php echo $rows['Data']; ?></td>
<td><?php echo $rows['Description']; ?></td>
<td><?php echo $rows['Unit']; ?></td>
<td><?php echo $rows['Year']; ?></td>
<td> <?php echo $Total ?></td>
<td><?php echo $rows['Col1']; ?></td>
<td><?php echo $rows['Col2']; ?></td>
<td><?php echo $rows['Col3']; ?></td>
<td><?php echo $rows['Col4']; ?></td>
</tr>
<?php
}
?>
<tr>
<td>Total:</td>
<td> <?php echo $Total2 ?></td>
<td> <?php echo $SUM1 ?></td>
<td> <?php echo $SUM2 ?></td>
<td> <?php echo $SUM3 ?></td>
<td> <?php echo $SUM4 ?></td>
</tr>
</table>
<?php
$con->close();
?>
Output of the code
Please suggest any changes in the code so that I could get the sum for each row in Total Column.
Thanks in advance
Use this query for row sum of column elements
SELECT Col1+Col2+Col3+Col4 as I_want_this_figure from DistrictData
Note that a normalised environment would typically look more like this...
sr_no district_block val
1 1 5
1 2 6
1 3 5
1 4 8
2 1 226
2 2 112
2 3 4
2 4 5
3 1 2
3 2 7
3 3 3
3 4 8
... where (sr_no,district_block) are components of a PRIMARY KEY
Use Allies after every Sum() as something.
You will get last result when you take away total from while loop.
do your first Query under 2nd Query and pass the ID as parameter.
while($row = mysqli_fetch_array($query)){
$total_query = mysqli_query($con,"SELECT Col1, Col2, Col3, Col4,
SUM(Col1 + Col2 + Col3 + Col4) as Total from DistrictData WHERE id='row[yourID]'");
$total_result = mysqli_fetch_array($total_query);
NOW you can populate your data here
}
Hope this will help you.
i have a table with id, name, address, sector, financiar, link
on the link i when i press it i want to show me 2 tables from the id of row selected, ex: id 1.
http://postimg.org/image/khelg1m0z/
and the result: http://s28.postimg.org/srvcwj065/Capture2.jpg
now it's a static page with search clause where by id 1, but i need an automatically link show by id on each row.
<?php
include "connect.php";
$sql = "select * from studenti where id='1'";
$query = mysql_query($sql) or die (mysql_error());
?>
<table width="70%" cellpadding="5" cellspace="5">
<tr><td>Id</td>
<td>Nume</td>
<td>Localitate</td>
<td>Judet</td>
<td>Sector Financiar</td>
<td>Link</td></tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['localitate']; ?></td>
<td><?php echo $row['judet']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<td><?php echo $row['link']; ?></td>
<?php } ?>
</table>
<?php
include "connect.php";
$sql1 = "select * from certificari where id='1' ";
$query = mysql_query($sql1) or die (mysql_error());
?>
<table width="70%" cellpadding="5" cellspace="5">
<tr><td>Id</td>
<td>Denumire certificare</td>
<td>Serie si numar certificare</td>
<td>Data certificarii</td>
<td>Valabilitate certificare</td>
<td>Sector Financiar</td></tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['serie_numar']; ?></td>
<td><?php echo $row['data']; ?></td>
<td><?php echo $row['valabilitate']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<?php } ?>
</table>
You should not use the mysql_ functions since they are depricated and are very vulnerable to SQL injection attacks. Instead, I will use MySQLi in this answer, but you could also use PDO if you want to.
To display records for different ID's based on what the user selects you can pass an ID in the page URL. When you link to the page you add the desired ID to the address like this:
Link
The value of the variable id will now be available in page.php as $_GET['id'].
The actual PHP in your page would then look something like the code below. I have left out some of your HTML for brevity, but you can just add it int.
//Connect to the DB. Might want to put this in your connect.php and include it.
$db = new mysqli('localhost', 'user', 'pass', 'db');
//Prepare the statement. The ? will be your ID.
$statment = $db->prepare("SELECT * FROM studenti WHERE id = ?");
//Bind the parameters, so that the first (and only) question mark is turned into your ID.
//The 'i' means integer, if you store the id as a string your should use 's' instead.
$statement->bind_param('i', $_GET['id']);
//Execute the query.
$statement->execute();
//Get the results.
$result = $statement->get_result();
//Iterate over it to create the output.
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['localitate']; ?></td>
<td><?php echo $row['judet']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<td><?php echo $row['link']; ?></td>
</tr>
<?
}
//Then do the same thing for the table certificari.
//Note that you only need to connect to the DB once.
This beginners guide to MySQLi is very helpful if you need some more guidance.
Problems:
1) Just 3 id_penjualan rows were displayed in order history, it should be 4 id_penjualan rows.
2) jumlah and total have same value, it should be different for each other depending on sum of product quantity of each transaction.
Table tb_penjualan:
Table: tb_detail_penjualan:
Output:
Code:
<?php
$id_pelanggan=$_SESSION['user_session'];
$query="SELECT tb_penjualan.*, tb_pelanggan.nama_dpn, tb_pelanggan.nama_blk
FROM tb_penjualan LEFT JOIN tb_pelanggan
ON tb_penjualan.id_pelanggan=tb_pelanggan.id_pelanggan
WHERE tb_penjualan.id_pelanggan=:id_pelanggan
AND tb_penjualan.checkout='yes' ORDER BY id_penjualan";
$stmt=$con->prepare($query);
$stmt->execute(array(":id_pelanggan"=>$id_pelanggan));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
$id_penjualan=$row['id_penjualan'];
if($stmt->rowCount()>0)
{
$query2="SELECT SUM(subtotal) AS total, SUM(jumlah) as total_jml
FROM tb_detail_penjualan
WHERE id_penjualan=:id_penjualan";
$stmt2=$con->prepare($query2);
$stmt2->execute(array(":id_penjualan"=>$id_penjualan));
$stmt2->execute();
$row2=$stmt2->fetch(PDO::FETCH_ASSOC);
$no=1;
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{?>
<tr>
<td></td>
<td><?php echo $no++;?></td>
<td> <?php echo $row['id_penjualan'] ;?></td>
<td><?php echo $row['nama_dpn'] ;?> <?php echo $row['nama_blk'] ;?> </td>
<td><?php echo date('d F y', strtotime($row['tanggal_blj'])); ?></td>
<td><?php echo $row2['total_jml']; ?></td>
<td> <?php $harga = $row2['total'] ?> <?php echo "Rp. " .number_format($harga, 0, ',', '.');?></td>
<td>Rp. </td>
<td></td>
</tr>
<?php
}
} else { ?>
<p>Order history empty</p>
<?php }?>
</tbody>
You have 2 queries in your code:
$query executed with $stmt
$query2 executed with $stmt2 and you fetch first row in $row2.
Then you have a loop, where you are fetching all rows from $stmt via variable $row. So $row contains each time values from next row of the first query.
But inside the loop you are also outputting information from $row2, which doesn't change (it is still the first row fetched before the loop), so each time it prints the same output.
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 }?>