How can iterate every 5 results in a table? - php

I have a foreach that need to be distributed every 5 result per row on a table.
This is my current foreach:
<?php
$i = 0;
echo "<tr>";
foreach($klasifikasi as $result){
$i++;
if($i % 5 == 0){
echo "<td width='20%' align='center'>".$result['klasifikasi']."</td>";
echo "<td width='20%' align='center'>".$result['klasifikasi']."png</td>"; }
};
echo "</tr>";
?>
It doesn't stop every 5 result and doesn't create new row.
This is what I'm looking for:
---------------------------------------------------
A1 | A2 | A3 | A4 | A5
---------------------------------------------------
A1.png | A2.png | A3.png | A4.png | A5.png
---------------------------------------------------
/*this row should be empty for some spacing*/
---------------------------------------------------
.../*skipped till the last row*/
---------------------------------------------------
A21 | A22 | A23 | A24
---------------------------------------------------
A21.png | A22.png | A23.png | A24.png
---------------------------------------------------
My current result from the iteration is 24 data. It may become more or less
note:
if possible, every columns that are not empty should have widht of 20%

A simple solution to your problem:
$i = 1;
$html = "<table border='1'>";
$row1 = '<tr>';
$row2 = '<tr>';
foreach($klasifikasi as $result){
$row1 .= "<td width='20%' align='center'>".$result['klasifikasi']."</td>";
$row2 .= "<td width='20%' align='center'>".$result['klasifikasi']."png</td>";
if($i > 0 && $i % 5 === 0){
$row1 .= '</tr>';
$row2 .= '</tr>';
$html .= $row1.$row2;
$html .= '<tr><td colspan="5"></td></tr>;
$row1 = '<tr>';
$row2 = '<tr>';
}
$i++;
}
if($i > 0 && $i % 5 !== 1) {
$html .= $row1.$row2;
}
$html .= '</table>';
echo $html;

Use nested loops. The outer loop should iterate by 5, and the inner loop gets the 5 items within that group.
$len = count($klasifikasi);
for ($i = 0; $i < $len; $i += 5) {
echo "<tr>";
for ($j = $i; $j < $i+5; $j++) {
if ($j < $len) {
echo "<td>{$row['klasifikasi']}</td>";
}
}
echo "</tr>";
echo "<tr>";
for ($j = $i; $j < $i+5; $j++) {
if ($j < $len) {
echo "<td>{$row['klasifikasi']}.png</td>";
}
}
echo "</tr>";
}

Related

How to break a column to fix a php table [duplicate]

This question already has answers here:
PHP: How do you determine every Nth iteration of a loop?
(8 answers)
Closed last year.
I had a little problem, i cannot break the table because I'm looking for a 16 columns in these table to create a unicode table...
The aim is to find where i must break the line of the column as like as there:
<?php
$columnas = 16;
$filas = 16;
$word= 0;
$contador = 0;
print "<table border=\"1\">\n";
print "<caption>ASCII</caption>\n";
print "<tbody>\n";
print "<tr>\n";
for ($j = 1; $j <= $columnas; $j++){
if ($j%2 == 1){
print "<th>Codigo</th>\n";
}elseif ($j%2 == 0){
print "<th>Valor</th>\n";
}
}
print "</tr>";
for ($i = 1; $i <= $filas; $i++){
for($i = 1; $i <= 50000; $i++,$contador){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>" .$i. "</td>\n";
print "<td>".$unicodeChar."</td> \n";
}
print "</tr>\n";
}
print "</tbody>\n";
print "</table>\n";
?>
enter image description here
You used the modulus in the first loop, so use it again in the second
print "</tr>";
print "<tr>";
for($i = 1; $i <= 50000; $i++){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>$i</td><td>$unicodeChar</td>\n";
if ( $i % $columnas/2 ) == 0 ) {
print "</tr><tr>\n";
}
}
print "</tr>\n";
Thank you very much for this help, i have been fixed!!
here is the code that i fixed, with your idea :)
´´´´
<?php
$columnas = 16;
$filas = 16;
$word= 0;
$contador = 0;
print "<table border=\"1\">\n";
print "<caption>ASCII</caption>\n";
print "<tbody>\n";
print "<tr>\n";
for ($j = 1; $j <= $columnas; $j++){
if ($j%2 == 1){
print "<th>Codigo</th>\n";
}elseif ($j%2 == 0){
print "<th>Valor</th>\n";
}
}
print "</tr>";
for ($i = 1; $i <= $filas; $i++){
print "<tr>";
for($i = 1; $i <= 50000; $i++,$contador){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>" .$i. "</td>\n";
print "<td>".$unicodeChar."</td> \n";
if (($i % 8 ) == 0 ){
print "</tr><tr>";
}
}
print "</tr>\n";
}
print "</tbody>\n";
print "</table>\n";
?>
´´´´
Like Riggsfolly said but maybe divide the columns number by 2 before the modulo, because there are 2 TDs / iteration:
if ( $i % ($columnas/2) ) == 0 ) {
print "</tr><tr>\n";
}

