Display each color separate with values - php

I am having trouble in printing values from database.
ITEM TABLE
ITEM | COLOR | MATERIAL | DIMENSIONS | CATEGORY | QUANTITY
- 01 33 05 111 12 1000.00
- 02 33 07 125 18 200.00
- 03 33 11 156 18 254.00
- 04 56 15 25 66 113.00
- 05 66 05 11 33 521.00
I am trying to print values in table(for each color print material dimension category)
So the output will be:
COLOR - > 33
MATERIAL | DIMENSION | CATEGORY | QUANTITY
05 111 12 1000.00
07 125 18 200.00
11 156 18 254.00
COLOR - > 56
MATERIAL | DIMENSION | CATEGORY | QUANTITY
15 25 66 113.00
COLOR - > 66
MATERIAL | DIMENSION | CATEGORY | QUANTITY
05 11 33 521.00
I am using query
$query = "SELECT a.itemnb, b.colorname, c.materialname, d.categoryname, sum(a.quantity) as quantity
FROM dbo_items a
JOIN dbo_color b
ON a.color=b.colorid
JOIN dbo_material c
on a.material=c.material
JOIN dbo_category
on a.category=d.categoryid
GROUP BY b.colorname, c.materialname, d.categoryname, ";
I am using PDO.
$q=$conn->query($query);
Now I can fetch all values in table, but that is not actually I want to make.
<table class="table table-bordered table-striped">
<thead>
<tr class="bg-primary">
<td data-field="color">COLOR</td>
<td data-field="material">MATERIAL</td>
<td data-field="dim">DIMENSIONS</td>
<td data-field="quantity">QUANTITY</td>
</tr>
</thead>
<tbody>
<?php while ($r = $m->fetch()){?>
<tr>
<td><?=$r['colorname']?></td>
<td><?=$r['materialname']?></td>
<td><?=$r['categoryname']?></td>
<td><?=$r['quantity ']?></td>
<?php } ?>
</tbody>
</table>
I want to print first color and then all material related to that color.
I am having trouble there, any help or advice is appreciated?

First remove the colorname field from group by clause in query and add this column with order by means add order by colorname in query.
And then change from HTML and php code with the following:
<table class="table table-bordered table-striped">
<thead>
<tr class="bg-primary">
<td data-field="color">COLOR</td>
<td data-field="material">MATERIAL</td>
<td data-field="dim">DIMENSIONS</td>
<td data-field="quantity">QUANTITY</td>
</tr>
</thead>
<tbody>
<?php
$tempColor = '';
while ($r = $m->fetch()){
if($tempColor != $r['colorname']) {
?>
<tr><td colspan="4">Color Name: <?=$r['colorname']?></td></tr>
<?php $tempColor = $r['colorname'];
} else {
?>
<tr>
<td><?=$r['colorname']?></td>
<td><?=$r['materialname']?></td>
<td><?=$r['categoryname']?></td>
<td><?=$r['quantity ']?></td>
<?php }
}
?>
</tbody>
</table>

You have to ORDER BY 'COLOR' and not to GROUP BY 'COLOR'
After that yout test if you are switching to a new color
<?php
<table class="table table-bordered table-striped">
<thead>
<tr class="bg-primary">
<td data-field="color">COLOR</td>
<td data-field="material">MATERIAL</td>
<td data-field="dim">DIMENSIONS</td>
<td data-field="quantity">QUANTITY</td>
</tr>
</thead>
<tbody>
<?php
$prevColor = '';
while ($r = $m->fetch()){?>
<tr>
<td><? print ($prevColor == $r['colorname'] ? '' : $r['colorname']) ?></td>
<td><? print $r['materialname']?></td>
<td><? print $r['categoryname']?></td>
<td><? print $r['quantity ']?></td>
<?php
$prevColor = $r['colorname'];}
?>
</tbody>
</table>
UPDATE
If you can not change your query, so you have to order by COLOR your resultset

