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) {
}
}
Related
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>";
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 }?>
This question already has answers here:
How to float 3 divs side by side using CSS?
(16 answers)
Closed 6 years ago.
My table has rows that's looped in a non-specific length because the values in the cells may be added or removed anytime. Anyway, here's the code:
<?php
$i = 1;
foreach($items as $item => $itemValue) {
if ($itemValue['item_id'] == $parentItemValue['id']) {
if (fmod($i,7)) echo '<tr>';
echo '<td class="inner-td"><input type="checkbox" id="itemId">'.$itemValue['item'].'</td>';
if (!fmod($i,7)) echo '</tr>';
$i++;
}
?>
The above code displays this:
I also tried if (!fmod($i,7)) echo '<tr>' and if (!fmod($i,8)) echo '</tr>' and gives me this:
Also, if (!fmod($i,10)) echo '<tr>' and if (!fmod($i,11)) echo '</tr>' and gives me this:
I want my table to look like this:
Is there a way that the cells will fill in the entire row before making a new one?
You can try this. Just change the $maxcol value for how many columns you want.
<?php
$tmp = array('test1','test2','test3','test4');
echo '<table border="1">';
$x = 0;
$maxcol = 2; // Max column
foreach($tmp as $i=>$v)
{
echo $x === 0 ? '<tr>' : '';
echo '<td>'.$v.'</td>';
echo $x === ($maxcol-1) ? '</tr>' : '';
$x++;
$x = $x == $maxcol ? 0 : $x;
}
echo '</table>';
?>
Try to follow this structure.
Note that all fields and rows are made up by myself.
<tbody>
<?php
require_once ('connectionWithDB.php');
$table = "members";
$array = $admin_query->viewTableData($table);
foreach ($array as $row){
print_r($array);
?>
<tr>
<td> <?php $row[member_id] ?> </td>
<td> <?php $row[member_password] ?> </td>
<td> <?php $row[member_first_name] ?> </td>
<td> <?php $row[member_last_name] ?> </td>
<td> <?php $row[member_DOB] ?> </td>
<td> <?php $row[member_address] ?> </td>
<td> <?php $row[member_email] ?> </td>
<td> <?php $row[member_phone] ?> </td>
<td> <?php $row[member_gender] ?> </td>
</tr>
</tbody>
how to limit this loop ..just thee loops..thanks for helping
<?php
foreach($section['Article'] as $article) :
?>
<tr>
<td>
<?php
if ($article['status'] == 1) {
echo $article['title'];
}
?>
</td>
<td>
<?php
if($article['status']== 1) {
echo ' '.$html->link('View', '/articles/view/'.$article['id']);
}
?>
</td>
</tr>
<?php
endforeach;
?>
Slice the array.
foreach(array_slice($section['Article'], 0, 3) as $article ):
first, prepare your data
$i = 1;
$data = array();
foreach($section['Article'] as $article ) {
if($article['status']== 1) {
$article['link'] = $html->link('View', '/articles/view/'.$article['id']);
$data[] = $article;
if ($i++ == 3) break;
}
}
$section['Article'] = $data;
then display it
<?php foreach($section['Article'] as $article ): ?>
<tr>
<td><?php echo $article['title'] ?></td>
<td> <?php echo $article['link']?></td>
</tr>
<?php endforeach ?>
This will help if your array is numerically indexed
foreach($section['Article'] as $i => $article ):
if ($i > 3) break;
Otherwise - manually increment the counter:
$i = 0;
foreach($section['Article'] as $article ):
if ($i++ > 3) break;
It'd be easier to use a for() loop to do this, but to answer the question:
<?
$i = 0;
foreach ($section['Article'] AS $article):
if ($i == 3) { break; }
?>
...
<?
$i++;
endforeach
?>
A foreach loop wouldn't be the best if you need to limit it. Try using a for loop.
<?php
for(i=1; i<=3; i++)
{
$article = $section['Article'];
?>
<tr>
<td><?php if($article['status']== 1){echo $article['title'];} ?></td>
<td><?php if($article['status']== 1){echo ' '.$html->link('View', '/articles/view/'.$article['id']);}?></td>
</tr>
<?php } ?>
This code will make the text loop 3 times.
Awesome one must try this one
<?php $count = 0; $pages = get_pages('child_of=1119&sort_column=post_date&sort_order=desc'); foreach($pages as $page) {
$count++;
if ( $count < 50) { // only process 10 ?>
<div class="main_post_listing"> <?php echo $page->post_title ?><br /></div>
<?php
} } ?>
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>