Jul 3, 2011

Simple PHP Script to Generate TreeGrid View



I've been working on PHP which need to generate TreeGrid view. I've tried to find a free script on TreeGrid View but seem like it's hard to find one.  Most of the script are shareware. By shareware I mean the cost is more than 10 bucks. I'm not going to waste my money so I decided to write my own script.

The script was written in PHP.  Thanks to Apogee for the guide and advise on using recursive function a few years ago. The script cover only basic function to generate Tree Grid view.  Feel free to modified, delete, or add codes as long as you the script stay free.


Below is my script which is currently need a lot of work on appearance.
   1: <?php
   2: /*
   3:  * This script was created due to lots of script to generate output 
   4:  * in Tree Grid View are shareware.  So I decided to make my own script 
   5:  * which will generate Tree Grid View. Feel free to edit, delete or use
   6:  * this script as long as it is free.
   7:  * 
   8:  * written by GunBladeIV
   9:  * 03/07/2011
  10:  */
  11:  
  12:  
  13: //Connection to database
  14: define("PASSWORD", "your_pass");
  15: define("HOST", "localhost");
  16: define("USER", "your_user");
  17:  
  18: $LINK = mysql_connect(HOST, USER, PASSWORD);
  19: mysql_select_db("database_name", $LINK);
  20:  
  21: class Tree {
  22:     
  23:     // Public variable
  24:     public $count;
  25:     
  26:     public function __construct() {
  27:         $this->count = 0;
  28:     }
  29:     
  30:     public function getTree($id) {
  31:         $query = "SELECT * FROM tree WHERE ID='$id'";
  32:         $result = mysql_query($query);
  33:  
  34:         $root = mysql_fetch_assoc($result);
  35:         return $root;
  36:     }
  37:  
  38:     public function getChild($pid,$level,$limit) {
  39:         
  40:         if ($level < $limit) {
  41:             $query = "SELECT * FROM tree WHERE PID='$pid'";
  42:             $result = mysql_query($query);
  43:             while ($data = mysql_fetch_assoc($result)) {
  44:                 $this->count = $this->count + 1;
  45:                 echo "<tr id='child-" . $level . "'>";
  46:                 echo "<td class='title'>";
  47:                 for ($i = 0; $i < $level; $i++) {
  48:                     echo "&nbsp;&nbsp;&nbsp;";
  49:                 }
  50:                 echo $data['Title'] . "</td>";
  51:                 echo "<td class='id'>" . $data['ID'] . "</td>";
  52:                 echo "<td class='pid'>" . $data['PID'] . "</td>";
  53:                 echo "</tr>";
  54:                 $this->getChild($data['ID'], $level + 1, $limit);
  55:             }
  56:         }
  57:     }
  58:  
  59: }
  60:  
  61:  
  62: // Declare object base on Tree class
  63: $tree = new Tree();
  64:  
  65: // Now we fetch the ID of node to be displayed using GET
  66: if (isset($_GET['id'])) {
  67:     $id = $_GET['id'];
  68:     $root = $tree->getTree($id);
  69:  
  70:     // Output the root Title 
  71:     echo "Title :" . $root['Title'];
  72:     echo "<br>";
  73:     
  74:     // Now we set the limit and current level.
  75:     $level=1; $limit=5;
  76:     ?>
  77:     <table id="tree-container">
  78:         <thead>
  79:         <th>Title</th>
  80:         <th>ID</th>
  81:         <th>PID</th>
  82:     </thead>
  83:     <? $tree->getChild($id,$level,$limit); ?>
  84:     </table>
  85:     <?php echo "Total node: " . $tree->count; ?>
  86: <?php
  87: }
  88: ?>
  89: <style>
  90:     table#child-1
  91:     {
  92:         width:100%;
  93:         border:thin dotted gray;
  94:         padding-left:10px;
  95:         color:green;
  96:     }
  97:     table#child-2
  98:     {
  99:         width:100%;
 100:         border:thin dotted gray;
 101:         padding-left: 20px;
 102:         color:magenta;
 103:     }
 104:     table#child-3
 105:     {
 106:         width:100%;
 107:         border:thin dotted gray;
 108:         padding-left: 30px;
 109:         color:brown;
 110:     }
 111:     table#child-4
 112:     {
 113:         width:100%;
 114:         border:thin dotted gray;
 115:         padding-left: 40px;
 116:         color:yellow;
 117:     }
 118:     table#child-5
 119:     {
 120:         width:100%;
 121:         border:thin dotted gray;
 122:         padding-left: 50px;
 123:         color:pink;
 124:     }
 125:     td.title
 126:     {
 127:         width:600px;
 128:         border:thin dotted gray;
 129:     }
 130:     td.id
 131:     {
 132:         width:200px;
 133:         border:thin dotted gray;
 134:         text-align:center;
 135:     }
 136:     td.pid
 137:     {
 138:         width:200px;
 139:         border: thin dotted grey;
 140:         text-align:center;
 141:     }
 142: </style>