Break PHP into 3 columns and display one by one

I'm trying to break a PHP array into 3 columns (has to be columns, not rows) so it would look something like this:
Item 1 Item 5 Item 9
Item 2 Item 6 Item 10
Item 3 Item 7 Item 11..............
Item 4 Item 8
Update to Meldin answer
$cols = 3; // Number of columns
$len = ceil(count($items) / $cols);
echo '<ul class="floatleft">';
for ($i = 0; $i < count($items); $i++) {
if ($i % $len == 0)
echo '</ul><ul class="floatleft">';
echo "<li>" . $items[$i] . "</li>";
}
echo '</ul>';
Also you can try this using two foreach() loops
$len = ceil(count($items)/3);
$itm = array_chunk($items,$len,true);
foreach($itm as $cols){
echo '<div class="floatleft">';
echo '<ul>';
foreach($cols as $col){
echo '<li>'.$col.'</li>';
}
echo '</ul>';
echo '</div>';
}
Use this
echo '<ul class="floatleft">';
for($i=0;$i<count($items);$i++){
if($i%4==0)
echo '</ul><ul class="floatleft">';
echo "<li>".$items[$i]."</li>";
}
echo '</ul>';
Try this:
$total = 10;
$cols = 3;
$rows = ceil($total/$cols);
echo "<table>";
for($i = 1; $i <= $rows; $i++) {
echo "<tr>";
for($j = 1; $j <= $cols; $j++) {
$value = $i + 4*($j-1);
if ($value <= $total) {
echo "<td>".$value."</td>";
}
}
echo "</tr>";
}
echo "</table>";

Splitting MySql data in 3 columns if i have less than eight values

I want to display some data split into 3 columns. If I have more than eight values​​, then everything is working properly, but if I have less than eight values I get: Notice: Undefined offset:
How can I fix that problem?
echo '<table><tr>';
for ($i=0;$i < count($audio_fileexts) / 3; $i++) {
for ($j = 0; $j < 3; $j++){
echo $audio_fileexts[ $i + $j * 3];
}
echo '</tr><tr>';
}
echo '</tr></table>';
echo '<table>';
for ($i = 0; $i < count($audio_fileexts); $i++) {
if ($i % 3 == 0) {
if ($i > 0) {
echo '</tr>';
}
echo '<tr>';
}
echo '<td>' . $audio_fileexts[$i] . '</td>';
}
if ($i % 3 > 0) {
while ($i++ % 3 > 0) {
echo '<td></td>';
}
echo '</tr>';
}
echo '</table>';

Creating a multiplication "grid"

