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>";
}
?>
Related
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++;
}
?>
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>
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 do it turn a multidimensional array like:
$fruits['apples']['blue'] = 24;
$fruits['bananas']['blue'] = 12;
$fruits['apple']['red'] = 34;
$fruits['gooseberries']['orange'] = 4;
$fruits['oranges']['red'] = 12;
into a cross referenced table like:
alt text http://1updesign.org/uploads/p24.png
$cols = array('blue', 'red', 'orange');
echo '<table>';
echo '<thead><tr><td></td><th scope="col">' . implode('</th><th scope="col">', $cols) . '</th></tr></thead>';
echo '<tbody>';
foreach($fruits as $label => $row)
{
echo '<tr>';
echo '<th scope="row">' . $label . '</th>';
foreach($cols as $k)
{
echo '<td>' . (isset($row[$k]) ? $row[$k] : 0) . '</td>';
}
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
You’ll want some HTML escaping and such, but that’s the gist of it.
Your array is bad designed. How should one know, that apples is the same as apple. Your array should be constructed this way:
$fruits['apples']['blue'] = 24;
$fruits['apples']['read'] = 24;
$fruits['bananas']['blue'] = 12;
$fruits['gooseberries']['orange'] = 4;
$fruits['oranges']['red'] = 12;
Then iterating is easy. The first level of the array are the rows. So:
<?php
$column_head = array();
foreach($fruits as $key => $value) {
$column_head = array_merge($column_head, array_keys($value));
}
$column_head = array_unique($column_head);
print_r($column_head);
?>
<table>
<tr>
<th></th>
<?php foreach($column_head as $head): ?>
<th><?php echo $head; ?></th>
<?php endforeach; ?>
</tr>
<?php foreach($fruits as $fruit => $amount): ?>
<tr>
<td><?php echo $fruit ?></td>
<?php foreach($column_head as $head): ?>
<td><?php echo isset($amount[$head]) ? $amount[$head] : 0 ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
This generates which columns to use on fly, but it can be hardcoded which might be easier. As the code uses a lot loops it might be not that efficient...
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>