Posts Tagged ‘php datagrid’
Mar 03 2008
Php Data Grid - 1
I couldn’t find a good php OS Data Grid Class, so let’s learn how to write one together.
1) The first step will be to create a new folder with the name “arpsdatagrid”
2) Create new file under “arpsdatagrid” folder with the name “arpsdgclass.php”
3) Lets start writing the file
Notes:
1) I’m writing this class with almost no ready made Array functions, just for best understanding of php.
2) There is a reason why i’m using the $dataheaders Array, it’s for future developing of our class.
<?php
class datagrid
{
var $datares; //Array of Data
var $cellsnum; //Size Of $datares
var $dataheaders; //Array of Data Headers
function datagrid($datares,$dataheaders)
{
$this->datares=$datares; //Setting Class Value
$this->cellsnum=sizeof($datares); //Setting Class Value
$this->dataheaders=$dataheaders; //Setting Class Value
return true;
}
function gettable() //Return String that contain table with all the Data
{
$array=$this->datares; //For easy use
$s=”<table border=1><tr>”; //Table Set
for($i=0;$i<sizeof($this->dataheaders);$i++) //get every header
{
$s.=”<td class=\”datagridheadertd\”>”.$this->dataheaders[$i].”</td> \n”;
}
$s.=”</tr>”;
for($i=0;$i<sizeof($array);$i++) //for every value at our Data Array
{
$s.=”<tr>”;
for($x=0;$x<sizeof($this->dataheaders);$x++) //Getting Data Headers
{
$s.=”<td>”.$array[$i][$this->dataheaders[$x]].”</td>”;
}
$s.=”</tr>”;
}
$s.=”</table>”;
return $s;
}
} //Closing the class
?>
How to use it?
Let’s say we have an Array named $array, the array contain
{
$array[0][’FNAME’]=’Aviv’;
$array[0][’LNAME’]=’Ronen’;
$array[1][’FNAME’]=’Aviv2′;
$array[1][’LNAME’]=’Ronen2′;
}
Basically, what we need to do is very simple:
1) $headersarray[0]=’FNAME’; //Setting the headers Array
$headersarray[1]=’LNAME’;
2) $datagrid=new datagrid($array,$headersarray); // Setting a Data Grid.
$tablegrid=$datagrid->gettable(); //Fetching the table string to $tablegrid
echo $tablegrid; //Printing the table string
The result will be:
| FNAME | LNAME |
| Aviv | Ronen |
| Aviv2 | Ronen2 |
Walla!
Enjoy!

