Jump to content


Photo
- - - - -

PHP Navigation


  • Please log in to reply
2 replies to this topic

#1 Alux

Alux

    Young Padawan

  • Members
  • Pip
  • 1 posts

Posted 12 January 2007 - 11:53 AM

<?
	$page_variable = "id";
	//change "id" to whatever you want the page variable to be - this relates to the URLs so if id is the variable, the URLs should comply with index.php?id=page where page is the page we want to include. If it was 'page', the URLs would be index.php?page=page where the second page is the page we want to include.
	
	$page_to_include = $_GET[$page_variable];
	//where we get the bit after the = - for example, if you kept 'id' as the page variable and the url was index.php?id=helloworld, helloworld would be the page to include
	
	$home_path = "/home/username/public_html/";
	//this is your home path - basically, where all your files are kept, ending with a forward slash. This can be blank but that's not very secure. If you use cpanel, it's more than likley your home path is /home/YOUR CPANEL USERNAME/public_html where YOUR CPANEL USERNAME is.. well, your cpanel username
	
	$default_page = "home.php";
	//the page to include if there is no page defined in the URL - in this example, home.php
	
	$default_extension = ".php";
	//the extension of pages that'll be included if they exist, starting with a dot - eg, if it was '.php' and your page variable was 'id' and the URL was index.php?id=helloworld, helloworld.php would be included
	
	$error_page = "error.php";
	//the page to include if the file doesn't exist
	
	if($page_to_include == NULL) {
	//if $page_to_include is blank
	
		include($home_path.$default_page);
		//include the default page
		
	}
	//end the 'if $page_to_include is blank' possible outcome
	
	else {
	//else, the $page_to_include isn't blank and we continue!
	
		if(file_exists($home_path.$page_to_include.$default_extension)) {
			//if the $page_to_include.$default_extension exists
			
			include($home_path.$page_to_include.$default_extension);
			//then we include it!
			
		}
		//end that possible outcome
		
		else {
		//the page doesn't exist!
		
			include($error_page);
			//include the eror page!
			
		}
		//end this outcome
		
	}
	//end the else on wether or not the $page_to_include is blank
?>

Was browsing techtuts & noticed the only tutorial on a php nav was quite overwhelming for a newbie, so, I coded this.

It's pretty basic & a lot of it could be cut out, probably to a line or two, but it was to help newbies.

To use, stick it in your index page (wherever you want the content to show up), create the pages with content as usual, just, only the content, no <html>~<body> & </body>~</html> along with ignoring the template code, then, if the page was helloworld.php and the page variable was left as id, your url would be index.php?id=helloworld

It checks to see if the page exists within your hosting & if the page actually exists, so its pretty secure.

It's more of a heavy commented block of code than a tutorial but still, I think it could be of some help so i'll still post it here

:)

Edited by Alux, 12 January 2007 - 11:54 AM.


#2 smart-coder

smart-coder

    Young Padawan

  • Members
  • Pip
  • 16 posts

Posted 29 January 2007 - 08:31 PM

You dont necessarly have to do it that way, My preferable way is to have index.php? and then have ids such as id=index
and use switch(); functions and cases to create my pages. But its sometimes difficult to have sites run off of one page. But sometimes its worth changing 1 page rather than 20.

#3 Arutha

Arutha

    Young Padawan

  • Members
  • Pip
  • 144 posts
  • Gender:Male
  • Location:Southampton, England

Posted 19 February 2007 - 04:59 PM

You dont necessarly have to do it that way, My preferable way is to have index.php? and then have ids such as id=index
and use switch(); functions and cases to create my pages. But its sometimes difficult to have sites run off of one page. But sometimes its worth changing 1 page rather than 20.


Agreed, the switch function is usually used to get this effect however its nicely structured code, well commented and explained and looks from first glance like it would work. Nice tutorial ;).

Arutha




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users