Print an array from top to bottom in a table instead of left to right using PHP

I am trying to write logic to display records from top to bottom and not from left to right in a table.

Here is my attempt:

<?php $options = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); ?>
<table border="1" colspan="0" cellspan="0">
    <?php
    $htmldata = "";
    $counter = 1;

    $rowcount = ceil(count($options) / 3);

    for ($i = 0; $i < count($options); $i++) {
        echo $counter % 3 ."<br/>";

        if ($counter % 3 == 1) {
            $htmldata .= "<tr>";
            $htmldata .= "<td>".$options[$i]."</td>";
        }
        if ($counter % 3 == 2) {
            $htmldata .= "<td>".$options[$i]."</td>";
        }
        if ($counter % 3 == 0) {
            $htmldata .= "<td>".$options[$i]."</td>";
            $htmldata .= "</tr>";
        }
        $counter++;
    }
    echo $htmldata;
    exit;
    ?>
</table>

Below is the current code output in the table.

Actual result

But I need a table for output as follows.

Expected Result

How can i achieve this?
Can you guys help me write this logic?
In addition, it currently displays 10 entries, but can be much larger or smaller, so we need the code to be dynamic.

Any help would be appreciated. Thanks in advance!

NOTE. It should have only 3 columns for any record numbers, and the output should be the same as in the second image example for 10 records.

+4
4

, ( )

<?php
// feel free to change the values in these variables
$options = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
$numCols = 3; // the number of columns to present
$pad = false; // whether to add blank <td> in final row

// don't alter these variables
$rows = (int)ceil(count($options) / $numCols); // number of rows of the column with most rows
$c = count($options) % $numCols; // how many columns have records on the final row
$col = 1; // current column

// iterate over the columns
for ($j = 0; $j < $numCols; $j++) {
    // iterate over rows
    for ($i = 0; $i < $rows; $i++) {
        // if iteration is last line and on a column with extra data
        // or if the iteration is not the last row
        // or if the last row perfectly fits the column count
        if (($col <= $c && $i == $rows - 1) || $i != $rows - 1 || $c == 0) {
            // remove the first element from the options array
            $arr[$i][$j] = array_shift($options);
        }
    }
    // move onto the next column
    $col++;
}

// create the extra <td> elements in the final row
if ($pad) {
    $arr[count($arr) - 1] = array_pad($arr[count($arr) - 1], $numCols, null);
}
?>
<table border="1" colspan="0" cellspan="0">
    <?php for ($i = 0; $i < count($arr); $i++) { ?>
    <tr>
        <?php for ($j = 0; $j < count($arr[$i]); $j++) { ?>
        <td><?= $arr[$i][$j]; ?></td>
        <?php } ?>
    </tr>
    <?php } ?>
</table>

, , . , .

, , . ($numCols), . .

, , . :

  • $options
    . , . .
  • $numCols
    , , . " " ; , , <td> , HTML, .
  • $pad
    , , - $options , , $numCols.
    , . , for , , .
    , , . $pad true, , .

, .
, , ; , , , , , .
. , - -, , exit, , , . , , , PHP. , ( , for ).

, !

Output of code

+4

:

<?php
$options = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16");
$elems = count($options);
// set the # of rows you want to show
$rows=5;
// calculate # of columns
$cols=ceil($elems/$rows);


echo "<table style='border: 2px solid black;'>";
for($r=0; $r<$rows; $r++)
{
    echo '<tr>';
    $shift=0;
    for($c=0; $c<$cols; $c++)
    {
        echo "<td style='border: 1px solid blue;'>";
        if($r+$shift < $elems)
        {
        echo $options[$r+$shift];
        }
        echo '</td>';

        $shift+=$rows;
    }
    echo '</tr>';
}
echo '</table>';
?>
+1

:

<?php
function getTableHtml(array $data, int $colLength): string
{
    $result = '';

    $rowNumber = 0;
    $rows = [];
    foreach (array_values($data) as $k => $v) {
        if ($k % $colLength === 0) {
           ++$rowNumber;
           $rows[$rowNumber] = '';
        }

        $rows[$rowNumber] .= '<td>';
        $rows[$rowNumber] .= htmlentities($v);
        $rows[$rowNumber] .= '</td>';
        $rows[$rowNumber] .= PHP_EOL; // optional formatting
    }

    $result = '<table><tr>' . join('</tr><tr>', $rows) . '</tr></table>';

    return $result;
}

: https://3v4l.org/A7erq

+1

- . . . :

You must fix a row or column (here I assume that you have a 3 row fix). Your desired result has a problem since the first line has 4 lines and the others have 3

<?php 
$options = range(1,10);
$new_array = array();
$rows = 3;
foreach ($options as $value) {
  $new_array[$value % $rows][] = $value;    
}

?>
<table border="1" colspan="0" cellspan="0">
    <?php
        $htmldata = "";
        foreach ($new_array as $key => $value) {
          echo "<tr>";
          foreach ($value as $option) {
            echo "<td>".$option."</td>";
          }
          echo "</tr>";
        }  
    ?>
</table>

The output will be

enter image description here

+1
source

Source: https://habr.com/ru/post/1686807/


All Articles