Hello Daniweb.com, I was told at the php forum that I should try to write here because I have a dropdown navigation script written in php, but also has some css on it. He believed that it was css which was mad with.

My dropdown navigation works. But it will not shows under the right navigations points. As you can se here on my website. http://www.icmsdesigns.prowdesign.dk/index.php

I hope that the wish to look at it.

// My dropdown script

find_selected_page();
echo "<div id=\"navigation\">";
echo public_navigation($sel_subject, $sel_page);
echo "</div>";

function confirm_query($result_set) {
if (!$result_set) {
die("Database query failed: " . mysql_error());
}
}

function get_all_subjects($public = true) {
global $connection;
$query = "SELECT * 
FROM icms_subjects ";
if ($public) {
$query .= "WHERE active = 1 ";
}
$query .= "ORDER BY position ASC";
$subject_set = mysql_query($query, $connection);
confirm_query($subject_set);
return $subject_set;
}

function get_pages_for_subject($subject_id, $public = true) {
global $connection;
$query = "SELECT * 
FROM icms_pages ";
$query .= "WHERE subject_id = {$subject_id} ";
if ($public) {
$query .= "AND active = 1 ";
}
$query .= "ORDER BY position ASC";
$page_set = mysql_query($query, $connection);
confirm_query($page_set);
return $page_set;
}

function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM icms_subjects ";
$query .= "WHERE id=" . $subject_id ." ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
// REMEMBER:
// if no rows are returned, fetch_array will return false
if ($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}
}

function get_page_by_id($page_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM icms_pages ";
$query .= "WHERE id=" . $page_id ." ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
// REMEMBER:
// if no rows are returned, fetch_array will return false
if ($page = mysql_fetch_array($result_set)) {
return $page;
} else {
return NULL;
}
}

function get_default_page($subject_id) {
// Get all visible pages
$page_set = get_pages_for_subject($subject_id, true);
if ($first_page = mysql_fetch_array($page_set)) {
return $first_page;
} else {
return NULL;
}
}

function find_selected_page() {
global $sel_subject;
global $sel_page;
if (isset($_GET['subj'])) {
$sel_subject = get_subject_by_id($_GET['subj']);
$sel_page = get_default_page($sel_subject['id']);
} elseif (isset($_GET['page'])) {
$sel_subject = NULL;
$sel_page = get_page_by_id($_GET['page']);
} else {
$sel_subject = NULL;
$sel_page = NULL;
}
}

function navigation($sel_subject, $sel_page, $public = false) {
$output = "<ul class=\"subjects\">";
$subject_set = get_all_subjects($public);
while ($subject = mysql_fetch_array($subject_set)) {
$output .= "<li";
if ($subject["id"] == $sel_subject['id']) { $output .= " class=\"selected\""; }
$output .= "><a href=\"edit_subject.php?subj=" . urlencode($subject["id"]) . 
"\">{$subject["subject_name"]}</a></li>";
$page_set = get_pages_for_subject($subject["id"], $public);
$output .= "<ul class=\"pages\">";
while ($page = mysql_fetch_array($page_set)) {
$output .= "<li";
if ($page["id"] == $sel_page['id']) { $output .= " class=\"selected\""; }
$output .= "><a href=\"content.php?page=" . urlencode($page["id"]) .
"\">{$page["page_name"]}</a></li>";
}
$output .= "</ul>";
}
$output .= "</ul>";
return $output;
}

function public_navigation($sel_subject, $sel_page, $public = true) { 
// $output = echo
$output = "<ul class=\"subjects\">";
$subject_set = get_all_subjects($public);
while ($subject = mysql_fetch_array($subject_set)) {
$output .= "<li class=\"subjects_li\"";
if ($subject["id"] == $sel_subject['id']) { $output .= " class=\"selected\""; }
$output .= "><a href=\"index.php?subj=" . urlencode($subject["id"]) . 
"\">{$subject["subject_name"]}</a></li>";
if ($subject["id"] == $sel_subject['id']) {	
$page_set = get_pages_for_subject($subject["id"], $public);
$output .= "<ul class=\"pages\">";
while ($page = mysql_fetch_array($page_set)) {
$output .= "<li";
if ($page["id"] == $sel_page['id']) { $output .= " class=\"selected\""; }
$output .= "><a href=\"index.php?page=" . urlencode($page["id"]) .
"\">{$page["page_name"]}</a></li>";
}
$output .= "</ul>";
}
}
$output .= "</ul>";
return $output;
}


// My Css


#navigation {
background:url("/_images/menu.jpg") no-repeat scroll 0 0 #222222;
height:30px;
line-height:30px;
}
#navigation a {
color: #000000;
text-decoration:none;
}
ul.subjects {
list-style:none outside none;
padding-left:0;
}
ul.pages {
background:none repeat scroll 0 0 #3A8FE9;
border-bottom:1px solid #000000;
border-left:1px solid #000000;
border-right:1px solid #000000;
float:left;
height:auto;
list-style:none outside none;
margin-left:85px;
margin-top:30px;
padding:3px;
position:absolute;
width:100px;
}
.selected {
color:#000000;
font-weight:bold;
}
.subjects_li {
float:left;
margin-left:24px;
}

Recommended Answers

All 2 Replies

wow thats alot of code to do a drop down menu! lol SOrry I dont have a clue about PHP. Do you use Dreamweaver? It has a very nice and easy to use spry navigation tool that will implement dropdowns. It really looks great and you can style them well.

I agree with teedoff...and I do know PHP. You have nested functions producing HTML.
The question is...why?
I know there are times when this is useful,but this code seems to be too complex to do a mundane task! All of the nesting makes it hard to follow and troubleshoot.
In fact I didn't even understand your description of the problem, so I didn't try that hard to figure it out.

If you want others to follow what you are doing, try annotating the code.
Heck, you wrote it,but I bet you forgot what you did yourself!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.