As testing purpose, I attached SQL query to create table to have the idea on how this script works.  Briefly, the script only need ID and PID where ID is the id of the nodes, and PID is the Parent ID for the nodes. 


   1: -- phpMyAdmin SQL Dump
   2: -- version 3.3.9
   3: -- http://www.phpmyadmin.net
   4: --
   5: -- Host: localhost
   6: -- Generation Time: Jul 03, 2011 at 02:16 AM
   7: -- Server version: 5.5.8
   8: -- PHP Version: 5.3.5
   9:  
  10: SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
  11:  
  12: --
  13: -- Database: `test`
  14: --
  15: -- --------------------------------------------------------
  16:  
  17: --
  18: -- Table structure for table `tree`
  19: --
  20: CREATE TABLE IF NOT EXISTS `tree` (
  21:   `ID` int(11) NOT NULL AUTO_INCREMENT,
  22:   `PID` int(11) DEFAULT NULL,
  23:   `Title` varchar(150) NOT NULL,
  24:   PRIMARY KEY (`ID`)
  25: ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
  26:  
  27: --
  28: -- Dumping data for table `tree`
  29: --
  30: INSERT INTO `tree` (`ID`, `PID`, `Title`) VALUES
  31: (1, NULL, 'Root One'),
  32: (2, NULL, 'Root Two'),
  33: (3, 1, 'Child Root One 1'),
  34: (4, 1, 'Child Root One 2'),
  35: (5, 3, 'Child of Child Root One 1'),
  36: (6, 3, 'Child of Child Root One 2'),
  37: (7, 6, 'Child of root '),
  38: (8, 7, 'Child of Child Unknown');

Enjoys and don't forget to like this post on Facebook.  Any suggestion, comments or critics please drop it as comments. All response will be appreciated.  Thank you.

Blogger Labels: Simple,Script,Generate,TreeGrid,View,Most,cost,bucks,money,Thanks,Apogee,Tree,Grid,Feel,appearance,output,GunBladeIV,Connection,database,PASSWORD,HOST,USER,LINK,Public,__construct,SELECT,FROM,WHERE,result,data,Title,Declare,node,_GET,container,Total,width,text,purpose,Parent,Dump,version,Generation,Time,Server,SQL_MODE,NO_AUTO_VALUE_ON_ZERO,Table,CREATE,NULL,AUTO_INCREMENT,DEFAULT,PRIMARY,ENGINE,InnoDB,CHARSET,INSERT,INTO,VALUES,Root,Child,Unknown,Facebook,suggestion,comments,response,Thank,codes,critics,shareware,localhost,getTree,mysql_query,mysql_fetch_assoc,getChild,nbsp,thead,nodes,phpMyAdmin


Thank you for your unbelievable support on Negative Zero - Permission to read and write blog for nearly 4 years. Don't forget to like Negative Zero on Facebook.
Blogirific.com Blog Directory





Post(s) you might like to read :

Comments
0 Comments

0 comments:

Post a Comment