Wrox Programmer Forums
|
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 23rd, 2004, 03:08 AM
Registered User
 
Join Date: Mar 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default PHP Tree menu

Hi, I am trying to make a website with a categorised menu system,
I have elements in the database in binary tree format
|id|cateogory|parent_id|

http://mdonnici.no-ip.com/~muralik/p...//menutest.php

I want the result exactly like above .. and my i made solution like below , i want to know if there is any better solution, basically i want to put entire result in an array, and use a template (smarty) to display the result. at the moment, i am displaying in the function itself. any other ideaS?

function getRelatedItems($id)
    {
        //$rootelement = array(0, $id);
        //$rootelements = getImmediateChildren(0);
        $allparents = getAllParents($id);
        $parentsSubOrdinates = parentsub($allparents);
        $getfinal = array_merge(getImmediateChildren($id),getSubOrdina tes($id),$parentsSubOrdinates);
        //echo showelements($getfinal);
        return $getfinal;
    }


    $myarrays = array();

function parentsub($myarray)
    {
        global $myarrays;
        $i=0;
        foreach ($myarray as $val)
        {
            foreach(getSubOrdinates($val) as $v)
            {
                array_push($myarrays,$v);
            }
            //$myarrays = array_merge_recursive(getSubOrdinates($val));
        }
        //print_r($myarray);
        //echo "" . $i . "" ;
        return $myarrays;
    }

    function getSubOrdinates($id)
    {
            global $catalogtable;
            $dbs = new dbSession();
            //echo $id;
            //echo getParent(1);
            if ($id == 0)
            { $parent = 0; }
            else
            {
                $parent = getParent($id);
            }
            //echo getParent($id);
            $sql = "SELECT CategoryID FROM $catalogtable where ParentID = $parent";
            //echo $sql;
            //echo $sql;
            $result = $dbs->getResult($sql);
                while ($row = mysql_fetch_array($result))
                    {
                        if ( !is_array($myarray) ) $myarray= array();
                        array_push($myarray, $row["CategoryID"]);
                        //echo showelements($myarray);
// echo "hello" . getSubOrdinates($row["CategoryID"]);
                    }
            return $myarray;

    }

    $myarrayx = array();
    function getAllParents($id)
    {
            global $catalogtable, $myarrayx;
            $dbs = new dbSession();

            //$parent = getParent($id);
            $sql = "SELECT * FROM $catalogtable where CategoryID = $id";
            //echo $sql;
            $result = $dbs->getResult($sql);
                while ($row = mysql_fetch_array($result))
                    {
                        //if ( !is_array($myarray) ) $myarray= array();
                        //$row["ParentID"] ;
                        array_push($myarrayx, $row["ParentID"]);
                        //array_merge($myarrayx, getSubOrdinates($row["ParentID"]));
                        getAllParents($row['ParentID']);
                        //print_r($myarrayx) . "<br>";
                        $id = $row['ParentID'];
                    }
            return $myarrayx;

    }
    //echo count($subarray);
    $myid = getcurrentElement();
    $subarray = getSubOrdinates($myid);
    //echo $myid;
    //echo showelements($subarray);
    //parentsSubordinates($subarray);

    function parentsSubordinates($myarray)
    {
        foreach ($myarray as $val)
        {
            echo showelements(getSubOrdinates($val));
        }

    }

    function getImmediateChildren($id)
        {
            global $catalogtable;
            $dbs = new dbSession();
            $sql = "SELECT CategoryID FROM $catalogtable where ParentID = $id";
            //echo $sql;

            $result = $dbs->getResult($sql);
                while ($row = mysql_fetch_array($result))
                    {
                        if ( !is_array($myarray) ) $myarray= array();
                        array_push($myarray, $row["CategoryID"]);
                    }
            if (count($myarray) == 0)
                {
                $myarray = array();
                }
            return $myarray;
        }


    function displayrootElements($id, $myelements)
    {
    global $imgpath, $siteroot, $catalog, $catalogtable, $baseURL,$catalogURL, $currentid;
        $dbs = new dbSession();
        $currentid = getcurrentElement();
        //echo $currentid;
        $sql = "SELECT * FROM $catalogtable where ParentID = $id";
        $relatedrec = array();
        //echo showelements($myelements);
    if (!$result = $dbs->getResult($sql) )
                    {
                    $dbs->printError();
                    }
                    else
                    {

               while ($row = mysql_fetch_array($result))
                   {
    // $relatedrec = getrelated($row['CategoryID']);
    // $relatedrec = $myelements;
            if (in_array( $row['CategoryID'], $myelements )) {
                    //echo showelements($myelements);
                    $retvalue .= "<table cellspacing=0 cellpadding=0 border=0 width=\"100%\">\n";
                    $retvalue .= "<tr><td width=\"10\"><img valign=top src=\"". $baseURL . "images/trans.gif\" width=\"10\"></td><td valign=\"top\" align=\"left\" width=\"200\" >\n";
                    $retvalue .= "<table border=0 cellspacing=0 cellpadding=\"2\" >\n";
                    $retvalue .= "<tr>\n";
                    $retvalue .= "<td valign=\"top\" align=\"left\">\n";
                    //$retvalue .= "<b>\n";
                $retvalue .= "<a href=\"". $baseURL . "/menutest.php?id=" . $row['CategoryID']."\">";
                $retvalue .= $row['Category'] ;
                $retvalue .= "</a>";
                } //for inarray loop
                //echo showelements($relatedrec);
                $currentElement = $row['CategoryID'];

                if ($currentElement != 0)
                {
                    if (getParent($row['CategoryID']) != $currentElement)
                    {
                        if ( $currentid != 0 )
                        {
                                if (in_array( $row['CategoryID'], $myelements )) {
                                //echo "".showelements($relatedrec)."";
                                    $retvalue .= displayrootElements($row['CategoryID'], $myelements);
                                }
                        }
                        //echo $currentid;
                    //echo "hello";
                    }
                }
                        if ( in_array( $row['CategoryID'], $myelements )) {
                $retvalue .= "</td></tr></table>\n";
                $retvalue .= "</td></tr>\n";
                $retvalue .= "</table>\n";
                }
        }

    }
    return $retvalue;
    }

    function getParent($id)
    {
        global $catalogtable;
        $dbs = new dbSession();
        $sql = "SELECT ParentID FROM $catalogtable where CategoryID = $id";

    if (!$result = $dbs->getResult($sql) )
                    {
                    $dbs->printError();
                    }
                    else
                    {
                      while ($row = mysql_fetch_array($result))
                      {
                        $parent = $row['ParentID'];
                      }
                    }
    return $parent;
    }

    function showelements($relatedx)
        {
            foreach ($relatedx as $val)
                {
                    $txt .= $val . ",";
                }

        return $txt;
    }