Check if the current colorname is equal to the previous one:
<?php
$currcolor=array();
$i=0;
while ($r = $m->fetch()){
$currcolor[$i]=$r['colorname']; ?>
<tr>
<td>
<?php if($currcolor[$i] != $currcolor[$i-1]){
echo $currcolor[$i];
} ?>
</td>
<td><?php echo $r['materialname']; ?></td>
<td><?php echo $r['categoryname']; ?></td>
<td><?php echo $r['quantity ']; ?></td>
<?php
$i++;
} ?>

The easiest approach is to make one query for getting all colors, then you can make loop for result from first query and use another query for getting all information related to specific color.
Example:
$q = 'SELECT * FROM dbo_color';
$q=$conn->query($query);
while ($res = $q->fetch()){
$secondQ = 'SELECT * FROM relatedMaterials WHERE color = ' . $res->color;
}
And include in this query all html you need for creating table.
I hope this will help you.

Related

Check Student grades using IF statement in a php while loop

I've two tables (examresults and gradings). I'm trying to give grades according to score value. here are my tables
examresults
ID
SUBJECT
SCORE
1
English
100
1
Math
80
1
Bios
40
gradings
GNAME
MAXSCORE
A
100
B
80
C
64
D
40
F
34
and here is what I've tried
my code
$counter = 0;
$stid=1;
if($sql=("SELECT * from `examresults` where `id`='$stid'"))
{
$stmt = $conn->prepare($sql);
// execute the query
$stmt->execute();
echo '<h3 class="text-center">Examination Results</h3>
';
echo'
<table class="table table-bordered">
<thead>
<tr style="background-color: #F2F2F2;">
<td class="text-bold">Sl No</td>
<td class="text-bold">Subject</td>
<td class="text-bold">Score</td>
<td class="text-bold">Grade</td>
</tr>
</thead>
';
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$score=$row['score'];
$check_for_grade = $conn->prepare("SELECT IF(maxscore>='$score',gname,IF(maxscore<='$score',gname,'')) FROM gradings");
$check_for_grade->execute();
$mypy = $check_for_grade->fetch();
$grade = $mypy[0];
echo'
<tbody>
<tr role="row" class="odd">
<td>'.++$Counter.'</td>
<td style="padding-left: 50px;">'.$row['subject'].'</td>
<td>'.$row['score'].'</td>
<td>'.$grade.'</td>
</tr>
';
}
}
echo'
</thead>
</tbody>
</table>
';
The problem is that, it gives me wrong info and I think problem exist in my IF statement, please any help.
Here is Output
ID
SUBJECT
SCORE
GRADE
1
English
100
A
1
Math
80
A
1
Bios
40
A
What I need is
ID
SUBJECT
SCORE
GRADE
1
English
100
A
1
Math
80
B
1
Bios
40
D

How do you make list based on different id with the current session of user?

I'm creating cart function for my e-commerce project. I want to create a view item list that has been grouped according to the seller id so it will not get mix.
This is my current code.
$sqlq = $Conn->prepare("SELECT c.*, i.name, i.price FROM cart c
JOIN info i
ON c.id = i.id
WHERE c.id = ? ");
$sqlq->bind_param("i", $_SESSION["id"]);
$sqlq->execute();
$result = $sqlq->get_result();
<table class="table table-bordered">
<tr>
<th width="10%">Product ID</th>
<th width="10%">Seller ID</th>
<th width="40%">Product Name</th>
<th width="10%">Quantity</th>
<th width="10%">Price</th>
<th width="15%">Total</th>
<th colspan ="2">Action</th>
</tr>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["seller_id"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["qty"]; ?></td>
<td><?php echo $row["price"]; ?></td>
<td>RM <?php echo number_format($row["qty"] * $row["price"], 2);?></td>
<td><span class="text-danger">Remove</span></td>
</tr>
<?php
$total = $total + ($row["qty"] * $row["price"]);
$count += ($row["qty"]);
}
}
?>
<tr>
<td colspan="5" align="right">Total</td>
<td align="center">RM <?php echo number_format($total, 2); ?></td>
<td colspan ="2"><?php echo ($count); ?></td>
</tr>
</table>
current output of the code:
What I'm trying to achieve is how can I make the list based on the seller_id? How do I fix the SQL in order to get output like table below?
product id | sellerid | product name | quantity |
---------------------------------------------------
4 | 1 | iphone12 | 1 |
5 | 1 | milo | 1 |
product id | sellerid | product name | quantity |
---------------------------------------------------
6 | 2 | samsung | 1 |

