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>
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'm trying create loop in my table, there is 4 item, when column is 3 then create new row . The current output is like this:
x
x
x
x
Here's my code:
<table border="0">
<?php
$i = 0;
foreach ($list_items as $item){ // there is 4 item
$i++;
echo "<tr>";
if ($i <= 3) { ?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
<br>
<br>
<?php echo $item['qty'] ?>
</td>
<?php }
}
echo "</tr>";
?>
</table>
What i expected is like this:
x|x|x
x
Thank you.
In the comment section of your question, Sirko is right.
Anyway you can do this like below;
<?php
$i = 0;
foreach ($list_items as $item) {
if($i % 3 == 0)
echo '<tr>';
echo '<td> bla bla bla </td>';
if($i % 3 == 0)
echo '</tr>';
$i++;
}
Change your code to below, it should work.
<table border="0">
<?php
$i = 0;
foreach ($list_items as $item){ // there is 4 item
$i++;
echo "<tr>";
if($i%3==0) echo echo "</tr><tr>";
?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
</td>
<td>
<?php echo $item['qty'] ?>
</td>
<?php
}
if($i%3!=0)
echo "</tr>";
?>
</table>
use array_chunk()
<?php
foreach (array_chunk($list_items,3) as $items) {
echo '<tr>';
foreach($items as $item){
?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
<br>
<br>
<?php echo $item['qty'] ?>
</td>
<?php
}
echo '</tr>';
}
?>
try this ==>
<table border="0">
<?php
$i = 0;
foreach ($list_items as $item) { // there is 4 item
if ($i % 3 == 0) // for i=0,3,6,9 <tr> tag will open
echo "<tr>";
?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
<br>
<br>
<?php echo $item['qty'] ?>
</td>
<?php
if ($i % 3 == 0) // for i=0,3,6,9 <tr> tag will close
echo "</tr>";
$i++;
}
?>
</table>
My problem is that my code always starts at the first position. So I can't give each line it's input field with the relating id.
foreach ($phase as $line) {
echo "<tr>";
echo "<td>";
echo "<div class='headliner-th'><b>".$line['title']."</b></div>";
echo "</td>";?>
<?php foreach($items as $i=>$item): ?>
<td><?= $form->field($item,"[$i]content")->textInput(['maxlength' => 200])->label(false); ?></td>
<?php endforeach; ?>
<?php echo "</tr>";
}
So how can I avoid to get the same input field again when it comes to a new row?
The inner foreach loop always loops over the same variable $items. Shouldn't it be something that is somehow cycled through by the outer foreach?
I did it with multiple if cases. It seems a bit dirty but easier than harass my object. So it looks like this: I hope anybody can use it: It is important to pre sort the object firstly on row than on col
<table class="sal-list" style="width: 100%;">
<tr>
<?php
$width = 100/(count($pos)+1);
echo "<th style='width:".$width."%;'></th>";
foreach ($pos as $item) {
echo "<th style='width:".$width."%;'><div class='headliner-th'>".$item['title']."</div></th>";
}
?>
</tr>
<?php
$g = 0;
foreach ($phase as $line) {
echo "<tr>";
echo "<td>";
echo "<div class='headliner-th'><b>".$line['title']."</b></div>";
echo "</td>";?>
<?php foreach($items as $i=>$item): ?>
<?php if (($g <= count($items)) and ($g == $i) and ($line['id'] == $item['phas_id'])) : ?>
<td><?= $form->field($item,"[$i]content")->textInput(['maxlength' => 200])->label(false); ?></td>
<?php $g++; endif;?>
<?php endforeach; ?>
<?php
echo "</tr>";
}
?>
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) {
}
}
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
} } ?>