1

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.

4 Answers 4

6

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>
Sign up to request clarification or add additional context in comments.

4 Comments

you should add similar condition like if ($startid <= $maxid) in second for loop also to print only content for cells having header.
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.
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
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";
1

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

5 Comments

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 ?
@user214292 does your key of array start with 0 and value start with 1. if yes then make id='{$k}' to id='{$k+1}'
I was using the example you posted which should start at 1.
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
@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
0

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);
}
}

Comments

0
<?php
function TableFunc($Data)
{
    $Table = "<table>" . PHP_EOL;

    foreach ($Data as $tags => $array) {
        $Table .= "<$tags>" . PHP_EOL;
        foreach ($array as $thead) {
            $tag=$tags==="tbody"?"td":"th";

            $Table .= "<tr>" . PHP_EOL;
            if (is_array($thead)) {
                foreach ($thead as $theadItem) {
                    if (is_array($theadItem))
                        $Table .= "<$tag colspan='$theadItem[1]'>$theadItem[0]</$tag>" . PHP_EOL;
                    else
                        $Table .= "<$tag>$theadItem</$tag>" . PHP_EOL;
                }
            }
            $Table .= "</tr>" . PHP_EOL;
        }
        $Table .= "</$tags>" . PHP_EOL;
    }


    $Table .= "</table>" . PHP_EOL;

    return $Table;
}

$Data = array(
    "thead" => [
        [["GENEL BİLGİ", 2], ["KALORİMETRE (ISINMA)", 2], ["HESAPLAMA", 2]],
        ["NO", "AD SOYAD", "FARK", "TUTAR", "OKUMA", "ÖDENECEK"]
    ],
    "tbody"=>array(
        array("1","MURAT DURAN","100","100.00","10.00","110.00"),
        array("1","MURAT DURAN","100","100.00","10.00","110.00"),
        array("1","MURAT DURAN","100","100.00","10.00","110.00"),
        array("1","MURAT DURAN","100","100.00","10.00","110.00"),
    ),
    "tfoot" => [["NO", "AD SOYAD", "M2", "MAHSUP", "SAYAÇ", "15°", "FARK", "TUTAR", "ORTAK ALAN", "EKSTRA", "MUAFİYET", "OKUMA", "ÖDENECEK"]]
);



echo  TableFunc($Data);

2 Comments

This answer is missing its education explanation. Please do not simply dump snippets on Stack Overflow.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.