Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I want to make a recapitulation of absent data, which I display using a table in CI. The problem that occurs to me is that the data that appears in the table is not in date order.
I want to make a report like this
enter image description here
The absence data does not match based on the date data above the table
How to display the data according to the date data that I called from the database?
Here is my code:
<tbody>
<tr class="text-center">
<td>Nip</td>
<td>Nama Lengkap</td>
<?php
foreach ($tampiltgl as $data) :?>
<td>
<?php echo $data['waktu']; ?>
</td>
<?php endforeach; ?>
</thead>
<tbody>
<?php
$no=1;
foreach ($tampil as $data) :?>
<tr>
<td><?php echo $data['nip']; ?> </td>
<td>
<?php echo $data['nm_lengkap']; ?>
</td>
<?php
date_default_timezone_set("Asia/Jakarta");
$timestamp = date('m');
$opd=$this->session->userdata('opd');
$tgl=$this->db->query ("SELECT DISTINCT waktu FROM absen_pagi where opd='$opd'AND MONTH(waktu) ='$timestamp'")->result_array();
$absen = $this->db->query ("SELECT DISTINCT waktu, status from absen_pagi where nip='".$data['nip']."' AND opd='$opd' AND MONTH(waktu) ='$timestamp'")->result_array();
foreach ($absen as $key) :?>
<td><?php echo $key['status']; ?> </td>
</td>
<?php endforeach; ?>
<?php endforeach; ?>
</tbody>
</table>
I think you're looking for this:
<table>
<thead>
<tr class="text-center">
<td>Nip</td>
<td>Nama Lengkap</td>
<?php
foreach ($tampiltgl as $data) :?>
<td>
<?php echo $data['waktu']; ?>
</td>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php
$no=1;
foreach ($tampil as $data) :?>
<tr>
<td><?php echo $data['nip']; ?> </td>
<td>
<?php echo $data['nm_lengkap']; ?>
</td>
<?php
date_default_timezone_set("Asia/Jakarta");
$timestamp = date('m');
$opd=$this->session->userdata('opd');
$tgl=$this->db->query ("SELECT DISTINCT waktu FROM absen_pagi where opd='$opd'AND MONTH(waktu) ='$timestamp'")->result_array();
$absen = $this->db->query ("SELECT DISTINCT waktu, status from absen_pagi where nip='".$data['nip']."' AND opd='$opd' AND MONTH(waktu) ='$timestamp'")->result_array();
foreach ($tampiltgl as $tampiltgldata) {
$celldata = '';
foreach ($absen as $key) {
if ($tampiltgldata['waktu'] == $key['waktu']) {
$celldata = $key['status'];
break;
}
}
echo "<td>$celldata</td>";
} ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
When displaying the $absen data from the database, loop over the $tampiltgl array that you used to print the dates in the header.
For each date in the $tampiltgl array, check if there is an entry in the $absen array with the same date. If a match is found, print the status. If no match was found print an empty table cell.
Related
I tried to create a table similar to the sporting tables. There are 8 group in this table. (A-H)
There are 4 teams for each group. I tried to select all the teams with the SQL code below, And sort them by group name
SELECT *
FROM teams
ORDER BY groupName
I want the results to be displayed in a panel that contains a html table.
below code:
<?php
while($row = $result->fetch_assoc()){
?>
<div class="flip">
<h3>Group <?php echo($row['groupName']);?></h3>
</div>
<div class="panel">
<table>
<thead>
<tr>
<th>#</th>
<th>Team</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo($row['team_pos']); ?></td>
<td><?php echo($row['teamName_Pr']); ?></td>
</tr>
</tbody>
</table>
</div>
<?php
}
?>
But with every repetition of the while loop, the group name repeats for each team.
How can I display all the groups teams in a specific table?
Also, when I use GROUP BY instead of ORDER BY, only one team is displayed per group.
My connection method is mysqli.
You need to accumulate the rows by the groupName. Don't use GROUP BY as it will not return all rows.
<?php
//$result = ...
$groups = [];
while($row = $result->fetch_assoc()){
#$groups[$row['groupName']][] = $row;
}
foreach ($groups as $groupName => $rows) {
?>
<div class="flip">
<h3>Group <?php echo($groupName);?></h3>
</div>
<div class="panel">
<table>
<thead>
<tr>
<th>#</th>
<th>Team</th>
</tr>
</thead>
<tbody>
<?php
foreach ($rows as $row) {
?>
<tr>
<td><?php echo($row['team_pos']); ?></td>
<td><?php echo($row['teamName_Pr']); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<?php
}
Btw, please consider using a template engine.
How can i show data in two row separately as shown in image
I have tried this but i am not getting what i want. Also i don't want to use two loops separately.
<table class="tbl1">
</thead>
<tbody>
<tr>
<td>
<h1> date</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++;?>
<td>
<?php echo $value[ 'date']; ?>
<?php } ?>
</tr>
<tr>
<td>
<h1>Status</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++; ?>
<td>
<?php echo $value[ 'status']; ?>
</td>
<?php } ?>
</tr>
</tr>
</tbody>
</table>
Do a single loop and in that loop include 2 <tr>s for date and status.
<table class="tbl1">
<tbody>
<?php foreach ($student as $value) { ?>
<tr>
<td><h1> date</h1> </td>
<td><?php echo $value['date']; ?>
</tr>
<tr>
<td><h1>Status</h1></td>
<td><?php echo $value['status']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Also i don't see the purpose of using $i here and what about the rest of the column in front of date and status.
Or if you want only 2 rows and display all data column wise then you could do it like
<?php
$dateHTML = '';
$statusHTML = '';
foreach ($student as $value) {
$dateHTML .= '<td>'.$value['date'].'</td>';
$statusHTML .= '<td>'.$value['status'].'</td>';
}
?>
<table class="tbl1">
<tbody>
<tr>
<td><h1> date</h1> </td>
<?php echo $dateHTML;?>
</tr>
<tr>
<td><h1>Status</h1></td>
<?php echo $statusHTML;?>
</tr>
</tbody>
</table>
Ok, I have a variable called $total_bal that is the answer of a simple equation made from two queries stored in variables x and y
for example
$y = $row->amount_one;
$z = $row->amount_two;
$total_bal = $z + $y;
However I have many entries in amount_one and amount_two.
As I am using codeigniters active records
I tried
echo $this->db->count_all($total_bal);
But this dose not work, any idea of the best way to do this ?
So Im after a way to add all the $total_balup, for more incite into my code see bellow.
<?php
if (isset($records)) : foreach ($records as $row) :
$x = $row->amount_two;
$y = $row->ammount_one;
$total_bal = $z + $y;
?>
<table>
<tbody>
<tr>
<td>amount one</td>
<td>amount two</td>
</tr>
<tr>
<td>
<?php echo $x;?>
</td>
<td>
<?php echo $y;?>
</td>
<td>
<?php echo $$total_bal;?>
</td>
</tr>
<!-- <tr>-->
<!-- <td>-->
<!-- --><?php //echo $this->db->count_all('$total_bal'); ?>
<!-- </td>-->
<!-- </tr>-->
</tbody>
</table>
<?php endforeach; ?>
<?php else : ?>
<h3>You Have No Accounts</h3>
<h4>Why No Add A Account?</h4>
<?php endif; ?>
One way to do this is to use an accumulator variable. Take for example, a $grandTotal variable. You set it to 0 outside the foreach, over each iteration of the loop, you add the $rowTotal to the $grandTotal. Eventually when the loop ends, you have a total value of all row totals.
The benefit to this method is that it doesn't require any additional calls to the database and since you are already looping through the values to display them, accumulating them is minimal processing.
<?php if (isset($records)) : ?>
<table>
<thead>
<tr>
<th>Amount One</th>
<th>Amount Two</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php $grandTotal = 0; ?>
<?php foreach ($records as $row) : ?>
<?php
// Add field values to get row total
$rowTotal = $row->amount_one + $row->amount_two;
?>
<?php
// Add row total to grand total
$grandTotal += $rowTotal;
?>
<tr>
<td>
<?php echo $row->amount_one;?>
</td>
<td>
<?php echo $row->amount_two;?>
</td>
<td>
<?php echo $rowTotal;?>
</td>
</tr>
<?php endforeach; ?>
<tr>
<td></td>
<td></td>
<td><?php $grandTotal; ?></td>
</tr>
</tbody>
</table>
<?php else : ?>
<h3>You Have No Account</h3>
<h4>Why Not Add An Account?</h4>
<?php endif;?>
I am using datatables and generating my table using a foreach, but I need one last to contain the options (edit, etc). I cannot think how to most efficiently do this, I can think to do it using a counter and then when the output has ended place an if statement (rows = rows then) to put these last td's in... I am hoping someone has a better way using my current syntax?
<?php foreach($results as $row):?>
<tr>
<?php foreach($row as $cell): ?>
<td>
<?php echo $cell ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
you can put then after internal for like this:
<?php foreach($results as $row):?>
<tr>
<?php foreach($row as $cell): ?>
<td>
<?php echo $cell ?>
</td>
<?php endforeach; ?>
<td>edit </td> <td>show</td><td>delete</td>
</tr>
<?php endforeach; ?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
<tbody>
<?php foreach($result as $row){ ?>
<?php foreach($result1 as $row1) {?>
<tr>
<td >
<span><?php echo $row->state; ?></span>
</td>
<td >
<span><?php echo $row->breakdown_grants; ?></span>
</td>
<td >
<span><?php echo $row1->breakdown_grants; ?></span>
</td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
I need to display my coding in table format.in that first foreach contain state and breakdown_grants value.
state actual provisional
$row->state $row->breakdown_grants $row1->breakdown_grants
this is my table format.what i need to change in my foreach.in $result and $result1 i'll get different data.
Better you use for loop like
<?php for($i=0;$i<count($result);$i++) { ?>
<tr>
<td>
<span><?php echo $result[$i]->state; ?></span>
</td>
<td>
<span><?php echo $result[$i]->breakdown_grants; ?></span>
</td>
<td>
<span><?php echo $result1[$i]->breakdown_grants; ?></span>
</td>
</tr>
<?php } ?>
Make sure that $result and $result1 are of same indexes and of same lengths.