Select only 1 distinct column - SQL - php

I have a table like this in my database
orderID| productID | qty
1 5 1
1 2 1
1 6 1
1 4 1
2 9 1
2 5 2
2 6 1
I want to output it into my HTML/php page in a similar way to this
Order ID:1
Product ID | Qty
5 | 1
2 | 1
6 | 1
4 | 1
Order ID:2
Product ID | Qty
9 | 1
5 | 2
6 | 1
Is this even possible to do? I have tried to use DISTINCT but that only outputs 1 row for each orderID. Here is the closest way I have outputted using the ORDER BY statement at the moment but this makes the headers for every row from the database.
<?php
if ($query = "SELECT * FROM productOrders ORDER BY orderID"){
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<table>
<tr>
<th>Order ID: <?php echo $row['orderID'] ?></th>
</tr>
<tr>
<th>Product ID</th>
<th>Qty</th>
</tr>
<tr>
<td><?php echo $row['productID'] ?></td>
<td><?php echo $row['qty'] ?></td>
</tr>
</table>
<?php
}
}
?>

Try this
<?php
$query = "SELECT * FROM productOrders ORDER BY orderID";
if ($query){
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$id=$row['orderID'];
$sql="SELECT orderID, qty FROM productOrders WHERE orderID=$id";
$q=mysqli_query($connection,$sql);
if(!$q){ echo "error"; }
else { while($row1=mysqli_fetch_assoc($q)){
?>
<table>
<tr>
<th>Order ID <?php echo $id; ?></th>
</tr>
<tr>
<th>Product ID</th>
<th>Qty</th>
</tr>
<tr>
<td><?php echo $row1['productID'] ?></td>
<td><?php echo $row1['qty'] ?></td>
</tr>
</table>
<?
}
}
}
}
}
?>

select orderid, productid, sum(qty)
from productOrders
group by orderid, productid
which will give you layout of...
OrderID | ProductID | Qty
1 | 5 | 1
1 | 2 | 1
1 | 6 | 1
1 | 4 | 1
2 | 9 | 1
2 | 5 | 2
2 | 6 | 1
Then just loop through each row and display as needed in PHP

Related

How to match array values from one array block with another and show result in table

I am trying to display report in table related to car rates. Specific employees are in charge to rate some cars and add a comment. I got confused as I am not sure how to do it, this is how I started and got stuck:
Table employees
ID | Name
1 John
2 Mark
3 Mario
4 Markus
Table cars
ID | Name
1 Audi
2 Audi S6
3 Audi Q5
4 Audi M5
Table employee_can_rate
ID | carID | employeeID
1 1 2
2 1 3
3 1 4
Table car_rates
ID | carID | employeeID | rate | month | year | comment
1 1 3 10 11 2022 All good
2 1 2 8 11 2022 Nothing specific
3 1 4 6 11 2022 Bad engine
Now my goal is to create a table that will look like this
Code..
<?php
$month = $_POST['month']; //lets assume it is 11
$year = $_POST['year'];
$stmt = "SELECT * FROM cars"
$q = $conn->query($stmt);
$cars = array();
while($r = $q ->fetch()){
$cars[] = $r;
}
foreach($cars as $car){
$stmt = "SELECT a.ID, b.Name FROM employee_can_rate a
LEFT JOIN employees b ON a.employeeID = b.ID";
$q = $conn->query($stmt);
while($r = $q ->fetch()){
$employees[] = $r;
}
}
?>
<?php
foreach($cars as $car){ ?>
<table class="table table-bordered">
<thead>
<tr class="">
<th colspan="2">Car <?= $car['Name']?></th>
</tr>
<tr class="">
<th rowspan="2">Month</th>
<th colspan="<?php echo $countOcjenjuje ?>" style="text-align:center;"> Employees can rate </th>
<th rowspan="2">Average rate </th>
</tr>
<tr>
<?php
foreach($employees as $employee){
?>
<th colspan=""><?php echo $employee["Name"]?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2"><?= $month .' / '.$year?></td>
<?php
// Here I need to match rates with employees and add a comment below
?>
<td><?php echo $rr["ocjena"]; ?></td>
<?php } else{ ?>
<td>No rate</td><?php }
} } ?>
<td rowspan="2">Average rate result</td>
</tr>
</tbody>
</table>
Any help is appreciated here.