//$smarty->assign('menu', displayrootElements($id, $myelements) );


    function getcurrentElement()
    {
        //$myarray = processURI();
        //$currentElement= array_pop($myarray);
        $currentElement= $_GET["id"];
        if (!$currentElement || !is_numeric($currentElement) )
        {
        $currentElement=0;
        }
    return $currentElement;
    }



            $currentid = getcurrentElement();
            //get related items to display only items related to the current selected id
            $getfinal = getRelatedItems($currentid);

            //passing the array to make it check if it is in array
            $menu = displayrootElements(0, $getfinal);
            echo $menu;

?>

 
Old March 23rd, 2004, 09:54 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Simple solution -- why not create a PHP variable that holds all the HTML for the menu instead of echoing it out? This will play into your smarty template well.

more complex solution -- generate a javascript array variable and have javascript expand and collapse the tree on the client side without making multiple requests of the server.

Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
tree menu anjalisharma Hibernate 4 September 24th, 2007 05:04 AM
How to make the Tree View Menu in Javascript sandeep_akhare Other Programming Languages 2 January 30th, 2007 12:03 PM
Tree Menu Based on Directories red_fiesta Classic ASP Professional 4 November 1st, 2006 08:42 AM
How I store & retrieve List/Menu Mysql/PHP gsedrak1 PHP Databases 3 September 15th, 2005 08:49 PM
Tree In PHP ruhin Pro PHP 4 June 19th, 2004 06:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.