1. #1
    xmatt1
    Guest

    Thumbs up tree menu view with PHP and MySQL

    tree menu view with PHP and MySQL


    Hello everyone!

    Well, i was looking for a great example about tree menu view with PHP and MySQL and i didnt found a GREAT EXAMPLE so i did it in my self and im here for show u as it is easy
    so lets start it. This Example its to build a tree menu that check the users permissions and our profiles so the our database look like this

    Code:
    CREATE TABLE profile (
      idProfile INT(11) NOT NULL AUTO_INCREMENT,
      name VARCHAR(50) NOT NULL,
      PRIMARY KEY(idProfile)
    )
    TYPE=InnoDB;
    
    CREATE TABLE page (
      idPage INT(11) NOT NULL AUTO_INCREMENT,
      parent_id INT(11) NULL,
      name VARCHAR(50) NULL,
      url VARCHAR(80) NULL,
      action VARCHAR(20) NULL,
      visible TINYINT(3) UNSIGNED NULL,
      order INT(11) NULL,
      PRIMARY KEY(idPage)
    )
    TYPE=InnoDB;
    
    CREATE TABLE user (
      idUser INT(11) NOT NULL AUTO_INCREMENT,
      idProfile INT(11) NOT NULL,
      name VARCHAR(50) NOT NULL,
      login VARCHAR(12) NOT NULL,
      password VARCHAR(12) NOT NULL,
      accessDate DATETIME NULL,
      lastAccess DATETIME NULL,
      visits INT(11) NULL,
      PRIMARY KEY(idUser),
      INDEX user_FKIndex1(idProfile),
      FOREIGN KEY(idProfile)
        REFERENCES profile(idProfile)
          ON DELETE NO ACTION
          ON UPDATE NO ACTION
    )
    TYPE=InnoDB;
    
    CREATE TABLE permission (
      idPage INT(11) NOT NULL,
      idProfile INT(11) NOT NULL,
      INDEX permission_FKIndex2(idProfile),
      INDEX permission_FKIndex1(idPage),
      FOREIGN KEY(idProfile)
        REFERENCES profile(idProfile)
          ON DELETE NO ACTION
          ON UPDATE NO ACTION,
      FOREIGN KEY(idPage)
        REFERENCES page(idPage)
          ON DELETE NO ACTION
          ON UPDATE NO ACTION
    )
    TYPE=InnoDB;
    Great so u have our databese structure and now you have to insert some records.
    i'll explain our database structure so
    1. We have a profile table that we insert the users profile if is Admin or its only a simple user
    2. We have a page table that we insert the idProfile that check what profile can access this page and etc..
    3. We have a user table that we insert a user with ur profile in profile table.
    4. and then we have a permission table that define what profile acces page if the user can acces or not! cool

    so now lets to the visual part. First create a html file like this:
    index.html:
    HTML Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>EXT - Tree View with PHP and MySQL </title>
    
    	<link rel="stylesheet" type="text/css" href="../includes/ext/resources/css/ext-all.css" />
    
    	<script type="text/javascript" src="../includes/ext/adapter/ext/ext-base.js"></script>     <!-- ENDLIBS -->
        <script type="text/javascript" src="../includes/ext/ext-all.js"></script>
    	
    	<script type="text/javascript" src="menu.js"></script>
    
    <style type="text/css">
        #tree {
        	float:left;
        	margin:20px;
        	border:1px solid #c3daf9;
        	width:250px;
        	height:300px;
        	overflow:auto;
        }
        .folder .x-tree-node-icon{
    		background:transparent url(../includes/ext/resources/images/default/tree/folder.gif);
    	}
    	.x-tree-node-expanded .x-tree-node-icon{
    		background:transparent url(../includes/ext/resources/images/default/tree/folder-open.gif);
    	}
        </style>
    </head>
    <body>
    
    <div id="tree"></div>
    
    </body>
    </html>
    and now the 2nd step javascript code:
    menu.js:
    Code:
    var TreeTest = function(){
        // shorthand
        var Tree = Ext.tree;
        
        return {
            init : function(){
                // yui-ext tree
                var tree = new Tree.TreePanel('tree', {
                    animate:true, 
                    loader: new Tree.TreeLoader({dataUrl:'get-nodes.php'}),
                    containerScroll: true,
                });
                
                // add a tree sorter in folder mode
                
                // set the root node
                var root = new Tree.AsyncTreeNode({
                    text: 'Menu', 
                    draggable:false, // disable root node dragging
                    id:'source'
                });
    
    			tree.setRootNode(root);
                
                tree.render();
                
                root.expand(false,false);
            }
        };
    }();
    
    Ext.EventManager.onDocumentReady(TreeTest.init, TreeTest, true);
    and now our user class php - ps: i have a extend DB class in my configure.inc.php but its easy for u understand that
    users.inc.php:
    PHP Code:

    class users {

        var 
    $db;
        
        function 
    users()
        {
            
    $this->db $GLOBALS['db'];
        }

        function 
    getParent()
        {
            
    $sql "SELECT * FROM page WHERE parent_id = 0 ";
            
            
    $query $this->db->query($sql);
            
            while(
    $obj $this->db->fetch_object($query)) {
                
                
    $children $this->getChild($obj->idPage);
                
                if(
    $children){
                
                    
    $tmp['text']           =    $obj->name;
                    
    $tmp['id']        =    $obj->idPage;
                    
    $tmp['leaf']        =    false;
                    
    $tmp['cls']        =    'folder';
                    
    $tmp['children']    =    $children;

                    
    $nodes[] = $tmp;
                }
            }
            
            print 
    json_encode($nodes);
        }
        
        function 
    getChild($id)
        {
            
    # look in sql INNER JOIN user as usu ON usu.idUser = '1' you have to alter it for user logon in the site u can do it by $_SESSION['idUser'] but other way i put 1 for a test
            
    $sql "    SELECT pag.*

                        FROM page as pag

                        INNER JOIN user as usu    ON usu.idUser = '1'
                        INNER JOIN permission as per ON (per.idPage = pag.idPage AND    per.idProfile = usu.idProfile)
                                                                    
                        WHERE pag.parent_id = '"
    .$id."' AND pag.visible = '1'
                        ORDER BY pag.order, pag.name "
    ;
                        
            
    $query $this->db->query($sql);
            
            while(
    $obj $this->db->fetch_object($query)){
                
                
    $tmp['text']    =    $obj->name;
                
    $tmp['id']        =    $obj->idPage;
                
    $tmp['leaf']    =    true;
                
    $tmp['cls']        =    'file';
                
                
    $nodes[] = $tmp;
        }
            
            return 
    $nodes;
        }

    and now our json enode in
    get-nodes.php
    PHP Code:
    <?
    require_once '../includes/configure.inc.php';
    require_once 
    CLASSES.'users.inc.php';

    $users = new users;

    $users->getParent();
    ?>

  2. #2
    Ext User
    Join Date
    Jul 2007
    Posts
    10
    Vote Rating
    0
    akbeyfb is on a distinguished road

      0  

    Default


    hi you have to give configure.inc.php file and also we have to assign query() and fetch_object() functions to work it...can you give?

  3. #3
    xmatt1
    Guest

    Wink the codes..

    the codes..


    HI.. sure i can give u...
    this is the configure.inc.php
    PHP Code:
    <?

    define
    (PREFIXO"fv_"); # prefix from table
    define(PROJETO"forever"); # database
    define(_WEBMASTER_"sistemas@mattsoft.eti.br"); # your email

        
    $arhost[0]    = "localhost";
        
    $ardb[0]    = "forever";
        
    $aruser[0]    = "root";
        
    $arpass[0]    = "root";
        
        
    define(PATH"E:/Projetos/WEB/PHP/forever/");
        
    define(URL"http://localhost/PHP/forever/");
        
        
    error_reporting (E_ALL E_NOTICE);
        
    define(INCLUDESPATH .'includes/');            # includes dir
    define(CLASSESINCLUDES .'class/');            # classes dir
    define(PAGESADMINPATH.'url/');            # admin dir
    define(EXTRASADMINPATH.'extras/');            # extra admin dir

    # here are the main classes 
    require_once(INCLUDES .'funcoes.inc.php');            # generic functions
    require_once(INCLUDES .'db.inc.php');                # database functions
    require_once(INCLUDES .'sendMail.inc.php');            # send email

    # Start the database connection
    $db = new db();
    $db->connect();
    $noheader 1;

    # no cache in the files
    if ( !$noheader ) {
        
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        
    header('Last Modified: 'gmdate('D, d M Y H:i:s') .' GMT');
        
    header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
        
    header('Pragma: no-cache');
        
    header('Expires: 0');
    }
    ?>
    this the db.inc.php
    [php]
    <?

    define('VM_FETCH_ARRAY' ,'array');
    define('VM_FETCH_ASSOC' ,'assoc');
    define('VM_FETCH_OBJECT','object');
    define('VM_FETCH_ROW' ,'row');

    class db
    {
    var $link; #ponteiro de conexao com o servidor
    var $host; #endere

  4. #4
    Ext User
    Join Date
    Aug 2007
    Posts
    6
    Vote Rating
    0
    Johann is on a distinguished road

      0  

    Default


    Hello!

    I'm new here. Sorry for my bad english, I'm from Germany.

    I have many Problems with your Script. Is there any way to get this complete Project in one ZIP-File?

    I can't find my error. My IE shows an error in line 13 / 13, but I can't find anyting...

    Thanks a lot.

  5. #5
    Ext User Grimsk's Avatar
    Join Date
    Jul 2007
    Location
    Saint-Georges, Canada
    Posts
    101
    Vote Rating
    0
    Grimsk is on a distinguished road

      0  

    Default


    first get FF and FireBug !

  6. #6
    Sencha User
    Join Date
    Mar 2007
    Posts
    464
    Vote Rating
    1
    JorisA is on a distinguished road

      0  

    Default


    Hi great example. I'm thinking about using the this for storing my tree in the database. Looks a bit complicated but if you put all the logic in the users class it should be easy to use.

  7. #7
    Ext User
    Join Date
    Apr 2008
    Posts
    1
    Vote Rating
    0
    tschenck is on a distinguished road

      0  

    Default


    Quote Originally Posted by JorisA View Post
    Hi great example. I'm thinking about using the this for storing my tree in the database. Looks a bit complicated but if you put all the logic in the users class it should be easy to use.
    If you are using PHP, this class is already neatly implemented in http://pear.php.net/package/Tree - we run it with big trees on high volume sites and it's sweet.

    regards,

    Toby

  8. #8
    Ext User
    Join Date
    Jun 2008
    Posts
    1
    Vote Rating
    0
    ted is on a distinguished road

      0  

    Default


    PHP Code:
    here are the main classes 
     
    require_once(INCLUDES .'funcoes.inc.php'); # generic functions
     
    require_once(INCLUDES .'db.inc.php'); # database functions
     
    require_once(INCLUDES .'sendMail.inc.php'); # send email 

    Greate example, however, I couldn't found the 'funcoes.inc.php' and 'sendMail.inc.php' in the thread.



  9. #9
    Ext User
    Join Date
    Nov 2008
    Posts
    4
    Vote Rating
    0
    anandafit is on a distinguished road

      0  

    Default Some thing is missing

    Some thing is missing


    Hi,

    I tried the example that described in above. But it did not working properly. I can't understand my fault.

  10. #10
    Ext User
    Join Date
    Feb 2009
    Posts
    6
    Vote Rating
    0
    deardeer76 is on a distinguished road

      0  

    Default Could you give us some data into the database tables?

    Could you give us some data into the database tables?


    Hi, could you give us some data in the database tables? With Insert SQL command. Thanks!

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..." hd porno faketaxi