if the date is same just display one time

if the date is same with database date just display one time only.Example:database date 16/2 then all 16/2 must display in a box, but other date also need to display. who help me to solve this problem.
<h1>Payment Record</h1>
<?php
$user_check=$_SESSION['login_user'];
$query = "SELECT * FROM `confirm_order` where customer = '$user_check'";
$result=mysqli_query($mysqli,$query);
while($row=mysqli_fetch_array($result))
{
?>
<table style="width:100%;border:1px solid black;margin-bottom:20px;">
<tr>
<td>Product Name: <?php echo $row['product_name'];?></td>
</tr>
<tr>
<td>Quantity: <?php echo $row['quantity']?></td>
</tr>
<tr>
<td>Receiver Name: <?php echo $row['receiver_name']?></td>
</tr>
<tr>
<td>Receiver Address: <?php echo $row['receiver_address']?></td>
</tr>
<tr>
<td>Receiver Contact: <?php echo $row['receiver_contact']?></td>
</tr>
<tr>
<td style="float:right">Date: <?php echo $row['date']?></td>
</tr>
</table>
<?php
}
?>
Example
id | product | date |
---|---------|------|
1 | book | 16/2 |
2 | pencil | 16/2 |
3 | shoe | 18/2 |
Result
-------
book |
pencil |
-------
shoe |
-------
Here is the complete solution:
$user_check=$_SESSION['login_user'];
$query = "SELECT * FROM `confirm_order` where customer = '$user_check'";
$result=mysqli_query($mysqli,$query);
$current_date=NULL;
$previous_date=NULL;
$table_open="<table border='1'>";
$table_close="</table><hr>";
$output=NULL;
while($row=mysqli_fetch_array($result))
{
$current_date=$row['date'];
if ($current_date==$previous_date) {
// SAME DATE
$output.= "<tr><td>SAME DATE</td><td>".$current_date."</td></tr>";
} else {
// NEW DATE
if ($previous_date<>NULL) {
$output.= $table_close;
}
$output.= $table_open."<tr><td>NEW DATE</td><td>".$current_date."</td></tr>";
}
$previous_date=$row['date'];
}
echo $output;

Order details display ORDER BY date and name

I have applied query to show order details in a table according to names and dates. It shows data correct.Right now its displaying data like this:
But now i want my data to be shown in format where i can see all the orders from same client according to date something like this:
ABC ordered on Date:20/05/15
ItemName | Size | Quantity | Color
item1 | XL | 7 | yellow
item2 | L | 3 | pink
item3 | S | 1 | green
XYZ ordered on Date:13/09/15
ItemName | Size | Quantity | Color
item1 | L | 8 | brown
item2 | L | 3 | pink
item3 | S | 4 | green
Any suggestions or help will be appreciable
Code
echo" <table border='3px' bordercolor='#333333' >";
$query="select orders.date,order_detail.quantity,order_detail.price,order_detail.color,order_detail.size,customers.name,products.product_name,products.product_image from order_detail JOIN orders on orders.serial=order_detail.orderid Join customers on customers.serial=orders.customerid Join products on products.productid=order_detail.productid ";
$sql=mysqli_query($con,$query);
while($row=mysqli_fetch_array($sql))
{
?>
<tr>
<td><?php echo $row['name'] ?> </td>
<td><?php echo $row['date'] ?></td>
</tr>
<th>Product</th>
<th>Name & quantity</th>
<th>Color</th>
<th>Price</th>
<th>Size</th>
<tr>
<td><image width="80px" height="90px" src="\images<?php echo $row['product_image'] ?>"/></td>
<td><?php echo $row['product_name']. "*". $row['quantity']?></td>
<td><?php echo $row['color'] ?></td>
<td><?php echo $row['price'] ?></td>
<td><?php echo $row['size'] ?></td>
</tr>
<?php
} }
?>
</table>
Try adding order by this way
$query="select orders.date,order_detail.quantity,
order_detail.price,order_detail.color,
order_detail.size,customers.name,products.product_name,
products.product_image from order_detail
JOIN orders on orders.serial=order_detail.orderid
Join customers on customers.serial=orders.customerid
Join products on products.productid=order_detail.productid
order by orders.date ASC, product.name ASC";
Use Group BY Order_date. If there are multiple orders from same customer Use SUM(quantity) and distinct(Itemname).

