Dynamically generate table using PHP

24,930

Solution 1

This should help you.

$maxcols = 8; 
$maxid = 12;
$startid = 1;

echo "<table id='table1'>\n";
for ($i = 1;$i<=ceil($maxid/$maxcols);$i++) {

    echo "<tr>\n";
    for ($j=1;$j<=$maxcols;$j++)
        if ($startid <= $maxid)
            echo "  <td class='mark'>ID".$startid++."</td>\n";
        else 
            echo "  <td> </td>\n";

    echo "</tr>\n<tr>\n";
    for ($j=1;$j<=$maxcols;$j++)
        echo "<td>Content</td>\n";

    echo "</tr>\n";
}

echo "</table>\n";

Generates

<table id='table1'>
    <tr>
        <td class='mark'>ID1</td>
        <td class='mark'>ID2</td>
        <td class='mark'>ID3</td>
        <td class='mark'>ID4</td>
        <td class='mark'>ID5</td>
        <td class='mark'>ID6</td>
        <td class='mark'>ID7</td>
        <td class='mark'>ID8</td>
    </tr>
    <tr>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
    </tr>
    <tr>
        <td class='mark'>ID9</td>
        <td class='mark'>ID10</td>
        <td class='mark'>ID11</td>
        <td class='mark'>ID12</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
    </tr>
</table>

Solution 2

try this. It will work in the same way as you desired

<?php

$id= array("1","2","3","4","5","6","7","8","9","10","11","12");

$maxcols = 8;  $i = 0;$j=0;$t=0;$s=0;

$maxid = count($id);

echo "<table id='table1'><tr>";

foreach ($id as $k => $v) 
{

    if($t == 0)
    {

        while ($t <= $maxcols-1) {
                if($s < $maxid)
                {
                         $s++;$t++; echo "<td class='mark'>id$s</td>";
                }
                else
                {
                    echo "<td class='mark'></td>";$t++;$s++;
                }
        }
        echo "</tr><tr>";
    }
    else
    {

    }
        echo "<td id='0'><div id='{$k}' class='drag t1'>{$v}</div></td>"; $i++;
        if ($i == $maxcols) 
    { 
        echo "</tr><tr>"; 

        if($j == 0)
        {
            while ($j <= $maxcols-1) {
                if($s < $maxid)
                {
                     $s++;$j++; echo "<td class='mark'>id$s</td>";
                }
                else
                {
                    echo "<td class='mark'></td>";$j++;$s++;
                }
            }
            echo "</tr><tr>";

        }


        $i=0;

    }
} 

echo "</tr></table>";
?>

Output

Share:
24,930
Rocket
Author by

Rocket

Updated on July 09, 2022

Comments

  • Rocket
    Rocket almost 2 years

    I know this has been asked before and I have got it working using the following code:

    <?php
    $maxcols = 8;  $i = 0;
    echo "<table id='table1'><tr>";
    
    foreach ($id as $k => $v) {
        echo "<td id='0'><div id='{$k}' class='drag t1'>{$v}</div></td>"; $i++;
        if ($i == $maxcols) { $i = 0; echo "</tr><tr>"; }
    } $i++;
    
    
    while ($i <= $maxcols) {
        $i++; echo "<td></td>";
    }
    
    echo "</tr></table>";
    ?>
    

    This results in a table that looks like this :

    enter image description here

    I'd like to add headers to this so the end result looks like this:

    enter image description here

    I'd like to do it dynamically so if I create a table that is only 5 columns wide I'd get on the first header row ID01 - ID05 and on the second header row ID06 - ID10

    I want to limit the header ID values to be no more than $maxid any extra header fields should be blank, like this : If $maxid = 12; then :

    enter image description here

    I need the header rows are made as follows and not using <TH>

    <td class="mark">
    

    I'm using some javascript to allow the movement of cell data.

    The class is used to set the formatting on the header and stop items from being dragged into the fields.

    Can anyone point me in the right direction on how to do this.

  • krishna
    krishna about 10 years
    you should add similar condition like if ($startid <= $maxid) in second for loop also to print only content for cells having header.
  • keepwalking
    keepwalking about 10 years
    Something similar but that condition will not work because in the first loop $startid will reach $maxid and last Content row will not show at all.
  • Rocket
    Rocket about 10 years
    Thanks for this - how do I edit echo "<td>Content</td>\n"; to include the cell ID number as well ? eg : echo "<td id='ID12'>Content</td>\n"; ? Thanks
  • Rocket
    Rocket about 10 years
    Thanks, The DIV ID seems to be one less than the ID.. <td id='0'><div id='5' class='drag t1'>6</div></td> ? I was expecting it to be the same ?
  • krishna
    krishna about 10 years
    @user214292 does your key of array start with 0 and value start with 1. if yes then make id='{$k}' to id='{$k+1}'
  • Rocket
    Rocket about 10 years
    I was using the example you posted which should start at 1.
  • krishna
    krishna about 10 years
    yes, in my example values start from 1 where as keys of array start from 0. i am assigning keys as id to div's. So you can also change the id to $v instead of $k which will result in same ids
  • krishna
    krishna about 10 years
    @user214292 check here after changing div id from $k to $v it works fine 3v4l.org/RYQY8 Also id='{$k+1}' is invalid. sorry for incorrect info
  • Rocket
    Rocket about 10 years
    This works. echo "</tr>\n<tr>\n"; for ($j=1;$j<=$maxcols;$j++) $p = ($i==1) ? $j : ($j+$maxcols); echo "<td id='$p'>Content</td>\n";