I have data like this:
ID
Parts Number
Parts Name
Std Qty
Stock
1
9810001
NGK-11O7GGH
12
10
1
9810010
NGK-1187GGH
12
10
2
3810001
OVH-78GH
14
16
2
4810010
OVH-86GS
14
16
So, each ID can have many parts variant, but user only set the stock for each ID not each variant.
Then, the table should be look like this:
Table with rowspan in excel
How i can dynamically make this table with PHP and MySQL?
So i can export to excel correctly.
My Code:
<?php
$pit = 10;
if($pit<=6){
$std = 'usix';
}elseif($pit>=7 || $pit<=10){
$std = 'sevten';
}if($pit>10){
$std = 'tenplus';
}
$inv = mysqli_query($conn, "SELECT i.*, s.$std AS std FROM inventory i, sparepart s WHERE id_dealer='1' AND s.id_parts=i.id_parts");
?>
<table class="table table-bordered">
<thead>
<th>No</th>
<th>Part Number</th>
<th>Deskripsi</th>
<th>Std Qty</th>
<th>Stock</th>
<th>Status</th>
</thead>
<tbody>
<?php
$i = 1;
while($d=mysqli_fetch_array($inv)){
$id = $d['id_parts'];
$varian = mysqli_query($conn, "SELECT * FROM varian WHERE id_parts='$id'");
$varian2 = mysqli_query($conn, "SELECT * FROM varian WHERE id_parts='$id'");
if($d['stock']>$d['std']){
$status = 'STOCK AMAN';
$class = 'text-success';
}elseif($d['stock']<$d['std']){
$status = 'STOCK LOW';
$class = 'text-danger';
}
echo "
<tr>
<td>$i</td>
<td style='padding:0'>";
while($x=mysqli_fetch_array($varian)){
echo "
<p class='mb-0 p-1'>$x[part_number]</p><hr>"
;
}
echo "</td><td style='padding:0'>";
while($y=mysqli_fetch_array($varian2)){
echo"
<p class='mb-0 p-1'>$y[nama_var]</p><hr>
";
}
echo
"</td>
<td>$d[std]</td>
<td>$d[stock]</td>
<td class='$class'>$status</td>
</tr>
";
$i++;
}
?>
</tbody>
</table>
Related
I have 3 tables like this
Orders Table
orders `order_id`,`order_date`
Orders_items table
orders_items `order_id`,`product_id`,`quantity`,`parent`
Products table
products `product_id`,`product_price`
MY Expectation
I want to make grand total in last row and dynamically, I have built my own query and work to show data from multiple tables above, but cannot display grand total as I mentioned above.
This my query
<?php
$query = $db->prepare("SELECT * FROM orders WHERE month(order_date)='".$_POST['bulan']."' AND YEAR(order_date)='".$_POST['tahun']."'");
?>
<table class="table table-hover table-bordered display" id="report" width="100%">
<thead>
<tr>
<th>Tanggal</th>
<th>Customer</th>
<th>Quantity</th>
<th>Harga</th>
<th>Total Harga</th>
</tr>
</thead>
<tbody>
<?php
$query->execute();
$row = $query->rowcount();
if($row > 0){
while($data = $query->fetch(PDO::FETCH_ASSOC)){
$oID = $data['order_id'];
$item = $db->prepare("SELECT * FROM orders_items WHERE order_id = '$oID' AND parent=3");
$item->execute();
while($i = $item->fetch(PDO::FETCH_ASSOC)){
$pID = $i['product_id'];
$product = $db->prepare("SELECT * FROM products WHERE product_id = '$pID'");
$product->execute();
while($p = $product->fetch(PDO::FETCH_ASSOC)){
//fech data
echo "<tr>
<td>{$data['order_date']}</td>
<td>{$data['order_name']}</td>
<td>{$i['quantity']}</td>
<td>Rp. ".number_format($p['product_price'])."</td>
<td>Rp. ".number_format($p['product_price']*$i['quantity'])."</td>
</tr>";
}
}
$total = 0;
$oID = $data['order_id'];
$rinci = $db->prepare("SELECT orders_items.order_id, SUM(orders_items.quantity * products.product_price ) AS grand_total
FROM orders_items JOIN products ON products.product_id=orders_items.product_id WHERE order_id = '$oID'
GROUP BY order_id");
$rinci->execute();
foreach($rinci as $r){
$total += $r['grand_total'];
?>
<tr class="bg-primary text-white">
<td class="text-center"><strong>Total</strong></td>
<td></td>
<td></td>
<td></td>
<td><strong>Rp. <?=number_format($total);?></strong></td>
</tr>
My query shows several rows as total based on order_id, if I change my query and group by another column the total is disappear. Here is my result based on query above
Here's my table with several total rows
How to make sub total only show at the end of rows and summarize all total price * quantity?
Sorry for my English.
Big thanks for someone who gives me an answer and solve my problem.
Probably this will work
while($data = $query->fetch(PDO::FETCH_ASSOC)){
$oID = $data['order_id'];
$item = $db->prepare("SELECT order_items.*, products.*, (order_items.quantity*products.product_price) as total FROM order_items LEFT JOIN products ON products.product_id = order_items.product_id WHERE order_id='$oID' AND parent=3");
$item->execute();
while($i = $item->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td><?php echo $data['order_date'];?></td>
<td><?php echo $data['order_name'];?></td>
<td><?php echo $i['quantity'];?></td>
<td>Rp. <?php echo number_format($i['product_price']);?></td>
<td>Rp. <?php echo number_format($i['product_price']*$i['quantity']);?></td>
</tr>
<?php
}
}
This is a tricky question to search, hence my post here. I have a a header that displays the sum of all the values in a column of a table that is printed below it. However the table is generated from a MYSQL table and the sum of the column values is calculated as it is generated. So somehow I have to have a variable printed but only after the table is generated and I am not sure how to pass the variables back up to the print statement so it doesn't always print out a 0
I feel like the solution is that the sum should call a script (Javascipt) that generates and prints the table returning the sum of columns to then be printed. But I am not sure how I would do this
echo "
<h3>EPL</h3>
<h5>Total Score: $total</h5>
<table class='table table-bordered'>
<tr>
<th>#</th>
<th>Name</th>
<th>Score</th>
</tr>
<tbody class='row_position'>"?>
<?php
require('db_config.php');
$tablename = $_SESSION['username'] . "_epl";
$_SESSION['tablename'] = $tablename;
$sql = "SELECT * FROM $tablename ORDER BY position_order";
$users = $mysqli->query($sql);
while($user = $users->fetch_assoc()){
$con = mysqli_connect('localhost', 'root', 'root', 'predictions');
$sql1 = "SELECT * FROM `predictions`.`".$tablename."` WHERE (CONVERT(`title` USING utf8) LIKE '%".$user['title']."%')";
$sql2 = "SELECT * FROM `predictions`.`epl` WHERE (CONVERT(`title` USING utf8) LIKE '%".$user['title']."%')";
$result = mysqli_query($con, $sql1);
$row = $result->fetch_assoc();
$position1 = $row['position_order'];
$result->close();
$result = mysqli_query($con, $sql2);
$row = $result->fetch_assoc();
$position2 = $row['position_order'];
$total += abs($position1-$position2);
?>
<tr id="<?php echo $user['id'] ?>">
<td><?php echo $user['position_order'] ?></td>
<td><?php echo $user['title'] ?></td>
<td><?php echo abs($position1-$position2); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
To explain the table further, each user has their own table with the same format username_league, so I use some php code to grab the table. Additionally I have a base case table in this case 'epl' in which I compare the tables and calculate the scores based on the absolute difference in the column 'position_order'.
You should always aim for separation of your code and presentation. And beyond that, your database code is extremely inefficient. You're re-creating an entire database object for every instance of your original query.
Just put your PHP code before the HTML, or better yet in a separate file.
<?php
$total = 0;
require('db_config.php');
$tablename = $_SESSION['username'] . "_epl";
$_SESSION['tablename'] = $tablename;
$sql = "SELECT id, position_order, title FROM `$tablename` ORDER BY position_order";
$users_result = $mysqli->query($sql);
while($user = $users_result->fetch_assoc()) {
$users[] = $user;
}
foreach ($users as $user) {
$sql1 = "
SELECT position_order FROM `$tablename` WHERE CONVERT(`title` USING utf8) LIKE '%$user[title]%' LIMIT 1
UNION
SELECT position_order FROM `epl` WHERE CONVERT(`title` USING utf8) LIKE '%$user[title]%' LIMIT 1
";
$result = $mysqli->query($sql1);
$row = $result->fetch_assoc();
$position1 = $row['position_order'];
$user["position1"] = $position1;
$row = $result->fetch_assoc();
$position2 = $row['position_order'];
$user["position2"] = $position2;
$total += abs($position1 - $position2);
}
?>
There you are; using a single database object, one third fewer queries, all your values in an array. They're ready to use later in the file, or in your separate HTML view:
<h3>EPL</h3>
<h5>Total Score: <?=$total?></h5>
<table class='table table-bordered'>
<tr>
<th>#</th>
<th>Name</th>
<th>Score</th>
</tr>
<tbody class='row_position'>
<?php foreach($users as $user):?>
<tr id="<?=$user['id'] ?>">
<td><?=$user['position_order'] ?></td>
<td><?=$user['title'] ?></td>
<td><?=abs($user["position1"]-$user["position2"]); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I don't know enough about the structure here, but I'd be very surprised if you couldn't make this a single database query.
Output buffering is your friend! Simply call ob_start before outputting the table i.e.
ob_start();
echo "
<table class='table table-bordered'>
<tr>
<th>#</th>
<th>Name</th>
<th>Score</th>
</tr>
<tbody class='row_position'>"?>
...
then once you have done generating the table, you can collect the output of the table's content using ob_get_clean, output the header and then the table content:
...
$table = ob_get_clean();
echo "<h3>EPL</h3>
<h5>Total Score: $total</h5>";
echo $table;
Question 1: Is it possible to feed 1st while loop's output result as 2nd while loop's variable for query?
I have mySQL table "reorder_in_process" below:
(`id`, `item`, `to_order`, `cat_no`, `supplier`) VALUES
(48, 'Petri Dish', 62, 1006, 'Progressive'),
(47, 'Beaker', 46, 1005, 'Progressive'),
(46, 'Tissue', 17, 1008, 'Needpoint'),
(45, 'Pipet', 77, 1004, 'Kumpulan');
And my php below:
<?PHP
include ('db.php');
$sql4 = "SELECT DISTINCT supplier FROM reorder_in_process";
$result4 = mysqli_query($con,$sql4);
$supplier4 = $row4['supplier'];
$i=0;
while($row4 = mysqli_fetch_array($result4)){
echo "<h4>".$row4['supplier']."</h4>";
echo "<div><table><tr>
<th>No</th>
<th>Item</th>
<th>Cat. No</th>
<th>Buy QTY</th>
<th>Supplier</th>
</tr>";
$sql5 = "SELECT * FROM reorder_in_process WHERE supplier=$supplier4";
$result5 = mysqli_query($con,$sql5);
while($row5 = mysqli_fetch_array($result5)) {
$i++;
echo "<tr>
<td>".$i. "</td>
<td>".$row5['item']."</td>
<td>".$row5['cat_no']."</td>
<td>".$row5['to_order']."</td>
<td>".$row5['supplier']."</td>
</tr>";
}
echo "</table></div>";
}
mysqli_close($con);
?>
I doesn't work...I want the output result like below:
Progressive
No Item Cat. No Buy QTY Supplier
1 Petri Dish 1006 62 Progressive
2 Beaker 1005 46 Progressive
Needpoint
No Item Cat. No Buy QTY Supplier
1 Tissue 1008 17 Needpoint
Kumpulan
No Item Cat. No Buy QTY Supplier
1 Pipet 1004 77 Kumpulan
Pls help to find my mistake and solution to it. Thanks.
You don't need to run two queries like that.
Instead of performing one query, just to query the same table again inside the loop, modify your structure such that you just query the table once. Just ORDER BY supplier and check if the current rows supplier differs from the last rows supplier. (Disclosure: I haven't fully tested this logic, it might need some tweaking).
$result = mysqli_query($con, "SELECT * FROM reorder_in_process ORDER BY supplier");
$first_iteration = true;
$current_supplier = null;
while($row = mysqli_fetch_assoc($result)){
// If a new supplier is encountered, close the first table (if its no the first iteration) and add new header + start new table
if ($row['supplier'] != $current_supplier) {
if ($first_iteration == false)
echo "</table></div>";
$i = 1;
$first_iteration = false;
$current_supplier = $row['supplier'];
echo "<h4>".$row['supplier']."</h4>";
echo "<div><table><tr>
<th>No</th>
<th>Item</th>
<th>Cat. No</th>
<th>Buy QTY</th>
<th>Supplier</th>
</tr>";
}
echo "<tr>
<td>".$i. "</td>
<td>".$row['item']."</td>
<td>".$row['cat_no']."</td>
<td>".$row['to_order']."</td>
<td>".$row['supplier']."</td>
</tr>";
$i++;
}
echo "</table></div>";
First of all the below line is undefined because it's outside of the while loop
$supplier4 = $row4['supplier'];
Push the above code into inside first while loop and string should be enclosed by single quotes like this
"...supplier='$supplier4'"
you need to reset the $i=1 for each new supplier
Update 1:
<?PHP
include ('db.php');
$sql4 = "SELECT DISTINCT supplier FROM reorder_in_process";
$result4 = mysqli_query($con,$sql4);
while($row4 = mysqli_fetch_array($result4)){
$i=1;
$supplier4 = $row4['supplier'];
echo "<h4>".$row4['supplier']."</h4>";
echo "<div><table><tr>
<th>No</th>
<th>Item</th>
<th>Cat. No</th>
<th>Buy QTY</th>
<th>Supplier</th>
</tr>";
$sql5 = "SELECT * FROM reorder_in_process WHERE supplier='$supplier4'";
$result5 = mysqli_query($con,$sql5);
while($row5 = mysqli_fetch_array($result5)) {
echo "<tr>
<td>".$i. "</td>
<td>".$row5['item']."</td>
<td>".$row5['cat_no']."</td>
<td>".$row5['to_order']."</td>
<td>".$row5['supplier']."</td>
</tr>";
$i++;
}
echo "</table></div>";
}
mysqli_close($con);
?>
I've done something similar to this table below quite a few times, where I use a while statement to populate the <td> section of a table, but I've never done it before where part of the <td> is populated with a foreach statement, and it's confusing me.
In this example, I create a table, then I populate the first column with a list of suppliers, based on the number of tables inside of my database. (each supplier has its own table).
$supplierList = array();
$showTable = "SHOW TABLES from dbOne";
$getSuppliers = mysqli_query($con, $showTable);
while ($row = mysqli_fetch_row($getSuppliers)) {
$supplierList[] = $row;
}
$supplierList = array_reduce($supplierList, 'array_merge', array());
Now I have my array that contains the list of all my suppliers, and I use that to generate the <td>s in my table.
<table>
<thead>
<th style="text-align: center;">Supplier</th>
<th style="text-align: center;">Earliest line</th>
<th style="text-align: center;">Latest line</th>
<th style="text-align: center;"># of total lines</th>
</thead>
<?php
foreach ($supplierList as $subList) {
$supplierName = $subList;
$earlyExp = "SELECT date FROM $subList ORDER BY date DESC LIMIT 1" ;
$earlyExpQuery = mysqli_query($con, $earlyExp);
$lateExp = "SELECT date FROM $subList ORDER BY date ASC LIMIT 1" ;
$lateExpQuery = mysqli_query($con, $lateExp);
$countLines = "SELECT * from $subList";
$countLinesQuery = mysqli_query($con, $countLines);
$countLinesCount = mysqli_num_rows($countLinesQuery);
while ($row = mysqli_fetch_array($earlyExpQuery)) {
$earlyExpDate = $row['date'];
?>
<tbody>
<td><img src = "/img/suppliers/<?= $supplierName ?>.png"></td>
<td style="text-align: center;"><?= $earlyExpDate ?></td>
<td style="text-align: center;"><?= $lateExpDate ?></td>
<td style="text-align: center;"><?= $countLinesCount ?></td>
</tbody>
<?php
}
}
?>
</table>
The table itself builds correctly, and displays each supplier in a unique row. I cannot figure out though how to populate the other parts of the row with the information based off the unique supplier in the foreach statement.
Okay, I sorted this out. In case it helps anyone else, here's how I did it:
This code goes in the same spot as my post above - so in between the header and the body of the table. I did not need to use any <tr>.
<?php
foreach ($supplierList as $subList) {
$supplierName = $subList;
$earlyExp = "SELECT * FROM $subList where dateOne != '' UNION SELECT * from $subList where dateTwo != '' ORDER BY dateTwo, dateOne ASC LIMIT 1" ;
$earlyExpQuery = mysqli_query($con, $earlyExp);
$lateExp = "SELECT * FROM $subList where dateOne != '' UNION SELECT * from $subList where dateTwo != '' ORDER BY dateTwo, dateOne DESC LIMIT 1" ;
$lateExpQuery = mysqli_query($con, $lateExp);
$countLines = "SELECT * from $subList";
$countLinesQuery = mysqli_query($con, $countLines);
$countLinesCount = mysqli_num_rows($countLinesQuery);
while ($row = mysqli_fetch_array($earlyExpQuery)) {
$earlyExpDate = $row['dateTwo'] ? $row['dateTwo'] : $row['dateOne'];
}
while ($row = mysqli_fetch_array($lateExpQuery)) {
$lateExpDate = $row['dateTwo'] ? $row['dateTwo'] : $row['dateOne'];
?>
They key is that I had to close off each individual while statement except the last one - closing that after I closed the </tbody> tag but before the </table>, like so:
</tbody>
<?php
}
}
?>
</table>
I have this pagination script
1 Show-page
$result_p = mysqli_query($conexao, "select count(*) as count FROM `banner-destaque`");
$row_p = mysqli_fetch_array($result_p);
$quant_resul = 10;
$pagina = 1;
$paginas = ceil($row_p['count'] / $quant_resul);
$result = mysqli_query($conexao, "select * FROM `banner-destaque` limit 0 , " . $quant_resul);
The script works fine to one mysql table, but I need to reuse the same pagination script for other mysql tables with diferent sizes.
This is the table that show te results
<table class="flat-table flat-table-1">
<thead>
<th>Id</th> **//Here i need get tables columns name to make it dinamically for reuse in other tables**
<th>Photo</th>
<th>Title</th>
<th>Description</th>
</thead>
<tbody>
<tr>
<?php
while ($row = mysqli_fetch_object($result)) {
echo
'
//and here i need get all columns values to populate the above column.
<td>'.$row->id.'</td>
<td>'.$row->foto.'</td>
<td>'.$row->titulo.'</td>
<td>'.$row->descricao.'</td>';
};
?>
Any solution? Thanks. I'm using mysqli to connect to the database.
make a function from the query script and then call it passing the database table name to it as a parameter. You can then re-use this for different tables
function paginate($conexao, $tblname){
$result_p = mysqli_query($conexao, "select count(*) as count FROM ".$tblname);
$row_p = mysqli_fetch_array($result_p);
$quant_resul = 10;
$pagina = 1;
$paginas = ceil($row_p['count'] / $quant_resul);
$result = mysqli_query($conexao, "select * FROM ".tblname." limit 0 , " . $quant_resul);
return $result;
}
<table class="flat-table flat-table-1">
<thead>
<th>Id</th> **//Here i need get tables columns name to make it dinamically for reuse in other tables**
<th>Photo</th>
<th>Title</th>
<th>Description</th>
</thead>
<tbody>
<tr>
<?php
$result = paginate($conexao, 'banner-destaque');
while ($row = mysqli_fetch_object($result)) {
echo
'
//and here i need get all columns values to populate the above column.
<td>'.$row->id.'</td>
<td>'.$row->foto.'</td>
<td>'.$row->titulo.'</td>
<td>'.$row->descricao.'</td>';
};
?>