seran128 Posted December 3, 2006 Share Posted December 3, 2006 I have a page that I need to have the date returned displayed in a table formatLIke $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 More sharing options...
trq Posted December 3, 2006 Share Posted December 3, 2006 Look [url=http://www.phpfreaks.com/forums/index.php/topic,95426.0.html]here[/url]. Link to comment https://forums.phpfreaks.com/topic/29327-dynamic-colums-and-rows/#findComment-134436 Share on other sites More sharing options...
seran128 Posted December 3, 2006 Author Share Posted December 3, 2006 Awsome THANKS!!!!!!! Link to comment https://forums.phpfreaks.com/topic/29327-dynamic-colums-and-rows/#findComment-134440 Share on other sites More sharing options...
keeB Posted December 3, 2006 Share Posted December 3, 2006 For this particular quick task, I wrote a simple class to do this for me. [code]<?phpclass 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 https://forums.phpfreaks.com/topic/29327-dynamic-colums-and-rows/#findComment-134509 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.