Multi-level navigation using PHP and Mysql to store the pages

Hi Experts,

I have been trying for a good week or so to find a solution to this problem, and all my attempts have failed.
I have a table that is storing page details as follows:

`page_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`page_title` TEXT NOT NULL ,
`page_content` LONGTEXT NOT NULL ,
`permalink` TEXT NOT NULL,
`parent_page_id` INT NULL ,
`dateCreated` VARCHAR( 25 ) NOT NULL ,
`dateUpdated` VARCHAR( 25 ) NULL


This is a 2 level page navigation I am wanting to do (unless you think there is an easier way to make it multi-level (more than 2 levels deep)), but from what i have read on other sites it is very difficult.

from my understanding I need 2 loops (Correct me if i am wrong...)
  loop 1 will loop through the pages (horizontal output) {
     the second loop will be run for every page {
         output all page links where parent_page_id = the current page's id
      }
  }


This is the code I am using so far and this correctly generates a list of all pages (the parents)

$sql = "select * from pages where page_id != 1 and approved=1 and deleted=0 ORDER BY `order` ASC";
		$result = mysql_query($sql) or die(mysql_error());
		
		
		 while($page = mysql_fetch_assoc($result)){
			extract($page); 
			echo '<li class="' . $page['page_title'] .'"><a href="' . $page['permalink'] . '" title="' . $page['page_title'] . '">' . $page['page_title'] . '</a></li>';			
			if($page['parent_page_id'] == $page['page_id']){
				echo '<ul id="sub-nav">
				        <li class="' . $page['page_title'] .'"><a href="' . $page['permalink'] . '.php" title="' . $page['page_title'] . '">' . $page['page_title'] . '</a></li>
				      </ul>';
			}
		}

Open in new window

LVL 1
seraph_matrix_631Asked:
Who is Participating?
 
KysolCommented:
/* Build menu */
foreach ( $pages_data as $page_id => $page_data )
{
   print '<li class="' . $page_data[ 'data' ][ 'page_title' ] . '">' .
            '<a href="' . $page_data[ 'data' ][ 'permalink' ] . '" title="' . $page_data[ 'data' ][ 'page_title' ] . '">' .
               $page_data[ 'data' ][ 'page_title' ] .
            '</a>';

   if ( count( $page_data[ 'children' ] ) > 0 )
   {
      print '<ul id="sub-nav">';

      foreach ( $page_data as $sub_id => $sub_data )
      {
         print '<li class="' . $sub_data[ 'data' ][ 'page_title' ] . '">' .
                  '<a href="' . $sub_data[ 'data' ][ 'permalink' ] . '" title="' . $sub_data[ 'data' ][ 'page_title' ] . '">' .
                     $sub_data[ 'data' ][ 'page_title' ] .
                  '</a>'
               '</li>';
      }

      print '</ul>';
   }

   print '</li>';
}


Didn't test it, but that should be it... I dunno what I was smoking when I wrote that code... sleep deprivation probably ;;
0
 
KysolCommented:
I dunno if this will work, I haven't tested it, but in theory it should work. If it doesn't it also should give you a good idea of what you could be doing with the results of that query.

Good Luck
$pages_sql   = "SELECT * " .
               "FROM `pages` " .
               "WHERE " .
                  "`page_id` != 1 AND " .
                  "`approved` = 1 AND " .
                  "`deleted` = 0 " .
               "ORDER BY `order` ASC";
$pages_ret   = mysql_query( $pages_sql ) or die ( mysql_error() );
$pages_arr   = mysql_fetch_assoc( $pages_ret );

$pages_data  = array();

/* Build pages data array */
foreach ( $pages_arr as $page_id => $page_data )
{
   if ( $page_data[ 'parent_page_id' ] == 0 )
   {
      $pages_data[ $page_data[ 'page_id' ] ] = array( 'data' => $page_data, 'children' => array() );
   }
}
foreach ( $pages_arr as $page_id => $page_data )
{
   if ( $page_data[ 'parent_page_id' ] != 0 )
   {
      $pages_data[ $page_data[ 'parent_page_id' ] ][ 'children' ][ $page_data[ 'page_id' ] ] = array( 'data' => $page_data );
   }
}

/* Build menu */
foreach ( $pages_data as $page_id => $page_data )
{
   print '<li class="' . $page_data[ 'data' ][ 'page_title' ] . '">' .
            '<a href="' . $page_data[ 'data' ][ 'permalink' ] . '" title="' . $page_data[ 'data' ][ 'page_title' ] . '">' .
               $page_data[ 'data' ][ 'page_title' ] .
            '</a>';

   if ( count( $page_data[ 'children' ] ) > 0 )
   {
      print '<ul id="sub-nav">';

      foreach ( $pages_data as $page_id => $page_data )
      {
         print '<li class="' . $page_data[ 'data' ][ 'page_title' ] . '">' .
                  '<a href="' . $page_data[ 'data' ][ 'permalink' ] . '" title="' . $page_data[ 'data' ][ 'page_title' ] . '">' .
                     $page_data[ 'data' ][ 'page_title' ] .
                  '</a>'
               '</li>';
      }

      print '</ul>';
   }

   print '</li>';
}

Open in new window

0
 
seraph_matrix_631Author Commented:
Hello!
Thank you for getting back to me. The code looks good, and I have tried to troubleshoot the errors but I don't fully understand all the code. The errors are as follows:

In my file, your code (from: $pages_sql = ...) starts on line 6.

The first thing that is shown on screen is:
* H
* T
* /
*
* f
* 0

Then:
Notice: Undefined index: data in nav.php on line 35
Notice: Undefined index: data in nav.php on line 36
Notice: Undefined index: data in nav.php on line 36
Notice: Undefined index: data in nav.php on line 37

*
    * H
    * T
    * /
    *
    * f
    * 0

      Notice: Undefined index: data in nav.php on line 46
      Notice: Undefined index: data in nav.php on line 47
      Notice: Undefined index: data in nav.php on line 47
      Notice: Undefined index: data in nav.php on line 48

*
      Notice: Undefined index: data in nav.php on line 46
      Notice: Undefined index: data in nav.php on line 47
      Notice: Undefined index: data in /nav.php on line 47
      Notice: Undefined index: data in nav.php on line 48
    *

      Notice: Undefined index: data in nav.php on line 46
      Notice: Undefined index: data in nav.php on line 47
      Notice: Undefined index: data in nav.php on line 47
      Notice: Undefined index: data in nav.php on line 48


And it continues with the same error on different lines. The SQL is returning the right data.
I think the problem is in the data array's but I'm probably wrong.

Sorry to be the bearer of bad news and thank you for helping :D Hopefully this can work! :P
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
KysolCommented:
Change:

$pages_ret   = mysql_query( $pages_sql ) or die ( mysql_error() );
$pages_arr   = mysql_fetch_assoc( $pages_ret );

To:

$pages_ret   = mysql_query( $pages_sql ) or die ( mysql_error() );
$pages_arr   = array();
while( $pages_tmp = mysql_fetch_assoc( $pages_ret ) )
{
   $pages_arr[] = $pages_tmp;
}


Sorry about the delay, and original buggy code, I new I messed something up, and it was more receiving the original data than the output which is what you were having the issue with.
0
 
seraph_matrix_631Author Commented:
Its working better and thank you for getting back to me again.
It would seem that the sub-navigation menu is being populated with items from the original query.

The menu is like:

about
    about
    gallery
    blog
    contact
gallery
blog
contact

Sorry to be a pain!
Thank you for all your help, it really is appreciated.
0
 
KysolCommented:
foreach ( $page_data[ 'children' ] as $sub_id => $sub_data )
      {
0
 
seraph_matrix_631Author Commented:
Ohh! That is brilliant!
Within a day and a bit you have found a solution to my problem.

It took me a week of trying different stuff and I couldn't do it!
Thank you so so much! :D

You have been an amazing help, and thank you for taking the time out to assist.
0
 
seraph_matrix_631Author Commented:
Very helpful, on the ball with quick responses.
Very happy :D
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.