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
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. :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.