• Index
  •  » Articles
  •  » Class for creating the navigation menu of the site

Class for creating the navigation menu of the site

Class for creating the navigation menu of the site

Navigation is one of the most important elements of the web page. Without proper and clear navigation you can’t create a good site because a lot of users come to the site from the search systems. As a result they could see previous pages of the site and if users don’t understand where they are he will probably leave it.

Once I have faced with such a problem and decided t create the same menu type for the whole site and use it on all the pages. Having read a lot of articles on the site optimization I understood that text from the menu should be both in the header and in the body.

In the header:

Name (of the article, book, etc) < Site section < Site name

In the menu:

Site name < Site section < Name (of the article, book, etc)

I decided to write the PHP class for creating and representing the menu on the site. At first I created the approximate structure of the class:

<?php 
class Url_nav /*class for creating the navigation */
{
var 
$link_and_text_list/* link array */
var $delitmer/*link delimiter */

/* item adding */
function add_item($title$s_url)
{
}


/*creating the navigation and header from the special array:
array
(
[0][url] = ?module=art....
[0][title] = title
)
*/
function nav_create()
{
}

/* title creating */
function title_create()
{
}

}
?>

After that I decided to make my class more convenient and clear. Here is the result:

<?php
class Url_nav class for creating the navigation */
{
var 
$link_and_text_list/* link array */
var $delitmer/* link delimiter */


/* item adding */
function add_item($title$s_url)
{
$n=sizeof($this->link_and_text_list);
$this->link_and_text_list[$n]["title"]=trim($title);
$this->link_and_text_list[$n]["url"]=trim($s_url);
}

/* creating the navigation and header from the special array */
function nav_create()
{
$links_arr=$this->link_and_text_list;
$str "";
$c=sizeof($links_arr);
for (
$i=0$i<$c$i++)
{
if (!
$links_arr[$i]["url"])
{
$str .= $links_arr[$i]["title"];
} else {
$str .= "<a href="".$links_arr[$i]["url"]."" target="_self">".$links_arr[$i]["title"]."</a>";
}
if (
$i<$c-1)
{
$str .= $this->delitmer;
}
}
return 
$str;
}

/* title crating */
function title_create()
{
$links_arr array_reverse($this->link_and_text_list);
$str "";
$c sizeof($links_arr);
for (
$i=0$i<$c$i++)
{
$str .= $links_arr[$i]["title"];
if (
$i<$c-1)
{
$str .= $this->delitmer;
}
}
return 
"<title>".$str."</title>";
}
}

/* Example */
$nav = new Url_nav(); 
$nav -> delitmer " < "

$nav -> add_item("items 1""?b=1"); 
$nav -> add_item("items 2""?b=2");
$nav -> add_item("items 3""?b=3");
$nav -> add_item("items 4""?b=4");
$nav -> add_item("items 5""");

echo 
$nav-> title_create(); // creating the title
$nav -> delitmer " > "// changing the delimiter 
echo $nav -> nav_create(); //creating and printing the navigation panel with links
?>

 
  • Top