Jump to content

Dynamic colums and rows


seran128

Recommended Posts

I have a page that I need to have the date returned displayed in a table format

LIke

$sql="select * from TABLE WHERE RegionID = '$InID' AND active = '1' ORDER BY sort_order";
echo $sql;
$result=mysql_query($sql,$connection) or die(mysql_error());

[table]
[tr][td]value1[/td][td]value2[/td][td]value3[/td][/tr]
[tr][td]value4[/td][td]value5[/td][td]value6[/td][/tr]
[tr][td]value7[/td][td]value8[/td][td]value9[/td][/tr]
[/table]

}

return $tabledata;
}
Link to comment
https://forums.phpfreaks.com/topic/29327-dynamic-colums-and-rows/
Share on other sites

For this particular quick task, I wrote a simple class to do this for me.

[code]
<?php

class table {

    public $t_cssId;
    public $t_data;
    public $colCount;
    public $rowCount = 1;
   
    function table() {
        $this->t_data = array();
        $this->count=1;
    }
   
    function addData($data) {
        array_push($this->t_data, $data);
    }
   
    private function drawSub($array, $depth) {
        $rows = $this->returnRowCount();
        print "</tr> \n<tr class=\"row$rows\">";
        $this->rowCount++;
    if ($depth == 1)
        break;
    $this->colCount=1;
        foreach ($array as $key => $column) {
            if (is_array($column)) {
                $this->drawSub($column, $depth - 1);
            } else {
                $column = nl2br($column);
                print "<tr>\n";
                print "<td class=\"$key\"> $column </td> \n";
                print "</tr>\n";
                $this->colCount++;
            }
        }
    }
   
    function setId($id) {
        $this->t_cssId = $id;
    }
   
    function returnRowCount() {
        return $this->rowCount;   
    }
   
   
    function draw($depth=3) {
        print "<table id=\"$this->t_cssId\"> \n";
        foreach ($this->t_data as $row) {   
        $rows = $this->returnRowCount();

           
            if (is_array($row)) {
                foreach ($row as $key => $column) {
                   
                    if (is_array($column)) {
                        $this->drawSub($column, $depth); //recursive;
                    } else {
                        print " <tr class=\"row$rows\"> \n";
                        print "  <td class=\"$this->colCount\"> $column</td>";
                        $this->rowCount++;
                    }
                }
            } else {

            print " <tr class=\"row$rows\"> \n";
                print "  <td class=\"col$this->colCount\"> $row </td>\n";     
                $this->rowCount++;   
            }
           
            print " </tr> \n"; 
            $this->colCount=1; //reset count
        }
        print "</table>";
    }


    function draw2($depth=3) {
        print "<table id=\"$this->t_cssId\" cellspacing=\"20\" cellpadding=\"0\"> \n";
        foreach ($this->t_data as $row) {   
        $rows = $this->returnRowCount();

           
            if (is_array($row)) {
                foreach ($row as $key => $column) {
                   
                    if (is_array($column)) {
                        $this->drawSub($column, $depth); //recursive;
                    } else {
                        $column = nl2br($column);
                        print " <tr class=\"row$rows\"> \n";
                        print "  <td class=\"$this->colCount\"> $column</td>\n";
                        print " </tr>\n";
                        $this->rowCount++;
                    }
                }
            } else {

            print " <tr class=\"row$rows\"> \n";
                print "  <td class=\"col$this->colCount\"> $row </td>\n";     
                $this->rowCount++;   
            }
           
            print " </tr> \n"; 
            $this->colCount=1; //reset count
        }
        print "</table>";
    }




}
?>
[/code]

usage:

[code]
<?php
$data = array();
$result = mysql_query("select * from some_table");
        while($row = @mysql_fetch_array($result)) {
            array_push($data, $row);
        }
$t = new table();
$t->setID("some_css_id") //sets the <table id value...
$t->addData($data);
$t->draw();
?>
[/code]

Hope this helps. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.