Hi. I am attempting to develop a couple of functions that will allow me organize and re-organize my navigation menu after I edit or add a new page to my website using my CMS.
Has anyone successfully done somthing like this before? if so please share your method.
What I have below works for the most part, but I am looking to make things work better.
- note* Throughout the code you will find the 'title_level' varable and column title, this will hold a number 1 to however many results are in the database and is used to place in order each link/button in the navigation.
First, I have my functions that display my Navigation Heirarchy.
if (! (function_exists('nav_display')) ){
function nav_display($parent, $level) { /* displays the actual navigation every visitor see's */
// retrieve all children of $parent
$result = mysql_query('SELECT navigation.id, navigation.link_loc, navigation.btn_txt, navigation.title_level, pages.status
FROM navigation INNER JOIN pages ON navigation.link_loc = pages.page_name
WHERE navigation.parent="'.$parent.'" ORDER BY navigation.title_level ASC;'); //AND pages.status="enabled" --> taken out b/c this is admin
// display each child
while ($row = mysql_fetch_array($result)) {
// indent and display the title of this child
if ($row['title_level'] == 'yes'){
echo str_repeat(" ",$level) ."<b>". $row['btn_txt'] . "<b/><br>";
}else{
echo str_repeat(" ",$level) ."<a href=\"index.php?PGNM=page&page=".$row['link_loc']."\" id=\"nav\">" . $row['btn_txt'] . "</a><br>";
}
// call this function again to display this child's children
nav_display($row['id'], $level+1);
}
}
}
// Navigation Hierarchy Display in <select>
if (! (function_exists('new_hierarchy')) ){ // Displays <select> for the Admin when creating a new page
function new_hierarchy(){
// retrieve all pages and put them in order
$result = mysql_query('SELECT btn_txt, title_level FROM navigation ORDER BY title_level ASC;');
// display the pages in a <select>
while ($row = mysql_fetch_array($result)) {
if ($row['title_level'] == 1){
echo "<option value=\"". 1 ."\">". "BEFORE " . $row['btn_txt'] ."</option>";
echo "<option value=\"". ($row['title_level'] +1) ."\">". "AFTER " . $row['btn_txt'] ."</option>";
}else{
echo "<option value=\"". ($row['title_level'] +1) ."\">". "AFTER " . $row['btn_txt'] ."</option>";
}
}
}
}
// Update Navigation Hierarchy
if (! (function_exists('update_hierarchy')) ){ /* displays <select options>'s for an existing page */
function update_hierarchy($cur_level){
// retrieve all pages and put them in order
$result = mysql_query('SELECT btn_txt, title_level FROM navigation ORDER BY title_level ASC;');
// display the pages in a <select>
while ($row = mysql_fetch_array($result)) {
if ($row['title_level'] < $cur_level){
echo "<option value=\"". $row['title_level'] ."\">". "Place BEFORE " . $row['btn_txt'] ."</option>";
}elseif($row['title_level'] > $cur_level){
echo "<option value=\"". $row['title_level'] ."\">". "Place AFTER " . $row['btn_txt'] ."</option>";
}else{
echo "<option value=\"". $cur_level ."\">". "Keep at SAME level " . $row['btn_txt'] ."</option>";
}
}
}
}
Second, I have a <select> where the User in the CMS will choose where they would like either their new page or exisitng page to be placed BEFORE or AFTER.
Where the Functions are Called
<select name="hierarchy">
<option>Select One</option>
<option>---------</option>
<?
if (isset($data['title_level'])){
update_hierarchy($data['title_level']);
}else{
new_hierarchy();
}
?>
<option value="null">Do Not Display in Navigation</option>
</select>
Third, I have the Validation and the entering/updating of the 'navigation' table.
Update
// Determine if this is an edit or new entry.
if (isset($_GET['page'])){ // Set-up & run Queries for an Update
// Grab conditional page name
$page_name = $_GET['page'];
// Find out where to place this in the navigation
$sel_level = $data['hierarchy'];
$cur_level = $data['cur_level'];
// Update the new Navigation Order if necessary.
if($sel_level == $cur_level){
$new_level = $cur_level;
}if($sel_level == "null"){
$new_level = "null";
}else{
// Do the Math
$difference = $sel_level - $cur_level;
$new_level = ($cur_level + $difference);
// Make room for new position
if ($sel_level < $cur_level){
// if placing BEFORE current position
$rewrite_levels = "UPDATE navigation SET title_level = (title_level + 1)
WHERE title_level >= $new_level AND title_level < '$cur_level'";
}elseif(sel_level > $cur_level){
// if placing AFTER current postition
$rewrite_levels = "UPDATE navigation SET title_level = (title_level - 1)
WHERE title_level > '$cur_level' AND title_level <= '$new_level'";
}
mysql_query($rewrite_levels) or die (mysql_error());
}
// Update 'navigation' Table & insert into new position
$level_up = "UPDATE navigation SET link_loc = '$page_name', btn_txt = '$btn_txt', title_level = '$new_level'
WHERE link_loc = '$page_name'";
mysql_query($level_up) or die (mysql_error());
//... etc...
New Page
// Find out where to place this in the navigation [passed from POST]
$title_level = $data['hierarchy'];
if($title_level != "null"){
// make room for the new page
$open_spot_in_nav = "UPDATE navigation SET title_level = (title_level + 1) WHERE title_level >= $title_level";
mysql_query($open_spot_in_nav) or die (mysql_error());
}
// Insert into 'navigation' Table
$level_ins = "INSERT INTO navigation (link_loc, btn_txt, title_level) VALUES ('$page_name', '$btn_txt', '$title_level')";
mysql_query($level_ins) or die (mysql_error());
With this code i can change the order of the pages and add new pages to the navigation in the correct order.
What I cannot do is....
Add web pages and not display them in the navigation (as stand alone pages).
set-up a hierarchy of pages and append their order...
Main Page 1
Sub Page
Sub Page
Main Page 2
Sub Page
How could I make these improvements?
I hope you can follow my code... if not just ask.
I would be interested in other solutions that accomplish the same thing...
Thanks,
phence