Solved

PHP MySQL tree menu

Posted on 2010-11-26
1
1,092 Views
Last Modified: 2012-05-10
Hi,
I'm trying to make my tree menu display properly but it's driving me a little bit nuts!

Basically I have the following code that produces a nested tree structure for pages in my cms:

 test.php

It is supposed to render as follows:

>Home
   - Spiders Attack!!!
      - About Page
   - Page Four Link
>HWRCs

but instead renders like this page (out of alignment)

http://dev.netbridge.co.uk/admin/test.php

I can of course supply css and extra code if it would help solve my problem, and I would really appreciate it if you could help in any way.

Thanks,

owennicol
0
Comment
Question by:owennicol
1 Comment
 
LVL 1

Accepted Solution

by:
leehanken earned 500 total points
Comment Utility
The issue is in function build_child()

for ( $c=0;$c<$depth;$c++ )            // Indent over so that there is distinction between levels
{
$tempTree .= '<div class="test">';
}

Open in new window

which adds a div with a 20 pixel margin on the left depending on the depth, however the entire row is put within the div instead of just the left hand button.

To fix this you somehow need to just appy the margin to enclose this code
<span><a class="sexybutton sexysimple" href="edit_page.php?pid=' . $pid . '"><span class="book">' . $linklabel . '</span></a></span>

Open in new window


as shown below
function build_child($oldID)            // Recursive function to get all of the children...unlimited depth 
{ 
    global $exclude, $depth;            // Refer to the global array defined at the top of this script 
	include("../scripts/connect_to_mysql.php");
    $child_query = mysqli_query($myConnection,"SELECT pageorder, id, linklabel, pagetitle, showing, navigation, date_format(lastmodified,'%e/%c/%Y') as formattedDate FROM pages WHERE parent_id='$oldID' ORDER BY pageorder ASC" ); 
    while ( $child = mysqli_fetch_array($child_query, MYSQL_ASSOC) ) 
    {
		$pid = $child["id"];
		$linklabel = $child["linklabel"];
		$pagetitle = $child["pagetitle"];
		$pageorder = $child["pageorder"];
		$parent_id = $child["parent_id"];
		$showing = $child["showing"];
		$date = $child["formattedDate"];
		if($showing=='0'){ $show =  '<img src="../style/SexyButtons/images/icons/silk/cross.png" width="16" height="16" alt="cross" />';} else { $show = '<img 	src="../style/SexyButtons/images/icons/silk/tick.png" width="16" height="16" alt="tick" />';}
		$navigation = $child["navigation"];
		if($navigation=='0'){ $nav =  '<img src="../style/SexyButtons/images/icons/silk/cross.png" width="16" height="16" alt="cross" />';} else { $nav = '<img src="../style/SexyButtons/images/icons/silk/tick.png" width="16" height="16" alt="tick" />';}
        
		
		if ( $pid != $parent_id ) 
        { 	
			
			
				
			$tempTree .= '<li id="recordsArray_' . $pid . '"><span class="label">";

for ( $c=0;$c<$depth;$c++ )            // Indent over so that there is distinction between levels 
            { 	 			
				$tempTree .= '<span class="test">';  				 
			} 
$tempTree .= "<a class="sexybutton sexysimple" href="edit_page.php?pid=' . $pid . '"><span class="book">' . $linklabel . '</span></a></span></span>
	<span class="delete"><span style="padding-top:3px;"><a class="sexybutton sexysimple" onclick="confirmation()"><span class="cancel">Delete this page</span></a></span></form></span>
	<span class="nav" ><span style="padding-top:3px;">' . $nav . '</span></span>
		<span class="live" ><span style="padding-top:3px;">' . $show . '</span></span>
	<span class="order"><span style="padding-top:3px;">Drag</span></span>
	<span class="mod"><span style="padding-top:3px;">' . $date . '</span></span></li>';
			
            $depth++;        // Incriment depth b/c we're building this child's child tree  (complicated yet???) 
			$tempTree .= build_child($pid);        // Add to the temporary local tree 
			$depth--;        // Decrement depth b/c we're done building the child's child tree. 
            $tempTree .= '</div>';
			array_push($exclude, $pid);            // Add the item to the exclusion list 
			
        } 
    }  
   
    return $tempTree;        // Return the entire child tree 
}

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now