muralik Smarty Rookie
Joined: 08 Mar 2004 Posts: 25
|
Posted: Tue Mar 23, 2004 7:45 am Post subject: Server Side Tree Menu |
|
|
Hi,
I am developing a website, it contains a categorised menu, Menu items comes from database, table is in binary tree structure.
Quote: | id | category | parent_id |
I have kept the sample here
http://mdonnici.no-ip.com/~muralik/picturer/menutest.php?id=1
Basically, When i click on a link, it expands, request goes to the server whenever i click, that is what i wanted. At the moment, i am displaying the result in the function itself, but i want to use smarty templates for displaying. Can anyone help me how do i achieve that.
My logic is like this,
1. I display the root elements if no ID is selected,
2. Whenever an ID is selected through URL, i get the querystring and find the related records, I find the Immediate Children of the selected ITEM, Subordinates of the ITEM, and Parent's Path Subordinate items. And i display the entire tree with only elements if they are present in the related array.
This might not be a better logic, but this i worked out after long time working on this.
Now i need to use smarty template for this, how can i achieve this, can anyone please help me.
here is the code
Code: |
<?php
$catalogtable = "arts";
$baseURL = "/picturedb/";
//function to get all the related items of the given ID
function getRelatedItems($id)
{
//getting elements in the parents path
$allparents = getAllParents($id);
//finding subordinates of elements in the parent path
$parentsSubOrdinates = parentsub($allparents);
//merging the arrays to return all related elements of selected id to compare while displaying
$getfinal = array_merge(getImmediateChildren($id),getSubOrdinates($id),$parentsSubOrdinates);
return $getfinal;
}
//function to return subordinates of given array and return a single array
$myarrays = array();
function parentsub($myarray)
{
global $myarrays;
$i=0;
foreach ($myarray as $val)
{
foreach(getSubOrdinates($val) as $v)
{
array_push($myarrays,$v);
}
}
return $myarrays;
}
//function to return subordinates of given element and return array
function getSubOrdinates($id)
{
global $catalogtable;
$dbs = new dbSession();
if ($id == 0)
{ $parent = 0; }
else
{
$parent = getParent($id);
}
$sql = "SELECT CategoryID FROM $catalogtable where ParentID = $parent";
$result = $dbs->getResult($sql);
while ($row = mysql_fetch_array($result))
{
if ( !is_array($myarray) ) $myarray= array();
array_push($myarray, $row["CategoryID"]);
}
return $myarray;
}
//return elements in parent path
$myarrayx = array();
function getAllParents($id)
{
global $catalogtable, $myarrayx;
$dbs = new dbSession();
$sql = "SELECT * FROM $catalogtable where CategoryID = $id";
$result = $dbs->getResult($sql);
while ($row = mysql_fetch_array($result))
{
array_push($myarrayx, $row["ParentID"]);
getAllParents($row['ParentID']);
$id = $row['ParentID'];
}
return $myarrayx;
}
//function to get immediate children of the current element
function getImmediateChildren($id)
{
global $catalogtable;
$dbs = new dbSession();
$sql = "SELECT CategoryID FROM $catalogtable where ParentID = $id";
$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;
}
//main function to display table
function displayrootElements($id, $myelements)
{
global $imgpath, $siteroot, $catalog, $catalogtable, $baseURL,$catalogURL, $currentid;
$dbs = new dbSession();
$currentid = getcurrentElement();
$sql = "SELECT * FROM $catalogtable where ParentID = $id";
$relatedrec = array();
if (!$result = $dbs->getResult($sql) )
{
$dbs->printError();
}
else
{
while ($row = mysql_fetch_array($result))
{
if (in_array( $row['CategoryID'], $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 .= "<a href=\"". $baseURL . "/menutest.php?id=" . $row['CategoryID']."\">";
$retvalue .= $row['Category'] ;
$retvalue .= "</a>";
} //for inarray loop
$currentElement = $row['CategoryID'];
if ($currentElement != 0)
{
if (getParent($row['CategoryID']) != $currentElement)
{
if ( $currentid != 0 )
{
if (in_array( $row['CategoryID'], $myelements )) {
$retvalue .= displayrootElements($row['CategoryID'], $myelements);
}
}
}
}
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;
}
//$smarty->assign('menu', displayrootElements($id, $myelements) );
function getcurrentElement()
{
$currentElement= $_GET["id"];
if (!$currentElement || !is_numeric($currentElement) )
{
$currentElement=0;
}
return $currentElement;
}
//main display
$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;
?>
|
Sorry, i pasted entire code.
thanks |
|