I am trying to create a simple multiplication grid in PHP
It should be of the format for example for a 2x2 grid:
0 1 2
1 1 2
2 2 4
My issue is getting it to start from 0.
This is my nested for loop so far:
for($i=0;$i<=$_POST['rows'];$i++)
{
echo "<tr>";
for($j=0;$j<=$_POST['columns'];$j++)
{
if($i==0)
{
echo "<td>" . 1*$j . "</td>";
}
else
{
$mult = $i * $j;
echo "<td> $mult </td>";
}
}
echo "</tr>";
}
But it gives the output:
0 1 2
0 1 2
0 2 4
I need the column of 0's to be appropriate.
The way you're getting the top row of 0 1 2 3 is by that special-case on the X-axis. Do a similar special-case for the Y-axis ($j):
if ($i == 0) {
... 1 * $j ...
}
else if ($j == 0) {
... $i * 1 ...
}
else {
... $i * $j ...
}
You not only have $i==0 as special case but also $j==0:
if($i==0)
{
echo "<td>" . 1*$j . "</td>";
}
elseif($j==0)
{
echo "<td>" . $i*1 . "</td>";
}
else
{
$mult = $i * $j;
echo "<td> $mult </td>";
}
I don't understand the way you construct your grid. All you need is a row indicator and the number for the multiplication not a nested loop. Second: Why don't you start with 1 instead of catching the case inside the loop. This would be my variant of the multiplication “grid”
<?php
$rows = $_POST['rows'];
$number = $_POST['columns'];
for( $i=1; $i <= $rows; $i++) {
$mult = $i * $number;
echo "<tr>
<td>" . $i.'*'. $j . "</td>
<td>".$mult."</td>
</tr>";
}
?>
This would to a simple grid (x * y) = result. If you want a complete multiplication table it would be something like this:
<?php
$rows = $_POST['rows'];
$number = $_POST['columns'];
echo "<tr><th></th>";
for( $j=1; $j <= $number; $j++) {
echo "<th>".$j."</th>";
}
echo "</tr>";
for( $i=1; $i <= $rows; $i++) {
echo "<tr>";
echo "<th>".$i."</th>";
for( $j=1; $j <= $number; $j++) {
$mult = $i * $j;
echo "<td>".$mult."</td>";
}
echo "</tr>";
}
?>

Transform html table

Hi I have an array of movie names in alphabetical order, of which I want to create a html table of, I want to do this on the fly so I did the following:
echo "<div align=\"center\"><table>";
$i=0;
foreach ($results as $entry){
//If first in row of 4, open row
if($i == 0) {
echo "<tr>\n";
}
//print a cell
echo "\t<td>" . $entry . "</td>\n";
i++;
//if last cell in row of 4, close row
if($i == 4) {
echo "</tr>\n";
$i=0;
}
}
if($i < 4) {
while($i < 4) {
echo "\t<td></td>\n";
$i++;
}
echo "</tr>\n";
}
echo "</table></div>";
However this builds a table which looks like:
entry0 | entry1 | entry2 | entry3
entry4 | entry5 | entry6 | entry7
How can I go about building a table like:
entry0 | entry3 | entry6
entry1 | entry4 | entry7
entry2 | entry5 | entry8
?
I'm guessing I would have to reorganise my $results array and still build the table the same way?
I'm very new to php ( a week!) so I'm not really sure how to go about this
Thanks for your help
$results = array( 'e1', 'e2', 'e3', 'e4', 'e5', 'e6','e7' );
$NUM_COLUMNS = 3;
$numRows = count($results) / $NUM_COLUMNS;
if (count($results) % $NUM_COLUMNS > 0) {
$numRows += 1;
}
echo "<div align=\"center\"><table>";
$i=0;
for ($i = 0; $i < $numRows; $i++) {
echo "<tr>\n";
$index = $i;
for ($j = 0; $j < $NUM_COLUMNS; $j++) {
//print a cell
$entry = '';
if ($index < count($results)) {
$entry = $results[$index];
}
echo "\t<td>" . $entry . "</td>\n";
$index += $numRows;
}
echo "</tr>\n";
}
echo "</table></div>";
This is tested and includes sorting items vertically. I would write a description, but I just got a phone call and have to go. I will answer questions if you have any in ~1hr. (sorry!)
How about this: (I did not test, but should be OK)
<?php
$i = 1;
$max = 3; // this is the number of columns to display
echo "<div align=\"center\"><table><tr>";
foreach ($results as $entry) {
echo "<td style=\"text-align: center;\">";
echo $entry;
echo "</td>";
$i++;
if ($i == ($max)) {
echo '</tr><tr>';
$i = 1;
}
}
echo "</tr>\n";
echo "</table></div>";
?>

Categories