How to display array value vertically in table based on key using PHP?

I have a table which display list of item names, its classes, item quantities in each class, and total quantity of item for all items. The expected result should look like this:
Item Name | Item Class | Item Quantity | Total Quantity
| 1 | 4 |
A | 2 | 5 | 11
| 3 | 2 |
-------------------------------------------------------
| 1 | 6 |
B | 2 | 3 | 10
| 3 | 1 |
but what I got is like this:
Item Name | Item Class | Item Quantity | Total Quantity
| 1 | 4 |
A | 2 | 5 |
| 3 | 2 |
----------------------------------------
| 1 | 6 |
B | 2 | 3 |
| 3 | 1 | 11 | 10
How to display the total quantity vertically in the same row with its key(item name) and with rowspan. I use array to display results as there are hidden calculation. My code is a bit complicated because of the calculation but lets just ignore the calculation part because it works fine. If you need to understand my calculation, you can view my previous questions from my profile. What I need to fix now is how to display the total quantity correctly.
Here is my code :
<table>
<tr>
<td>Item Name</td>
<td>Item Class</td>
<td>Item Quantity</td>
<td>Total Quantity</td>
</tr>
<?php
$result=mysql_query("SELECT * FROM tblitem");
$classqty=array();
while($row = mysql_fetch_array($result)){
$item=$row['itemName'];
$class=$row['itemClassName'];
if(!isset($classqty[$item][$class]))
{
$classqty[$item][$class] = 0;
}
if(is_null($row['itemLocCheckOut'])){
$classqty[$item][$class] += $row['itemQty'];
}
else{
$classqty[$item][$class] -= $row['itemQty'];
}
}
$sum=array();
foreach($classqty as $k1=>$v1){
foreach($v1 as $k2=>$v2){
if(!isset($sum[$k1])){
$sum[$k1] = $v2;
}
else
{
$sum[$k1] += $v2;
}
?>
<tr>
<td><?php echo $k1;?></td>
<td><?php echo $k2;?></td>
<td><?php echo $v2;?></td>
<?php
} /*close second foreach*/
} /*close first foreach*/
foreach($sum as $name=>$total){
?>
<td><?php echo $total;?></td>
<?php
} /*close foreach for $sum*/
?>
</tr>
</table>
P/S: Please don't suggest me to use SELECT SUM because there is no problem with my calculation and I can't simply use SUM because my code involves multiple series of calculation and the total quantity is based on the result from the calculation. My problem is just on how to display the total quantity result correctly at its place vertically.
Try this...
SELECT *,SUM(IF(item_name='a',itemQuantity,0)) AS itemQuantityA,SUM(IF(item_name='b',itemQuantity,0)) AS itemQuantityB FROM tblitem
Using this you will directly got the sum of item A and B.
and based on your requirement you can edit or customize this query.
Hope this helps.
The HTML needs to be corrected. Have rectified the code. Try it out
<table>
<tr>
<td>Item Name</td>
<td>Item Class</td>
<td>Item Quantity</td>
<td>Total Quantity</td>
</tr>
<?php
$result=mysql_query("SELECT * FROM tblitem");
$classqty=array();
while($row = mysql_fetch_array($result)){
$item=$row['itemName'];
$class=$row['itemClassName'];
if(!isset($classqty[$item][$class]))
{
$classqty[$item][$class] = 0;
}
if(is_null($row['itemLocCheckOut'])){
$classqty[$item][$class] += $row['itemQty'];
}
else{
$classqty[$item][$class] -= $row['itemQty'];
}
}
?>
<tr>
<?php
$sum=array();
foreach($classqty as $k1=>$v1){
?>
<td><?php echo $k1;?></td>
<td>
<table>
<tr>
<?php
foreach($v1 as $k2=>$v2){
if(!isset($sum[$k1])){
$sum[$k1] = $v2;
}
else
{
$sum[$k1] += $v2;
}
?>
<td><?php echo $k2;?></td>
<td><?php echo $v2;?></td>
<?php
} /*close second foreach*/
?>
</tr>
</table>
</td>
<td><?php echo $sum[$k1];?></td>
} /*close first foreach*/
?>
</tr>
</table>

