Dynamically generate table using PHP

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 :

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

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 :

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.

Answers


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>

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


Hi You can Use My Library:

class generate{
private $row = "<tr>{columns}</tr>";
private $td = "<td {attr}>{data}</td>";

private $attributeTR="";
private $attributeTD="";

private $tdBuilder="";

public function addCol($ColumValsArr=array("class='motota'"=>"Example")){
    foreach($ColumValsArr as $key=>$val){
        $newCol = str_replace("{data}",$val,$this->td); 
        $newCol = str_replace("{attr}",$key,$newCol);

        $this->tdBuilder .= str_replace("{data}",$key,$newCol); 
    }
}
public function getRow(){
    return str_replace("{columns}",$this->tdBuilder,$this->row);
}
}

Need Your Help

Selecting and using operators per partition group

sql sql-server sql-server-2014

I have the following table that stores sporting event data. I'm trying to find which events have increased in attendance upon returning to the same country.

Extract URL of Image From String

ios url nsstring range

I am trying to extract the URL for an image from a string of text. It may be from different websites, and could be any form of image (jpg, png, gif, etc.). What would be a good way to scan the st...