php foreach combine 2 array into 1 row (workdays & data) - php

I have a problem with my current "Attendance Project", so I have 2 arrays.
1st array is to show a "workdays"
the 1st array show only workdays in current month ex:April, so the result in my 1st array is (3,4,5,6,7,10,11,12,13,14,17,18,19,20,21,24,25,26,27,28)
2nd array is showing Employee Attendance in current month ex:April, so the result in my 2nd array is (17, 19)
here is my current code :
<table class="table table-striped table-bordered zero-configuration">
<thead>
<tr>
<th style="width: 200px">Siswa</th>
<!-- <?php for($i = 1; $i < 31; ++$i){?>
<th><?= $i ?></th>
<?php } ?> -->
<?php foreach($workdays as $w){ ?>
<th><?=$w;?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php
// for($x = 1; $x < 27; ++$x){
foreach($records as $r){
?>
<tr>
<td style="width: 200px"><?=$r->StudentName;?></td>
<?php
?>
<?php
foreach($workdays as $w){
foreach($tanggale as $t){
if($w == $t){
?>
<td style="background: #FFF000">M</td>
<?php }else{ ?>
<td style="background: #48C9A9">O</td>
<?php } } } ?>
</tr>
<?php } ?>
</tbody>
</table>
It will produce :
I want value (17 and 19) will markup the data with yellow background, and the table is NOT out of range.
Any help will appreciate..

Your code seems messy and I'm not gonna try to fix it on what you have, but I'll suggest solution:
1st - run foreach ($workdays as $w) and make header
2nd - run foreach ($workdays as $w) and make table-body like:
foreach ($workdays as $w) {
if (in_array($w, $tanggale)) //if tanggle is the one with 17 and 19
{
//code
}
else
{
//code
}
}

Simply what u can do is combine the 2 arrays in to one and then iterate the combine array as per your requirment.
Check below code for combining the array
<?php
$working_days = array(3,4,5,6,7,10,11,12,13,14,17,18,19,20,21,24,25,26,27,28);
$present_days = array(17.19);
$combine_attendence_array = array();
foreach($working_days as $day) {
$combine_attendence_array[$day] = 'Absent';
if(in_array($day, $present_days)) {
$combine_attendence_array[$day] = 'Present';
}
}
?>
This code will create combine array with key as day and value is present or Absent.
Now you can iterate as per your requirement below is the iteration code.
foreach($combine_attendence_array as $day => $value){
if($value == 'Present'){ ?>
<td style="background: #FFF000">M</td>
<?php }else{ ?>
<td style="background: #48C9A9">O</td>
<?php } ?>
<?php } ?>
I hope this answers solves your question.

Do it in this way
<?php
foreach($tanggale as $t){
if(in_array($t,$workdays)){
?>
<td style="background: #FFF000">M</td>
<?php }else{ ?>
<td style="background: #48C9A9">O</td>
<?php } } ?>
</tr>
<?php } ?>

foreach($workdays as $w){
foreach($tanggale as $t){
if($w == $t){
$color = "#FFF000";
$text = "M";
} else {
$color = "#48C9A9";
$text = "O";
}
}
?>
<td style="background: <?php echo $color; ?>"><?php echo $text; ?></td>
<?php }?>

Related

PHP Foreach cycle echoes wrong Bootstrap table structure