How to display tables by school year and semester using php?

I am trying to display a table by school year using PHP. I it seems not working very well because the school year displays redundantly.
How to simply display it like this?
No | SY | Student ID | Student Name | Gender | Section Code | Subject
-----------------------------------------------------------------------------
2015-2016 SEMESTER 1
-----------------------------------------------------------------------------
1 | 2 | 0011941 | Cocos, Scrappy S. | Male | IT15B | IT15
-----------------------------------------------------------------------------
1 | 2 | 1212211 | asasas, sasas a. | Male | IT15B | IT15
-----------------------------------------------------------------------------
2015-2016 SEMESTER 2
-----------------------------------------------------------------------------
1 | 2 | 0011941 | Cocos, Scrappy S. | Male | IT15B | IT15
-----------------------------------------------------------------------------
1 | 2 | aSAsaSA | dsdsadsad.ssasasas | Male | IT15B | IT15
-----------------------------------------------------------------------------
2016-2017 SEMESTER 1
-----------------------------------------------------------------------------
1 | 2 | 0011941 | Doo, Scooby D. | Male | IT15B | IT15
-----------------------------------------------------------------------------
2016-2017 SEMESTER 2
-----------------------------------------------------------------------------
1 | 2 | 0011941 | Doo, Scooby D. | Male | IT15B | IT15
here's my code:
<tr>
<th>No</th>
<th>SY</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Gender</th>
<th>Section Code</th>
<th>Subject</th>
<th colspan="2">Update</th>
</tr>
$sql= "SELECT sts.stud_id, sts.fname, sts.lname, sts.mi, sts.gender, sub.section_code, sub.subject_name, ets.sem, ets.enroll_num, ets.sy
FROM students sts
JOIN enrollments ets ON(sts.stud_id = ets.stud_id)
JOIN subjects sub ON (sub.section_code = ets.section_code)
GROUP BY sts.stud_id, sub.section_code ORDER BY ets.stud_id ASC";
$sql_sel=mysql_query($sql);
$num_rows = mysql_num_rows($sql_sel);
if($num_rows==0)
{
echo "No results found. Please try again";
}
$i=0;
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<th colspan="8"><?php echo $row['sy'];?></th> //this thing here
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['sem'];?></td>
<td><?php echo $row['stud_id'];?></td>
<td width="200"><?php echo $row['lname'].", ".$row['fname']." ".$row['mi'].".";?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['section_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center" width="82"><img src="picture/update.png" /></td>
<!----<td align="center"><img src="picture/delete.png" /></td>-------->
</tr>
<?php
}
?>
First you need to order by year and semester in your query:
$sql= "SELECT sts.stud_id, sts.fname, sts.lname, sts.mi, sts.gender, sub.section_code, sub.subject_name, ets.sem, ets.enroll_num, ets.sy
FROM students sts
JOIN enrollments ets ON(sts.stud_id = ets.stud_id)
JOIN subjects sub ON (sub.section_code = ets.section_code)
GROUP BY sts.stud_id, sub.section_code ORDER BY ets.sy ASC, ets.sem ASC, ets.stud_id ASC";
Then you only write the year row when you detect a change of year:
$i=0;
$yr=null;
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
if ($yr != ($row['sy'] . ' SEMESTER ' . $row['sem'])) {
$yr = $row['sy'] . ' SEMESTER ' . $row['sem'];
print ('<th colspan="8">' . $yr . '</th>');
}
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['sem'];?></td>
<td><?php echo $row['stud_id'];?></td>
<td width="200"><?php echo $row['lname'].", ".$row['fname']." ".$row['mi'].".";?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['section_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center" width="82"><img src="picture/update.png" /></td>
<!----<td align="center"><img src="picture/delete.png" /></td>-------->
</tr>
<?php
}

Categories