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>
Related
I am trying to display these data horizontally, the problem is it overlaps the headers and it only creates duplicate entry on 1st and 2nd rows of the table.
Here is an example of the problem:
Column A
Column B
Column C
Column D
Column E
No Column
No Column
No Column
No Column
#1
20
30
40
50
30
25
15
10
#2
20
30
40
. 50.
30
25
15
10
What the correct should be is:
Column A
Column B
Column C
Column D
Column E
#1
20
30
40
50
#2
30
25
15
. 10
I've already construct the table like this:
<div class="table-responsive">
<table class="table table-striped table-bordered" style="width:100%">
<tbody class="text-center">
<?php foreach($candidatesf as $candidate): ?>
<tr> <td><?php echo $candidate['fullname'] ?></td>
<?php foreach($candscores as $candscore): ?>
<?php echo "\t<td>"; ?><?php echo $candscore['score']; ?></td>
<?php endforeach; ?>
<tr></tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
You're opening the table row
Then you add some td elements.
At the end, you open another and close that one.
You have a syntax error here:
<tr></tr>
You should only close it like this:
</tr>
<tbody class="text-center">
<?php foreach($candidatesf as $candidate): ?>
<tr>
<td><?php echo $candidate['fullname'] ?></td>
<?php foreach($candscores as $candscore): ?>
<td><?php echo $candscore['score']; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
I have a table whose records are populated with result from database and foreach loop is used.
I want few columns with a single cell value to be merged with other columns.
Like:
Leave Type | Max Days | Leave Taken | Leave Balance | Roll Over
Float | 3 | 2 | 1 |
Personal | 3 | 2 | 1 | 5
Sick | 3 | 2 | 1 |
Mourning | 3 | 2 | 1 |
First four records are populated using foreach loop, and I need to separately calculate the fifth column and append it.
Here's my HTML:
<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);
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>
<!-- <td rowspan="<?= $totalRows; ?>">Some computed value</td> -->
</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>
</tr>
</tbody>
</table>
How do I get the fifth column out of foreach loop and append it?
Just add a flag which can help you check if you have already printed the last column as shown below:
$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; ?>
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?
I want to display the employees in Hierarchical tree structure. I have tried with a loop. But it was so hard to get the output. I'm new to PHP, help me with this, that is, how to write the recursive function in my source code.
<?php
include_once('config.php');
$Empname="Prakash";
$Designation="HR";
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" language="javascript"></script>
</head>
<body>
<div align="right">
Logout
</div>
<div id="div">
<h4>EmpName:<?php echo $Empname ;?></h4>
<h4>Designation:<?php echo $Designation ;?></h4>
</div>
<form>
<?php
$selectAF=array();
$i=0;
while($getbyHR=mysql_fetch_array($selectbyHR))
{
$selectAF[$i]=$getbyHR['Empname'];
?>
<table>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<tbody>
<tr>
<td><?php echo $getbyHR['Empname']?></td>
<td><?php echo $getbyHR['Designation']?></td>
<td><?php echo $getbyHR['Controlof'];?></td>
<td><?php echo $getbyHR['Projectstatus']?></td>
</tr>
</tbody>
</table>
<table>
<h2>Project managers</h2>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<?php
$selectedPM=array();
$x=0;
for($p=0;$p<count($selectAF);$p++)
{
$PMname=$selectAF[$p];
$selectpm=mysql_query("select * from reports where Controlof='$PMname'");
while($getPM=mysql_fetch_array($selectpm))
{
$selectedPM[$x] =$getPM['Empname'];
?>
<tbody>
<tr>
<td><?php echo $getPM['Empname']; ?></td>
<td><?php echo $getPM['Designation'];?></td>
<td><?php echo $getPM['Controlof'];?></td>
<td><?php echo $getPM['Projectstatus'];?></td>
</tr>
<?php
$x++;
}
}
?>
</tbody>
</table>
<table>
<h2>TeamLeaders</h2>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<?php
$selectedTL=array();
$q=0;
for($t=0;$t<count($selectedPM);$t++)
{
$TLname=$selectedPM[$t];
$selectedteamleader=mysql_query("select * from reports where Controlof='$TLname'");
while($getTL=mysql_fetch_array($selectedteamleader))
{
$selectedTL[$q] =$getTL['Empname'];
?>
<tbody>
<tr>
<td><?php echo $getTL['Empname']; ?></td>
<td><?php echo $getTL['Designation'];?></td>
<td><?php echo $getTL['Controlof'];?></td>
<td><?php echo $getTL['Projectstatus'];?></td>
</tr>
<?php
$q++;
}
}
?>
</tbody>
</table>
<table>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<h2>SeniorEngineer</h2>
<?php
$selectedSE=array();
$w=0;
for($s=0;$s<count($selectedTL);$s++)
{
$SEname=$selectedTL[$s];
$selectedseniorengineer=mysql_query("select * from reports where Controlof='$SEname'");
while($getSE=mysql_fetch_array($selectedseniorengineer))
{
$selectedSE[$w] =$getSE['Empname'];
?>
<tbody>
<tr>
<td><?php echo $getSE['Empname']; ?></td>
<td><?php echo $getSE['Designation'];?></td>
<td><?php echo $getSE['Controlof'];?></td>
<td><?php echo $getSE['Projectstatus'];?></td>
</tr>
<?php
$w++;
}
}
?>
</tbody>
</table>
<table>
<h2>AssistantEngineer</h2>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<?php
$selectedAE=array();
$b=0;
for($a=0;$a<count($selectedSE);$a++)
{
$AEname=$selectedSE[$a];
$selectedassistantengineer=mysql_query("select * from reports where Controlof='$AEname'");
while($getAE=mysql_fetch_array($selectedassistantengineer))
{
$selectedAE[$b] =$getAE['Empname'];
?>
<tbody>
<tr>
<td><?php echo $getAE['Empname']; ?></td>
<td><?php echo $getAE['Designation'];?></td>
<td><?php echo $getAE['Controlof'];?></td>
<td><?php echo $getAE['Projectstatus'];?></td>
</tr>
<?php
$b++;
}
}
?>
</tbody>
</table>
<table>
<h2>Execuetive Engineer</h2>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<?php
$selectedEE=array();
$y=0;
for($e=0;$e<count($selectedAE);$e++)
{
$Engineername=$selectedAE[$e];
$selectedengineer=mysql_query("select * from reports where Controlof='$Engineername'");
while($getEE=mysql_fetch_array($selectedengineer))
{
$selectedEE[$y] =$getEE['Empname'];
?>
<tbody>
<tr>
<td><?php echo $getEE['Empname']; ?></td>
<td><?php echo $getEE['Designation'];?></td>
<td><?php echo $getEE['Controlof'];?></td>
<td><?php echo $getEE['Projectstatus'];?></td>
</tr>
<?php
$y++;
}
}
?>
</tbody>
</table>
<table>
<h2>Trainee</h2>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<?php
$selectedtrainee=array();
$g=0;
for($r=0;$r<count($selectedEE);$r++)
{
$EEname=$selectedEE[$r];
$selectedTrainee=mysql_query("select * from reports where Controlof='$EEname'");
while($gettrainee=mysql_fetch_array($selectedTrainee))
{
$selectedtrainee[$g] =$gettrainee['Empname'];
?>
<tbody>
<tr>
<td><?php echo $gettrainee['Empname']; ?></td>
<td><?php echo $gettrainee['Designation'];?></td>
<td><?php echo $gettrainee['Controlof'];?></td>
<td><?php echo $gettrainee['Projectstatus'];?></td>
</tr>
<?php
$g++;
}
}
?>
</tbody>
</table>
<table>
<h2>Ap</h2>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<tbody>
<?php
$selectedAp=array();
for($v=0;$v<count($selectedtrainee);$v++)
{
$Trainee=$selectedtrainee[$v];
$selectedAp=mysql_query("select * from reports where Controlof='$Trainee'");
while($getAp=mysql_fetch_array($selectedAp))
{
?>
<tr>
<td><?php echo $getAp['Empname']; ?></td>
<td><?php echo $getAp['Designation'];?></td>
<td><?php echo $getAp['Controlof'];?></td>
<td><?php echo $getAp['Projectstatus'];?></td>
</tr>
<?php
}
}
?>
<?php
// while loop of AF
$i++;
}
?>
</tbody>
</table>
</form>
</body>
</html>
Table
Sno|Empname|Designation|Controlof|
1 |Prakash|HR |Ram |
2 |Sasi |AF |prakash |
3 |venki |AF |prakash |
4 |Tom |PM |Sasi |
5 |orton |PM |venki |
6 |Ram |TL |Tom |
7 |David |SE |orton |
If prakash logged into his profile the dashboard has to look like this
sno |Empname|Designation|controlof
1 Sasi AF prakash
2 venki AF Prakash
Project Managers
sno |Empname|Designation|Control of sno |Empname|Designation|Controlof
1 Tom PM Sasi 1 orton PM Venki
The easiest start point to reduce your code is to remove common code to a function...
function displayEmployees ( $employee ) {
echo '<tbody>';
$selected=array();
$select=mysql_query("select * from reports where Controlof='$employee'");
while($get=mysql_fetch_array($select))
{
$selected[] =$get['Empname'];
echo '<tr>';
echo '<td>'.$get['Empname'].'</td>';
echo '<td>'.$get['Designation'].'</td>';
echo '<td>'.$get['Controlof']'.</td>';
echo '<td>'.$get['Projectstatus'].'</td>';
echo '</tr>';
}
echo '</tbody>';
return $selected;
}
Then you can call it at each point you need to display data, so for example...
<?php
$selectedPM=displayEmployees($selectAF);
?>
If you only need one level of hierarchy, recursion is not necessary. But if you want to show subordinates of subordinates, then recursion is one of the ways to do it.
First, we need to make a change to the schema to make it more reliable. You can match against names, but all it takes is one letter mistyped to break everything. Use the id to match against. I have renamed Sno and Controlof to the more typical id and pid (parent id). pid is simply the id of the employee's manager. Use 0 or null if an employee has nobody over them.
table: Employees
id |pid | employeeName | designation
1 | 6 | Prakash | HR
2 | 1 | Sasi | AF
3 | 1 | Venki | AF
4 | 2 | Tom | PM
5 | 3 | Orton | PM
6 | 4 | Ram | TL
7 | 5 | David | SE
If you only need to go one level deep, then recursion is not needed; you just need a good sql query, assuming you know the id of the employee that is logged in:
SELECT * FROM Employees WHERE pid=?
and then bind the logged in employee's id to the placeholder.
Note: learn how to make queries using prepared statements. mysql_query("select * from reports where Controlof='$PMname'"); is vulnerable to SQL injection.
If for some reason you don't know the employee id, you could get there even by searching for the employee's name:
SELECT a.* from Employees as a, Employees as b WHERE a.pid=b.id AND a.employeeName LIKE ?
Recursion is a little more complicated... and is best done in an object so that you don't have to worry about variable scope. If you need to go deeper into the hierarchy, let me know and I'll write another answwer. It's more complicated and deserves a separate answer.
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