How to insert new row in the same table using while loop

I'm trying to make 3 tables, I've used this while loop,
the problem that
I didn't know how to make the loop insert the new row in the same table.
It makes new tables for each new row or just mess it up by putting information that should appear in another table.
Please help me.
What I got:
contract name cost period
210 sama 20 3
id date account visit
2 3-jun 20 3
...
contract name cost period
200 mama 10 3
id date account visit
4 10 3 2
....
contract name cost period
210 ana 50 6
id date account visit
6 50 6 5
What I want:
contract name cost period
210 sama 20 3
200 mama 10 3
210 ana 50 6
id date account visit
2 3-jun 20 3
4 10 3 2
6 50 6 5
This is my code:
<?php while($row = mysqli_fetch_array($search_result)):?>
<div style="font-size:1.5rem; padding:0px; margin:0px; position:relative; width:95%; " dir="rtl">
<h1>contract </h1>
<table bgcolor="#fff" align="center" width="70%" border="3" dir="rtl" style="font-size:1.5rem; ">
<tr>
<td>name</td>
<td>cost</td>
<td>period</td>
<tr>
<td><?php echo $row['contract_num'];?></td>
<td><?php echo $row['cost'];?></td>
<td><?php echo $row['bla'];?></td>
<td><?php echo $row['serv'];?></td>
</tr>
</table></div>
<div style="font-size:1.5rem; padding:0px; margin:0px; position:relative; width:95%; " dir="rtl">
<h1>visits</h1>
<table bgcolor="#fff" align="center" width="70%" border="3" dir="rtl" style="font-size:1.5rem; ">
<tr>
<td>id</td>
<td>date</td>
<td>account</td>
<td>visit</td>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['date'];?></td>
<td><?php echo $row['account'];?></td>
<td><?php echo $row['visit'];?></td>
</tr>
</table></div>
<?php endwhile;?>
Read more on tables: https://www.w3schools.com/html/html_tables.asp
thead - define table header. It has one row (tr) with your table's headers (th)
tbody - set the data for the columns. tr = table row, td = table data, or table cell
<table>
<thead> <!-- define the header outside the while, it only needs to be defined once -->
<tr>
<th>Contract</th>
<th>Name</th>
<th>Cost</th>
<th>Period</th>
</tr>
</thead>
<tbody> <!-- in the body loop through all results and print one row for each result -->
<?php while($row...): ?>
<tr>
<td><?php echo $row['contract'] ?></td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['cost'] ?></td>
<td><?php echo $row['period'] ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>

Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined when rowspan used

I have a table with records from database results populated using foreach loop.
The last column, "Roll Over", of the table, however, is a single cell value with rowspan attribute.
I'm using dataTable plugin for the table.
<table class="table table-striped table-hover" id="leave_balance_table">
<thead>
<tr>
<th>Leave Type</th>
<th>Max Days</th>
<th>Leave Taken</th>
<th>Leave Balance</th>
<th>Roll Over</th>
</tr>
</thead>
<tbody>
<?php
$totalLeaveTaken = 0.00;
$totalBalance = 0.00;
$totalRows = count($GetEmployeeLeaveBalance);
$lastColumnPrinted = false; //Add This
foreach ($GetEmployeeLeaveBalance as $member):
$totalLeaveTaken += $member['usedDays'];
$totalBalance += $member['Remaining_Leave_Days'];
$leaveBalance = floatval($member['Remaining_Leave_Days']);
?>
<tr>
<td><?php echo $member['title']; ?></td>
<td><?php echo $member['maxDays']; ?></td>
<td><?php echo $member['usedDays']; ?></td>
<!-- <td><?php echo gettype($leaveBalance);?></td> -->
<td
<?=
($leaveBalance < 0) ?
"style='background-color:red;font-weight:bold;color:white;'" : ""
?>
>
<?php echo $member['Remaining_Leave_Days']; ?>
</td>
<!--Add The Line Below -->
<?php if($lastColumnPrinted == false): $lastColumnPrinted = true;?>
<td rowspan="<?= $totalRows; ?>">Some computed value</td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
<tr>
<td></td>
<td></td>
<td style="background-color: #33CCFF; font-weight: bold;">Total: <?php echo number_format($totalLeaveTaken, 2); ?></td>
<td style="background-color: #33CCFF; font-weight: bold;">Total: <?php echo
number_format($totalBalance, 2); ?></td>
<td></td>
</tr>
</tbody>
</table>
The output is somewhat like this:
Leave Type | Max Days | Leave Taken | Leave Balance | Roll Over
Float | 3 | 2 | 1 | 5
Personal | 3 | 2 | 1 |
Sick | 3 | 2 | 1 |
Mourning | 3 | 2 | 1 |
The result is as expected, but I'm getting above mentioned error in the console.
Is it because there are empty cells in the table?

