Jump to content

Recommended Posts

i have this code for a navigation menu and i want to turn it into a menu class with object oriented programming can any body help:

 

	function get_all_subjects($public = true) {
	global $connection;
	$query = "SELECT * 
			FROM subjects ";
	if ($public) {
		$query .= "WHERE visible = 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 pages ";
	$query .= "WHERE subject_id = {$subject_id} ";
	if ($public) {
		$query .= "AND visible = 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 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 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["menu_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["menu_name"]}</a></li>";
		}
		$output .= "</ul>";
	}
	$output .= "</ul>";
	return $output;
}

function public_navigation($sel_subject, $sel_page, $public = true) {
	$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=\"index.php?subj=" . urlencode($subject["id"]) . 
			"\">{$subject["menu_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["menu_name"]}</a></li>";
			}
			$output .= "</ul>";
		}
	}
	$output .= "</ul>";
	return $output;
}

Link to comment
https://forums.phpfreaks.com/topic/264933-navigation-code/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.