I'm trying to populate a Bootstrap table in order to have a table like this:
Table Example
The final result should be that i can see which student will be present on that day.
Now I've tried to cycle the result of my database queries and put them inside the table tr and td but I'm missing how to do it properly...and the code looks very confusing...
If i do this way:
<?php
if($usersMON) {
foreach ($usersMON as $userlun){ ?>
<tr>
<td class="text-center maiuscolo"><?=$userlun['name']?></td>
<?php
}
} ?>
<?php
if($usersTUE) {
foreach ($usersTUE as $usermar){ ?>
<td class="text-center maiuscolo"><?=$usermar['name']?></td>
<?php
}
}
?>
<?php
if($usersWED) {
foreach ($usersWED as $usermer){ ?>
<td class="text-center maiuscolo"><?=$usermer['name']?></td>
<?php
}
}
?>
<?php
if($usersTHU) {
foreach ($usersTHU as $usergio){ ?>
<td class="text-center maiuscolo"><?=$usergio['name']?></td>
<?php
}
}
?>
<?php
if($usersFRI) {
foreach ($usersFRI as $userven){ ?>
<td class="text-center maiuscolo"><?=$userven['name']?></td>
<?php
}
}
?>
</tr>
I obtain this:
Table result
Could you suggest me a proper way to do it?
The prerequisite is that the user quantity is the same.
You could put the $usersMON ~ $usersFRI into an array.
array_push($allUsers, $usersMON, usersTUE, usersWED, usersTHU, usersFRI);
Then, show each student's name
<?php for ($i=0; $i < $userQuantity; $i++) { ?>
<tr>
<?php foreach ($allUsers as $users) { ?>
<td><?=$users[$i];?></td>
<?php } ?>
</tr>
<?php } ?>
$users = [
$usersTUE,
$usersWED,
$usersTHU,
$usersFRI
];
$col_count = count($users);
$row_count = 0;
foreach ($users as $u) {
$row_count = max($row_count, count($u));
}
echo "<table>";
for ($i=0; $i<$row_count; $i++) {
echo "<tr>";
for ($t=0; $t<$col_count; $t++) {
$user = $users[$t][$i] ?? [];
echo "<td class='text-center maiuscolo'>" . $user['name'] . "</td>";
}
echo "</tr>";
}
echo "</table>";

How to merge tabular data in PHP

I have a table with weeks of a year and some data adjacent to each week, and i can't seem to merge the values correctly. Week rows with no data should stay empty
This is where i list the weeks
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td><?php echo $leadCount->theweek ?>
/ <?php echo $leadCount->theyear ?></td>
</tr>
<?php }
And this is where i try to merge
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td style="text-align: center">
<?php if ($getFtdCountDMm[$index]->theweek == $leadCount->theweek && !empty($getFtdCountDMm[$index]->ftdcount)) {
echo $getFtdCountDMm[$index]->ftdcount;
} else {
echo '0';
} ?>
</td>
</tr>
<?php } ?>
it should look like this
but it looks like this
Instead of running two loops run one loop like this.
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td><?php echo $leadCount->theweek ?>
/ <?php echo $leadCount->theyear ?>
</td>
<td style="text-align: center">
<?php if ($getFtdCountDMm[$index]->theweek == $leadCount->theweek && !empty($getFtdCountDMm[$index]->ftdcount)) {
echo $getFtdCountDMm[$index]->ftdcount;
} else {
echo '0';
} ?>
</td>
</tr>
<?php }

Having issue in my for loop using PHP, I want to create dynamic rows and column in PHP, Each row has 10 column

I am Having an issue in my for loop using PHP, I want to create dynamic row and column, Each row has 10 column after 10 column, the second row also end with 10 column like this up to 5 row, how to do It for a loop.
My for loop code:
<table width="100%" border="1">
<?php
for($i=1; $i<=72; $i++)
{
?>
<tr>
<td width="100%">
<?php echo "Click Here to see Site No.'".$i."'. & Area sqft No" .$i;?></a></td>
</tr>
<?php
}
?>
</tr>
</table>
I tried like this also
<table width="100%">
<tr>
<?php
for($i=1; $i<=72; $i++)
{
$x = 10;
if ($i % $x == 0)
{
?>
<td><?php echo $i;?></td>
<?php
}
}
?>
</tr>
</table>
<?php
echo '<table width="100%" border="1">';
for($i=1; $i<=8; $i++)
{
$y=10;
$y*=($i-1);
echo '<tr>';
for ($x=1; $x <=10; $x++) {
if ($i==1) {
echo '<td>'.$x.'</td>';
}else{
$y+=$x;
echo '<td>'.$y.'</td>';
if ($y==72) {
break;
}
$y-=$x;
}
}
echo '</tr>';
}
echo '</table>';
This will print the bellow table:
As per what I understand you want simple 10 column in each row.
Here is my code which may help you:
<table style="border:1px solid #000">
<tr>
<?php $t=1; for($k=1;$k<=72;$k++){?>
<?php if($t == 10) { $t=0;?><td style="border:1px solid #000"> <?php echo $k; ?> </tr><?php } else {?><td style="border:1px solid #000"> <?php echo $k; ?></td><?php } ?>
<?php $t++;}?>
<?php $x = 10; if ($i % $x == 0) { ?>
....
<?php } ?>
If I am getting the question correct, why don't you do as below:
echo '<table>';
for($i=1; $i<=5; $i++) {
echo '<tr>';
for ($y=1; $y<=10; $y++) {
echo '<td>Row_'.$i.' - Col_'.$y.'</td>';
}
echo '</tr>';
}
echo '</table>';
It will print something like below:

php foreach break table

Im using tables to store content that's dynamically loaded. It's for a reservation form which will be responsive. What I'm looking to do is break each table row into two if there are more than 5 columns in order for the mobile version to fit on screen.
I'm sure this can be achieved by extending what I already have but can't get it to work.
Here's my current code:
<table>
<tr>
<?php foreach ($hostel->getAvailableDates() as $date): ?>
<th><?php echo $date->getDayOfTheWeek(); ?></th>
<?php endforeach ?>
</tr>
<tr>
<?php foreach ($hostel->getAvailableDates() as $date): ?>
<td>
<?php if($date->getAvailable()) { ?>
<b class="avail tick">Available</b>
<?php } else { ?>
<b class="avail cross">Unavailable</b>
<?php }?>
</td>
<?php endforeach ?>
</tr>
</table>
I'd need to break the loop for each row tr after 5 loops, then add a new row underneath.
I've been experimenting with
$max_loop = 5;
$count = 0;
But no luck so far.
I prefer to reorganize data:
<?php
$availDates = array();
foreach ($hostel->getAvailableDates() as $date) {
$availDates[] = $date;
}
$maxCols = 5;
$chunked = array_chunk( $availDates, $maxCols );
?>
<table>
<?php
foreach ($chunked as $chunk) {
?><tr>
<?php foreach ($chunk as $date): ?>
<th><?php echo $date->getDayOfTheWeek(); ?></th>
<?php endforeach; ?>
</tr>
<tr>
<?php foreach ($chunk as $date): ?>
<td>
<?php if($date->getAvailable()) { ?>
<b class="avail tick">Available</b>
<?php } else { ?>
<b class="avail cross">Unavailable</b>
<?php }?>
</td>
<?php endforeach; ?>
</tr><?php
}
?>
</table>
Look at the mod operator. It should give you what you need.
if($count % $max_loop == 0)
I hope this may help you. thanks.
<?php
$avDates = $hostel->getAvailableDates();
echo "<table><tr>";
foreach($avDates as $i=>$date){ {
if ($i == $max_loop) {
echo "</tr><tr>";
}
echo "<td>".($date->getAvailable() ? '<b class="avail tick">Available</b>' : '<b class="avail cross">Unavailable</b>')."</td>";
}
echo "</tr></table>";
?>
If the value returned by getAvailableDates is an array, you could use a for loop instead of a foreach, and check if the current index is a multiple of five, so you don't have to keep track of the count variable
$avDates = $hostel->getAvailableDates();
for ($i = 0; $i < count($avDates); $i++) {
$date = $avDates[$i];
//do your staff
//if multiple of five add another tr
if ($i % 5 == 0) {
}
}

Multiple rows with PHP foreach?

I am trying to make a table with 4 rows from a foreach-call.
My problem is, that in the result I get each ID twenty times in the same column.
I'm using this code:
<table width="80%" border="0" cellpadding="10px">
<?php foreach (array_chunk($items, 4) as $row) { ?>
<?php
$i = 0;
foreach ($items as $item):
$class = null;
if ($i++ % 2 == 0) {
$class = ' class="altrow"';
} ?>
<tr
<?php echo $class;?>
>
<?php foreach ($row as $item){ ?>
<td>
<?php echo htmlentities ($item['Item']['id']); ?>
</td>
<?php } ?>
<?php endforeach; ?>
</tr>
<?php } ?>
</table>
Any idea how I could get each ID just once?
You are incrememnting $i at every $item as opposed to every $row
Is this the fix you are looking for?
Edit: Mikel has your fix, add this to fix the row bug (Typical of me to notice that first eck!)
<table width="80%" border="0" cellpadding="10px">
<?php
$i = 0;
$chunkedarray = array_chunk($items, 4);
foreach ($chunkedarray as $row) {
$class = null;
if ($i++ % 2 == 0)
$class = ' class="altrow"';
echo "<tr ".$class.">";
foreach ($row as $item){
echo "<td>";
echo htmlentities ($item['Item']['id']);
echo "</td>";
}
echo "</tr>";
}?>
</table>

Categories