Solved

PHP MySQL tree menu

Posted on 2010-11-26
1
1,103 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
leehanken earned 500 total points
ID: 34220208
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

617 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