PHP loop new tr after every 3 loops - php

I have a table with a PHP loop. I want it to create a new row after every 3 loops. I've got the following code. However, seems to not be working correctly. The first new row gets created after the 4th loop and every loop after that works fine. And also it seems to create a blank at the end. Any ideas how I can get this to work?
<table cellpadding="20">
<tr>
<?php
$counter=0;
foreach ($links as $key){
echo '<td align="center">'.$links[$key].'</td>';
echo "\n";
if ($counter % 3 == 0 && $counter !== 0) {
echo '</tr><tr>';
}
$counter++;
}
?>
</tr>
</table>

You can try
foreach(array_chunk($links, 3) as $linkGroup) { ?>
<tr>
<?php
foreach($linkGroup as $link) { ?>
<td><?= $link['key'] ?></td>
<?php }
?>
</tr>
<?php }

Move $counter++; before if or set $counter=1; before for

Try doing it like this:
<?php
$counter=0;
foreach ($links as $key){
echo '<td align="center">'.$links[$key].'</td>';
echo "\n";
}
if ($counter >= 3){
echo '</tr><tr>';
$counter=0;
}
$counter++;
}
?>

Related

PHP styling while for

Glad you are here. I need solution and I am kinda newbie in these things.
Right now I have page looking like this
1.text 2.text 3.text 4.text
but I need it to be like this
1.text 2.text
3.text 4.text
Code I have
<table >
<?php
for ($x = 1; $x <= 2; $x++): ?>
<tr>
<?php while($row = $choices->fetch_assoc()): ?>
<td><button class="pogaAtbilzuVarianti" value="<?php echo $row['id_var']; ?>" id="atbilde_a"><?php echo $row['teksts']; ?></button></td>
<?php endwhile; ?>
</tr>
<?php endfor; ?>
</table>
The for() loop is useless in this, because the while() loop inside it will process every row returned by the query.
You should just use the while loop, and then use a counter to tell whether to start a new <tr>.
<table>
<?php
$counter = 0;
while ($row = $choices->fetch_assoc()) {
if ($counter % 2 == 0) { // Start a new row before event elements
echo "<tr>";
}
?>
<td><button class="pogaAtbilzuVarianti" value="<?php echo $row['id_var']; ?>" id="atbilde_a"><?php echo $row['teksts']; ?></button></td>
<?php
if ($counter % 2 == 1) { // End row after odd elements
echo "</tr>";
}
$counter++;
}
if ($counter % 2 == 1) { // End last row if it only had 1 column
echo "</tr>";
}
?>
</table>
<table >
<?php while($row = $choices->fetch_assoc()):
if(($row['id_var'] % 2) == 1) echo '<tr>';
?>
<td><button class="pogaAtbilzuVarianti" value="<?php echo $row['id_var']; ?>" id="atbilde_a"><?php echo $row['teksts']; ?></button></td>
<?php
if(($row['id_var'] % 2) == 0) echo '</tr>';
endwhile; ?>
</table>

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) {
}
}

Generating table for database records

The following code will generate <tr></tr> on every $product->title
<table>
<?php foreach ($products as $product) {
echo '<tr>
<td>'.$product->title.'</td>
</tr>';
}?>
</table>
But I want to generate the row after every three columns as output of above code.
<table>
<tr>
<td>$product->title/td>
<td>$product->title/td>
<td>$product->title</td>
</tr>
<td>$product->title</td>
<td>$product->title</td>
<td>$product->title</td>
</tr>
</table>
I use this form so much, that it's committed to type memory.
<table>
<?php
$count = 0; // we gotta count them lines
foreach ($products as $product)
{
if ( ($count % 3) == 0 ) // every 3 lines
{
if ($count > 0) // not at first line
echo '</tr>'; // close previous row
echo '<tr>'; // open new row
}
++$count; // better count this one now.
echo '<td>'.$product->title.'</td>;
}
if ($count > 0)
echo '</tr>'; // close last row
?>
</table>
Perhaps something like this:
<table>
<?php
$count = count($products);
foreach ($products as $key => $product) {
// print on first row and third row
if($key % 3 == 0) {
echo '<tr>';
}
echo '<td>'.$product->title.'</td>';
// print on third row or on last element
if((($key + 1) % 3 == 0 && $key > 0) || $key == $count-1) {
echo '</tr>';
}
}
?>
</table>
If you array isn't indexed from 0 and up you would have to use a counter for the $key variable.
$i=1;
<table>
<?php foreach ($products as $product) {
if ( $i<= 3 ) {
if($i==1) {
echo '<tr>';
}
echo '<td>'.$product->title.'</td>';
$i++;
}
else {
echo'</tr>';
$i=1;
}
}?>
</table>

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>

How to display two table columns per row in php loop

I would like to display data, two columns per row during my foreach. I would like my result to look like the following:
<table>
<tr><td>VALUE1</td><td>VALUE2</td></tr>
<tr><td>VALUE3</td><td>VALUE4</td></tr>
<tr><td>VALUE5</td><td>VALUE6</td></tr>
</table>
Any help would be greatly appreciated.
You can use array_chunk() to split an array of data into smaller arrays, in this case of length 2, for each row.
<table>
<?php foreach (array_chunk($values, 2) as $row) { ?>
<tr>
<?php foreach ($row as $value) { ?>
<td><?php echo htmlentities($value); ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
Note that if you have an odd number of values, this will leave a final row with only one cell. If you want to add an empty cell if necessary, you could check the length of $row within the outer foreach.
$i=0;
foreach ($x as $key=>$value)
{
if (fmod($i,2)) echo '<tr>';
echo '<td>',$value,'</td>';
if (fmod($i,2)) echo '</tr>';
$i++;
}
this will output TR (row) each second time
ps: i haven't tested the code, so maybe you will need to add ! sign before fmod, if it doesn't output TR on first iteration, but on second iteration in the beginning...
This would give you great table and for loop concept--
<table border="1" cellspacing="0" cellpadding="2">
<?php
for($x=1; $x<=20; $x++)
{
echo "<tr>";
for($y=1; $y<=20; $y++)
{
echo "<td>";
echo $x*$y;
echo "</td>";
}
echo "</tr>";
}
?>
</table>
<table>
<?php
$i=0;
foreach ($x as $key=>$value)
{
if (!$i%2) echo '<tr>';
echo '<td>',$value,'</td>';
if ($i%2) echo '</tr>';
$i++;
}
?>
</table>

Categories