Display a row with total amount of shift wise

I provide staff members to a site. They work shit wise Day and Night.
To make monthly Invoice I display the staff according to Shift wise.
Like
Day Shift
Emp Name Days Amount
A 30 1200
B 25 1000
Night Shift
C 27 1100
D 26 1050
I want to display a row with total Amount Shift Wise
I have this code
<?php
$temp='';
foreach ( $rows as $row)
{
///To Display Shift Row///////
if($temp!=$row->shift_type)
{
?>
<tr><td style="font-weight:bold"><?php echo $row->shift_type;?> Shift</td></tr>
<tr style=" border-top:3px solid #CCC">
<th style="text-align:center">Staff Name</th>
<th style="text-align:center"> Days</th>
<th style="text-align:center"> Amount</th>
</tr>
<?php $temp = $row->shift_type;
}
?>
////To Display Staff Members with Salry////
first_name.$row->last_name; ?>
<td align="center"><?php echo $row->week_days; ?></td>
<td align="center">$<?php echo $weekdaysamount= $row->week_days*$row->week_day_rate;
$totalweekdaysamount+=$weekdaysamount;?>
</td>
</tr>
<?php } ?>
This make the total of all rows . I want a row with total of Shift wise.
Should be like this
Day Shift
Emp Name Days Amount
A 30 1200
B 25 1000
Total 2200
Night Shift
C 27 1100
D 26 1050
Total 2150
<html>
<table style="width:100%">
<?php
require("stackoverflow_connect.php");
$extract=mysql_query("SELECT * FROM `employees` WHERE shift_type='Day'");
echo 'Day Shift';
echo '
<tr>
<td>Emp Name</td>
<td>Days</td>
<td>Amount</td>
</tr>
';
$total=0;
while($row=mysql_fetch_assoc($extract))
{
$total=$total+($row['week_day_rate']*$row['week_day']);
echo '
<tr>
<td>'.$row['name'].'</td>
<td>'.$row['week_day'].'</td>
<td>'.$row['week_day_rate']*$row['week_day'].'</td>
</tr>';
}
echo '<tr>
<td></td>
<td>Day Total</td>
<td>'.$total.'</td>
</tr>';
$extract_n=mysql_query("SELECT * FROM `employees` WHERE shift_type='Night'");
$total=0;
while($row_n=mysql_fetch_assoc($extract_n))
{
$total=$total+($row['week_day_rate']*$row['week_day']);
echo '
<tr>
<td>'.$row['name'].'</td>
<td>'.$row['week_day'].'</td>
<td>'.$row['week_day_rate']*$row['week_day'].'</td>
</tr>';
}
echo '<tr>
<td></td>
<td>Night Total</td>
<td>'.$total.'</td>
</tr>';
?>
</table>
<html>
You want to display the total shift wise,first order the result by the shift_type when fetching data.Then while calculating check the shift_type and if it is different from the previous record reset the value to zero.Something like below should work
$previous_shilft='';//Initally outside the foreach
if($previous_shilft!=$row->shift_type && !empty($previous_shilft)){
echo '<tr>
<td>Total</td>
<td></td>
<td>'.$totalweekdaysamount.'</td>
</tr>';//Display the row
}
if($previous_shilft!=$row->shift_type){
$totalweekdaysamount=0;
}
$totalweekdaysamount+=$weekdaysamount;
$previous_shilft=$row->shift_type;//For next loop check change the value

Categories