Showing one value outside the table from joined tables in Codeigniter - php

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

Related

DataTables - PHP while loop issue can't find the last result and add new TR

I am using DataTables and I want to add new TR at the end of while loop.
I know we can add <tfoot></tfoot>, but I don't want to add '' because I am filtering data with custom Ajax.
I have tried below code but it's not working:
<?php
$Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
<thead>
<tr>
<th>Group</th>
<th>Party Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody id="getGroups">
<?php
$i = 1;
while($row = mysqli_fetch_array($sdetails)){
$totalAmount += $row['total_debtors'];
$i++;
?>
<tr>
<td><?php echo getAreaName($row['area_id']); ?></td>
<td><?php echo GrabAccountIDName($row['client_id']); ?></td>
<td><?php echo abs($row['total_debtors']); ?></td>
</tr>
<?php if( $i == ( $numRows - 1 ) ) { ?>
<tr>
<td> </td>
<td style="text-align:right">Total:</td>
<td><?php echo abs($totalAmount); ?></td>
</tr>
<?php } } ?>
</tbody>
</table>
Also, when I use <tfoot></tfoot> it's not printable.
Probably your problem is in $numRows which is not defined.
So you can try this:
<?php
$Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
$numRows = mysqli_num_rows($Itesres);
while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
<thead>
<tr>
<th>Group</th>
<th>Party Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody id="getGroups">
<?php
$i = 1;
while($row = mysqli_fetch_array($sdetails)){
$totalAmount += $row['total_debtors'];
$i++;
?>
<tr>
<td><?php echo getAreaName($row['area_id']); ?></td>
<td><?php echo GrabAccountIDName($row['client_id']); ?></td>
<td><?php echo abs($row['total_debtors']); ?></td>
</tr>
<?php if( $i == ( $numRows - 1 ) ) { ?>
<tr>
<td> </td>
<td style="text-align:right">Total:</td>
<td><?php echo abs($totalAmount); ?></td>
</tr>
<?php } } ?>
</tbody>
</table>

show multiple rows for each result value

I have two tables one for products and another for items.
I want to show all items details for each product in one table like this:
I have tried to make a nested while loops but the result not as I want.
<table border="1">
<thead>
<th>Product No.</th>
<th>Product Name</th>
<th>T.Qty</th>
<th>Item No.</th>
<th>Item Name</th>
<th>Qty </th>
</thead>
<tbody>
<tr>
<td><?php echo $Product_no ?></td>
<td><?php echo $Product_name?></td>
<td><?php echo $TQty ?></td>
<?php
// my problem is here
$Items= $connect->prepare("Query Statment?");
$Items->execute();
$res = $Items->get_result();
while($GetItems = $res->fetch_assoc()){
?>
<td><?php echo GetItems['Item_no'];?></td>
<td><?php echo GetItems['Item_name']; ?></td>
<td><?php echo GetItems['Qty']; ?></td>
<?php } ?>
</tr>
</tbody>
</table>
but the items displayed beside each other not below.
You have a problem in your html table code. You should close the tag for each row you have, and, in case it is not the first line for that element, insert 3 cells with no data:
<tbody>
<tr>
<td><?php echo $Product_no ?></td>
<td><?php echo $Product_name?></td>
<td><?php echo $TQty ?></td>
<?php
// my problem is here
$Items= $connect->prepare("Query Statment?");
$Items->execute();
$res = $Items->get_result();
$i=0;
while($GetItems = $res->fetch_assoc()){
if ($i!=0){
echo "<td></td><td></td><td></td>";
}
?>
<td><?php echo GetItems['Item_no'];?></td>
<td><?php echo GetItems['Item_name']; ?></td>
<td><?php echo GetItems['Qty']; ?></td>
<?php
echo "</tr>";
$i++;
} ?>
</tbody>

Difference between values in while loop

