Display a row with total amount of shift wise - php

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

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

Display only specific data from array containing database values

I have a query which select the last entered data into the database month wise and i have assigned it into an array and tried to display it inside a table which works fine but the data is combined togeather as shown below
Example - the first numeric data you see on the table 1245871 should be displayed as follows
Month - 12
Closing Mileage - 45871
But as you can see on the image both the data are combined togeather how can i seperate these two values and display them accordingingly. The code for this is as below,
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$querymain = "SELECT * FROM users";
$resultsmain = mysqli_query($connect,$querymain);
if ($resultsmain->num_rows>0) {
while ($userid = mysqli_fetch_assoc($resultsmain)) {
?>
<table class="table" class="mt-3 mb-3">
<thead class="bg-dark text-light">
<tr>
<th colspan="3"><?php echo $userid['id']; ?></th>
<th colspan="3"><?php echo $userid['name']; ?></th>
<th colspan="3"><?php echo $userid['approved_kmpl'];?> KM</th>
</tr>
</thead>
<thead>
<tr>
<th>Month</th>
<th>Closing Mileage</th>
<th>Usage For Month</th>
<th>Required Per Month</th>
<th>Excess Used</th>
<th>(%)</th>
<th>KM/L</th>
<th>Consumed Liters</th>
<th>Cost</th>
</tr>
</thead>
<tbody>
<?php
$closingmileage = "SELECT extract(MONTH from date) as Month,
MAX(mileage) FROM mileagesnew
WHERE user_id='".$userid['id']."'
AND extract(YEAR FROM date) ='2020'
group by Month
ORDER BY month desc";
$closingres = mysqli_query($connect,$closingmileage);
if ($closingres->num_rows>0) {
while ($closingrow = mysqli_fetch_assoc($closingres)) {
$data =array($closingrow);
foreach($data as $value){
print_r($value);
?>
<tr>
<td> <?php echo implode($value); ?> </td>
</tr>
<tr>
<td> <?php echo implode($value); ?> </td>
</tr>
<?php
}
}
}
else{
echo "No Data Found";
}
?>
</tbody>
</table>
<?php
}
}
You have 2 columns returned in the resultset of the query Month and mileagesnew. So $closingrow will be an array with 2 members, named as the columns names from your table, or the alias you give the column name in the query. So all you need to do is output $closingrow['Month'] and $closingrow['mileagesnew'] seperately in the 2 table cells
Also I assume you want the 2 values on the same row of the tabel to output both cells within the same <tr>...</tr> along with the empty cells you have not filled yet
<?php
$closingmileage = "SELECT extract(MONTH from date) as Month,
MAX(mileage) FROM mileagesnew
WHERE user_id='".$userid['id']."'
AND extract(YEAR FROM date) ='2020'
group by Month
ORDER BY month desc";
$closingres = mysqli_query($connect,$closingmileage);
if ($closingres->num_rows>0) {
while ($closingrow = mysqli_fetch_assoc($closingres)) {
echo "<tr>
<td>$closingrow[Month]}</td>
<td>$closingrow[mileagesnew ]</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>";
}
} else{
echo "No Data Found";
}
?>

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>

foreach for php merging and adding the total amount in php

So guys I have a php code here:
<?php foreach($posts as $post){ ?>
<?php $totamount = ($post->price) * ($post->qty); ?>
<table>
<tr>
<th>Item Name</th>
<th>Quantity</th>
<th>Price<th>
<th>Total</th>
</tr>
<tr>
<td><div class="name"><?php echo $post->name; ?></div></td>
<td><div class="qty"><?php echo $post->qty; ?></div></td>
<td><div class="price"><?php echo $post->price; ?></div></td>
<td><div class="totamount"><?php echo $totamount; ?></div></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<div class="client"><?php echo $post->firstname; ?> <?php echo $post->lastname; ?></div>
<?php } ?>
If the client reserves one product it is good, but when the client adds new product, the receipt would look like this:
Item Name Quantity Price Total
gown 2 7.50 15
clients name
Item Name Quantity Price Total
bridal 1 5.00 5
clients name
Suddenly this became a loop, you can see that the <th> is looping and also the client name, I want it to be like this:
Item Name Quantity Price Total
gown 2 7.50 15
bridal 1 5.00 5
TOTAL AMOUNT: 20
clients name
And also, how can I get the TOTAL AMOUNT without using SQL because Total is not in the database! How will I do everything right?
Put in the loop only the <tr> with the product info:
<table>
<tr>
<th>Item Name</th>
<th>Quantity</th>
<th>Price<th>
<th>Total</th>
</tr>
<?php foreach($posts as $post){ ?>
<?php $totamount = ($post->price) * ($post->qty);
$orderTotal += $totamount;
?>
<tr>
<td><div class="name"><?php echo $post->name; ?></div></td>
<td><div class="qty"><?php echo $post->qty; ?></div></td>
<td><div class="price"><?php echo $post->price; ?></div></td>
<td><div class="totamount"><?php echo $totamount; ?></div></td>
</tr>
<?php } ?>
</table>
<div class="total"><?=$orderTotal?></div>
<div class="client"><?php echo $post->firstname; ?> <?php echo $post->lastname; ?></div>

Display each color separate with values

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.

Categories