Forum Moderators: coopster

Message Too Old, No Replies

Nest Menu Code Not working

Nest Menu Code Not working

         

ecnaralc

7:50 am on Oct 26, 2009 (gmt 0)

10+ Year Member



Hi! guys.
I am trying to impliment the following into my website.
For some reason nothing shows on the page from the database.
Not sure if its the login details or something further in the code. Not sure where to put anything to make it print etc. Any guidence would be appreaciated.

<?php
$hostname_logon = "localhost" ;
$database_logon = "sebastian" ;
$username_logon = "root" ;
$password_logon = "" ;
//**********************
$connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die ( "Unable to connect to the database" );
mysql_select_db($database_logon) or die ( "Unable to select database!" );

function get_page(){
$fn = explode("/", $_SERVER['PHP_SELF']);
$num_of_s = count($fn) - 1;
$fn = "$fn[$num_of_s]";
$query = "SELECT id, pid, title, page_name FROM menu_data WHERE page_name = '$fn'";
$result = mysql_query($query) or die("Query failed: $query<br>" . mysql_error());
$num_results = mysql_num_rows($result);
$row = mysql_fetch_array($result);

return array($row['id'], $row['pid'], $row['title']);
}

function print_child($id, $sid){
$peer_query = "select * from menu_data where pid = $sid";
$peers = mysql_query($peer_query) or die("Query failed: $query<br>" . mysql_error());
while ($prow = mysql_fetch_array($peers)){
if($id == $prow['id']){
echo '<img alt="" src="images/bullet-6.png" height="15" width="20">'.$prow['title'].'<br>';
}else{
echo '<img alt="" src="images/bullet-6.png" height="15" width="20"><a href="'.$prow['page_name'].'">'.$prow['title'].'</a><br>';
}
}
}

function menu($menu_info) {
$query = "select * from menu_data order by id";
$result = mysql_query($query) or die("Query failed: $query<br>" . mysql_error());;
$num_results = mysql_num_rows($result);

for ($i=0; $i < $num_results; $i++){
$row = mysql_fetch_array($result);
if($menu_info[0] == $row['id'] && $row['pid'] == 0){
echo $row['title'].'<br>';
print_child($menu_info[0], $menu_info[0]);
}elseif($menu_info[0] == $row['id'] && $row['pid'] == $menu_info[1]){
print_child($menu_info[0], $menu_info[1]);
}elseif($row['pid'] == '0') {
echo '<a href="'.$row['page_name'].'">'.$row['title'].'</a><br>';
}
}
}
?>

Zipper

2:28 pm on Oct 26, 2009 (gmt 0)

10+ Year Member



Where exactly are you calling any of these functions from? The code above has a bunch of functions, but no calls.

ecnaralc

8:14 pm on Oct 26, 2009 (gmt 0)

10+ Year Member



Okay.
I think this is supposed to call it...
menu(get_page());
But I don't know where to place it, or how to use it.. thanks for your reply

Zipper

9:04 pm on Oct 26, 2009 (gmt 0)

10+ Year Member



You got it!

Just put it after the mysql_select_db() function or at the end of the page.

ecnaralc

9:50 pm on Oct 26, 2009 (gmt 0)

10+ Year Member



Okay. Thanks for the help, it now works.
Everything works on pid=0 in the database. If I give something a pid of 1 in the database nothing is printed in the menu.
I was hoping to use this to make up various categories:
pid 0 would equal say Books with a few entries.
Then pid 1 would be Planes etc, with a few entries or maybe a dozen. Then to put this in a horizontal tool bar on my website - using suckerfish css to run the whole show.
Do you have any ideas on how to impliment this this.
Thanks

Zipper

10:53 pm on Oct 26, 2009 (gmt 0)

10+ Year Member



Try using the while loop in the menu() function just like you've done in the print_child() to iterate through the dataset.

Let me also suggest that in order to do this the right way, you should make some changes to the database. You will need two tables, one for categories and the other for links/pages. Here's a sample structure.

Categories { cat_id, cat_name }
Pages { page_id, cat_id, page_name }

This was you can easily build your menu by mapping all the links to each category.

ecnaralc

11:39 pm on Oct 26, 2009 (gmt 0)

10+ Year Member



Hi! Zipper.
I sent you a sticky mail...
Just wondering did you get it