I am trying to compute the differences between array values in PHP in a while loop.
Initially I was using a foreach loop using (next) and (prev) but it didn't seem to work very well.
What I am trying now actually works but the problem is the value that is calculated should actually appear on the next line of the table.
Here is the table and HTML:
<table table id="meter_entries" class="table table-striped table-hover dt-responsive"><thead>
<tr>
<th>ID</th>
<th>Date</th>
<th>Company Code</th>
<th>Oil Height</th>
<th>Water Height</th>
<th>Total Volume</th>
<th>Difference</th>
<th>Actions</th>
</tr>
</thead>
<?php
$s = $stock_automatic; $i=0; $count = count($s);
while($i < $count){
?>
<tr>
<td><?php echo $s[$i]['id']; ?></td>
<td><?php echo $s[$i]['timess']; ?></td>
<td><?php echo $s[$i]['company_code']; ?></td>
<td><?php echo $s[$i]['oil_height']; ?></td>
<td><?php echo $s[$i]['water_height']; ?></td>
<td><?php echo $s[$i]['total_volume']; ?></td>
<td><?php $oldvol = $s[$i-1]['total_volume']; $currvol = $s[$i]['total_volume']; $diff = $oldvol - $currvol; echo $diff; ?></td>
<td>
<span class="fa fa-pencil"></span> Edit
<span class="fa fa-trash"></span> Delete
</td>
</tr>
<?php
$i++;
}
?>
</table>
Here is the output:
It seems that your problem is that you are using datatables and your array $s is actually ordered the opposite way than it is displayed in your example.
To get the correct values you should change your code to the following
<?php if ($i==$count-1){
echo '0';
}
else {
$oldvol = $s[$i+1]['total_volume']; $currvol = $s[$i]['total_volume'];
$diff = $currvol - $oldvol; echo $diff;
} ?>

Display the rows from query in table

I have this code to get all the data from my products table. It's working great like this:
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row['id'];
echo $row['country'];
echo $row['price'];
echo $row['games'];
echo $row['plus'];
echo $row['buylink'];
echo "<br/>";
}
But I want these results to be shown in a table inside the body. I tried something but it's not working.
This is how the table looks :
<table class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>ID</th>
<th>Country</th>
<th>Price</th>
<th>Games</th>
<th>Plus</th>
<th>Buy Link</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['country']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><?php echo $row['games']; ?></td>
<td><?php echo $row['plus']; ?></td>
<td><?php echo $row['buylink']; ?></td>
</tr>
</tbody>
<?php
if ($result->num_rows > 0) {
?>
<table class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>ID</th>
<th>Country</th>
<th>Price</th>
<th>Games</th>
<th>Plus</th>
<th>Buy Link</th>
</tr>
</thead>
<tbody>
<?php while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['country']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><?php echo $row['games']; ?></td>
<td><?php echo $row['plus']; ?></td>
<td><?php echo $row['buylink']; ?></td>
</tr>
<?php }?>
</tbody>
</table>
<?php
}else {
// if there is no result... Code something here.
}
?>
Youre getting close. You need to have the loop spit out the rows into the html.
<table class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>ID</th>
<th>Country</th>
<th>Price</th>
<th>Games</th>
<th>Plus</th>
<th>Buy Link</th>
</tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_row()) {
echo "<tr>";
foreach ($row as $item) {
echo "<td>$item</td>";
}
echo"</tr>";
}
?>
</tbody>
also instead of using a associative array and indexing into it (ie. $row['country']), You can use a normal array and a foreach loop to cut down on the code.
LE:
Each html table row is created based on each database's table row. So, you just need to loop through the result set (that while), and for each iteration of the loop, you need to create a new html table row:
<table class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>ID</th>
<th>Country</th>
<th>Price</th>
<th>Games</th>
<th>Plus</th>
<th>Buy Link</th>
</tr>
</thead>
<tbody>
<?php while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['country']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><?php echo $row['games']; ?></td>
<td><?php echo $row['plus']; ?></td>
<td><?php echo $row['buylink']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>

how to looping array string with increment on table

I have an array like this
$Peringkat = array("Pertama","Ke-dua","Ke-tiga","Ke-empat","Ke-Lima");
and a table like this
<table class="table table-striped table-bordered text-center">
<tr>
<th>No</th>
<th>Nis</th>
<th>Nama</th>
<th>Kelas</th>
<th>Nilai Rata-rata</th>
<th>Nama Sekolah</th>
<th>Peringkat</th>
</tr>
<?php foreach ($Peringkat as $key => $value) {
while ($baris = $sql->fetch_assoc()) { ?>
<tr>
<td><?php echo $n++ ?></td>
<td><?php echo $baris['nis'] ?></td>
<td><?php echo $baris['nama_siswa'] ?></td>
<td><?php echo $baris['nama_kelas'] ?></td>
<td><?php echo $baris['Hasil'] ?></td>
<td><?php echo $baris['nama_sekolah'] ?></td>
<td><?php echo $value ; ?></td>
</tr>
<?php } }?>
</table>
How would I make a loop in ascending order to the value of the data string?
To sort your array of strings in ascending order, use asort
$Peringkat = array("Pertama","Ke-dua","Ke-tiga","Ke-empat","Ke-Lima");
asort($Peringkat); //ascending order
Now you can proceed as before.
Live demo
Note that if you sort $Peringkat, your table rows will be the same as before with the only difference being the last cell of each row. Depending on the nature of your $sql data, this could result in a mismatch.

Categories