How to write a recursive function in my PHP source code - php

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.

Related

How to display registered class based on three table

im still learning in php. Appreciate on your kindness. :)
<table style="width:100%;float:left">
<thead>
<tr>
<th>Class ID</th>
<th>Subject</th>
<th>Day</th>
<th>Time</th>
<th>Classroom</th>
</tr>
</thead>
Table name 'carts' :
<?php $query1=mysqli_query($mysqli,"select * from carts where status=1 and
id='$id';")or die(mysqli_error($mysqli));
while($row_carts=mysqli_fetch_array($query1))
{
$cartID = $row_carts['cartID']; //cartID=3 in my database
Table name 'cartitems' :
$query2=mysqli_query($mysqli,"select * from cartitems where
cartID='$cartID';")or die(mysqli_error($mysqli));
$row_cartitems=mysqli_fetch_array($query2);
$classID = $row_cartitems['classID'];//classID=13 & classID=14 in mydatabase
Table name 'class_detail' :
$query3=mysqli_query($mysqli,"select * from class_detail where
classID='$classID';")or die(mysqli_error($mysqli));
$row_class_detail=mysqli_fetch_array($query3);
$subject_title = $row_class_detail['subject_title'];
$date = $row_class_detail['date'];
$time = $row_class_detail['time'];
$classroom_name = $row_class_detail['classroom_name'];
?>
To display information based of classID=13 and classID=14:
<tr class="show">
<td class="name">SS<?php echo $classID;?></td>
<td><?php echo $subject_title; ?></td>
<td><?php echo $date;?></td>
<td><?php echo $time;?></td>
<td><?php echo $classroom_name;?></td>
</tr>
<?php }?>
</table>
I want to display it like this:
Class ID| Subject | Day | Time |Classroom|
--------+----------+------------+-----------+---------+
SS13 | ENGLISH | SUNDAY | 2PM - 4PM | G |
SS14 | HISTORY | WEDNESDAY | 9AM - 11AM| B |
But the coding display it like this:
Class ID| Subject | Day | Time |Classroom|
--------+----------+------------+-----------+---------+
SS13 | ENGLISH | SUNDAY | 2PM - 4PM | G |
Try change your code like this :
<?php
$query3=mysqli_query($mysqli,"select * from class_detail where
classID='$classID';")or die(mysqli_error($mysqli));
while($row_class_detail=mysqli_fetch_array($query3))
{
?>
<tr class="show">
<td class="name">SS<?php echo $classID;?></td>
<td><?php echo $row_class_detail['subject_title']; ?></td>
<td><?php echo $row_class_detail['date']; ?></td>
<td><?php echo $row_class_detail['time']; ?></td>
<td><?php echo $row_class_detail['classroom_name']; ?></td>
</tr>
<?php
}
?>
Hope it helps you.
Edit :
I created new array in cartitems while, you can take a look at code below :
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table style="width:100%;float:left">
<thead>
<tr>
<th>Class ID</th>
<th>Subject</th>
<th>Day</th>
<th>Time</th>
<th>Classroom</th>
</tr>
</thead>
<tbody>
<?php
$host="localhost";
$user="root";
$pass="";
$db="stack";
$id=3;
$connect=mysqli_connect($host,$user,$pass,$db);
if(mysqli_connect_errno())
{
echo "Terjadi kesalahan koneksi";
}
$cartID=0;
$val=0;
$mylistuser=array();
$sql="SELECT * FROM carts WHERE id='$id' AND status='1'";
$run=mysqli_query($connect,$sql);
while($row_carts=mysqli_fetch_array($run))
{
$cartID = $row_carts['id'];
}
$sql2="SELECT * FROM cartitems WHERE cartID='$cartID'";
$run2=mysqli_query($connect,$sql2);
while($row_carts2=mysqli_fetch_array($run2))
{
$mylistuser[$val]['classID']=$row_carts2['classID'];
$val++;
}
for($i=0;$i<count($mylistuser);$i++)
{
$sql3="SELECT * FROM class_detail WHERE classID='".$mylistuser[$i]
['classID']."'";
$run3=mysqli_query($connect,$sql3);
$row_carts3=mysqli_fetch_array($run3);
?>
<tr>
<td>SS<?php echo $row_carts3['classID']; ?></td>
<td><?php echo $row_carts3['subject_title']; ?></td>
<td><?php echo $row_carts3['date']; ?></td>
<td><?php echo $row_carts3['time']; ?></td>
<td><?php echo $row_carts3['classroom_name']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
What you are doing is sending the data only for one ID to your html table. In this case you are sending data for classID=13. If you want to display both, you need to send the data array to the html table, and then loop through the data and display the results.

Showing one value outside the table from joined tables in Codeigniter

I have this three tables that i joined, those tables are: attendance, employee, and division
this is my model to show the data
function lihatData()
{
$this->db->select('attendance.emp_code, attendance.emp_name,division.name_division,attendance.date,attendance.time_in, attendance.time_out');
$this->db->from('attendance');
$this->db->join('employee', 'employee.emp_code = attendance.emp_code');
$this->db->join('division', 'employee.id_division = divisiob.id_division');
$this->db->where_in('division.id_division',$this->session->userdata('id_division')
}
this is my view
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th><center>No</center></th>
<th><center>EMP CODE</center></th>
<th><center>NAME</center></th>
<th><center>DIVISION</center></th>
<th><center>DATE</center></th>
<th><center>TIME IN</center></th>
<th><center>TIME OUT</center></th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
if(is_array($data)){
foreach($data as $row){
?>
<tr>
<td><?php echo $no++;?></td>
<td><?php echo $row->emp_code; ?></td>
<td><?php echo $row->emp_name; ?></td>
<td><?php echo $row->name_division; ?></td>
<td><?php echo date("d-m-Y", strtotime($row->date))?></td>
<td><?php echo $row->time_in; ?></td>
<td><?php echo $row->time_out; ?></td>
</tr>
<?php } }?>
</tbody>
</table>
This is what i got from my code:
So what i need to change the results into something like this:
Division : Redaksi (based on user's division)
-------------------------------------------------------------------
| emp code | name | date | time in | time out |
-------------------------------------------------------------------
UPDATE: I already managed to group the division into a table, and here's the result:
As you can see, its already show datas of user's division. but i need to remove the division table, and change it into some kind of table's name or put the division's name at the top of the table based on user's name
a possible solution would be to extract data from your first array Item, something like the following should work
<?php
if (is_array($data) && count($data) > 0)
{
?>
<h1><?=$data[0]->nama_division; ?></h1>
<?php
}
?>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th><center>No</center></th>
<th><center>EMP CODE</center></th>
<th><center>NAME</center></th>
<th><center>DATE</center></th>
<th><center>TIME IN</center></th>
<th><center>TIME OUT</center></th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
if(is_array($data))
{
foreach($data as $row){
?>
<tr>
<td><?php echo $no++;?></td>
<td><?php echo $row->emp_code; ?></td>
<td><?php echo $row->emp_name; ?></td>
<td><?php echo date("d-m-Y", strtotime($row->date))?></td>
<td><?php echo $row->time_in; ?></td>
<td><?php echo $row->time_out; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
function lihatData()
{
$this->db->from('attendance');
$this->db->join('employee', 'employee.emp_code = attendance.emp_code');
$this->db->join('division', 'employee.id_division = divisiob.id_division');
$this->db->where_in('division.id_division',$division);
return $this->db->get()->result();
}
try this
The key is how to group result by division. I think you can sort the result array by division, or group it as follow:
$sortedResult = [];
foreach ($data as $row) {
$sortedResult[$row->name_division] = $row;
}
//Render
<?php foreach ($sortedResult as $devision => $data) { ?>
<div><?php echo $devision; ?></div>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th><center>No</center></th>
<th><center>EMP CODE</center></th>
<th><center>NAME</center></th>
<th><center>DATE</center></th>
<th><center>TIME IN</center></th>
<th><center>TIME OUT</center></th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
foreach($data as $row){
?>
<tr>
<td><?php echo $no++;?></td>
<td><?php echo $row->emp_code; ?></td>
<td><?php echo $row->emp_name; ?></td>
<td><?php echo date("d-m-Y", strtotime($row->date))?></td>
<td><?php echo $row->time_in; ?></td>
<td><?php echo $row->time_out; ?></td>
</tr>
<?php }?>
</tbody>
</table>
<?php } ?>

merge a column with one cell that rowspans other rows with foreach loop

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; ?>

How to Flight_Name instead of Flight_ID?

Sir I have two tables.. (i-e)
(1) fare(Fare_ID, Flight_ID, Departure_Arrival_Station, Fare
(2) flights(Flight_ID, Flight_Name, No_Of_Seats.
I have foreach all data from *fare table and it gives me Flight_ID, which is correct. Now how should i will display the flight_Name instead of Flight_ID on the screen.. I try on it, this is my code. Thank you..
My Model
function getFlightName()
{
$this->db->select("flights.Flight_ID, flights.Flight_Name");
$this->db->from('flights');
$this->db->join('fare', 'fare.Flight_ID = flights.Flight_ID');
$query = $this->db->get();
return $query->result();
}
My Controller
public function fare()
{
$data['tt'] = $this->db->get('fare')->result();
$this->load->model('time_tbl_model');
$data['flt_nm'] = $this->time_tbl_model->getFlightName();
$this->load->view('front/fare', $data);
}
My View
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th class="center">Fare_ID</th>
<th class="center">Flight Name</th>
<th class="center">Departure_Arrival_Station</th>
<th class="center">Fare</th>
</tr>
</thead>
<tbody>
<?php foreach ($tt as $s): ?>
<tr class="odd gradeX">
<td><?php echo $s->Fare_ID; ?></td>
<td><?php echo $s->Flight_ID; ?></td>
<?php foreach ($flt_nm as $ff): ?>
<td><?php echo $ff->Flight_Name; ?> </td>
<?php endforeach; ?>
<td><?php echo $s->Departure_Arrival_Station; ?></td>
<td><?php echo $s->Fare; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
This is output on the screen, when i executed it.
snag.gy/zLt2d.jpg
I saw your edited post and your image and I believe what you are trying to achieve is like the example of result in the end of this post.
Besides other problems, you're creating some messy stuff when using model code inside controller function. You should separate that, otherwise you're not correctly using MVC pattern.
That said, here's an example of what I would do:
Your model
public function getFlights()
{
$this->db->select('f.*, GROUP_CONCAT(fl.Flight_Name SEPARATOR "<br/>") AS `flight_names`')
->from('fare AS f')
->join('flights AS fl', 'fl.Flight_ID = f.Flight_ID', 'INNER')
->group_by('f.Flight_ID');
return $this->db->get()->result();
}
Your controller
$this->data['flights'] = $this->your_model->getFlights();
Your view
<?php foreach($flights as $flight) { ?>
<td><?php echo $flight->Fare_ID; ?></td>
<td><?php echo $flight->flight_names; ?></td>
<td><?php echo $flight->Departure_Arrival_Station; ?></td>
<td><?php echo $flight->Fare; ?></td>
<?php } ?>
This will reproduce something like:
Fare_ID | Flight Names | Departure Arrival Station | Fare
Flight Names
Flight Names
Fare_ID | Flight Names | Departure Arrival Station | Fare
Fare_ID | Flight Names | Departure Arrival Station | Fare
Flight Names
Flight Names
Flight Names
as i understood
<?php foreach ($tt as $s): ?>
<tr class="odd gradeX">
<td><?php echo $s->Fare_ID; ?></td> //Fare_ID
<?php foreach ($flt_nm as $ff): ?>
<td><?php echo $ff->Flight_Name; ?> </td> //Flight Name
<?php endforeach; ?>
<td><?php echo $s->Departure_Arrival_Station; ?></td>
<td><?php echo $s->Fare; ?></td>
</tr>
<?php endforeach;

Fetch and display data from database in table format in HTML form using PHP

I have done with fetching and display the data. But the problem is ,its display the data of fetched row in the same row of html table.
For example : i want to display like this
ID | Name | Desination
......................
1 | ABC | Developer
2 | PQR | Tester
3 | XYZ | Developer
But its showing as -
ID | Name | Desination
......................
1 | ABC | Developer 2 | PQR | Tester 3 | XYZ | Developer
I have done something like this-
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_desc FROM candidate ".$join.' where '.$condition;
$result = mysql_query($sql) or die(mysql_error());
Correct me with the display format of table.
<div class="box-body table-responsive no-padding">
<table class="table table-hover">
<tr>
<th>ID</th>
<th>Name</th>
<th>Designation</th>
</tr>
<tr>
<?php
If(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
?>
<td><?php echo $row['cand_number']; ?></td>
<td><?php echo $row['cand_fname']; ?></td>
<td><?php echo $row['cand_desc']; ?></td>
<?php
}
}
?>
</tr>
</table>
</div>
You need to repeat row not column
<?php
If (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['cand_number']; ?></td>
<td><?php echo $row['cand_fname']; ?></td>
<td><?php echo $row['cand_desc']; ?></td>
</tr>
<?php
}
}
?>
Use this HTML and try
<div class="box-body table-responsive no-padding">
<table class="table table-hover">
<tr>
<th>ID</th>
<th>Name</th>
<th>Designation</th>
</tr>
<?php
If(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['cand_number']; ?></td>
<td><?php echo $row['cand_fname']; ?></td>
<td><?php echo $row['cand_desc']; ?></td>
</tr>
<?php
}
}
?>
</table>
</div>
You need to add into the while loop. Because of this your all data is printed inside first row.
<?php
If (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['cand_number']; ?></td>
<td><?php echo $row['cand_fname']; ?></td>
<td><?php echo $row['cand_desc']; ?></td>
</tr>
<?php
}
}
?>
In addition to the other answers: don't forget to sanitise $join and $condition.
This to prevent SQL injection.

Categories