Jump to content


Photo
- - - - -

Simple PHP Navigation


  • Please log in to reply
28 replies to this topic

#1 Squid

Squid

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:Netherlands
  • Interests:Webdesign, php coding, visual basic programming

Posted 05 November 2005 - 03:56 PM

I've written a tutorial, for two purposes. First purpose, so people can learn how to make a site navigation with php, if they don't know yet. Secondly, so people can tell me how I can improve, or give me some piece of advice. Here's the tut:

Description: In this tutorial we'll learn how to make our site navigation like index.php?page=contact.
Skill: Beginner
Time needed: 15 minutes

As stated before, some experience with html (and php) is required, since we won't be going into too much detail. You can always at any time
ask questions on our forums, send us an e-mail or use the contact form.

Right, we'll start of with the main code:
<?
switch($_GET['id']) {

default:
include('home.php');
break;

case "news":
include('news.html');
break;

case "contact":
include('contact/contact.asp');
break;

}  
?>
You should paste this code into your main index file, on the part where you want your content to be displayed. The next thing you should do is edit the id's and the links. On top of the code it says the following:
default:
include('home.php');
break;
[/code
This is the default page that should be displayed. All the next bits that follow, are the id's with their associated links:
[code]
case "news":
include('news.html');
break;
As you can see, the id 'news' is linked to 'news.html'. You can name any id like you want to call it, and also any link. news.html, news.php, it
all works. Also, files which are located in sub folders can be linked. Watch the 'contact.asp' link. It's located in a folder called 'contact'. Also
this link will work. The id can be changed to whatever you like:
<?
switch($_GET['id']) {
The next thing you would do, is fix all your links in your menu's:

Add the following:
<a href="index.php?id=home">Home<a/>
Remember, once you change the 'id' to something else, like 'page', then you will have to update your links aswell:
<a href="index.php?page=home">Home<a/>
If you have for example a downloads page, you can add the main code posted above to a file called 'downloads.php'. Change 'id' to 'downloadid'
for example, and you can link like this:
<a href="index.php?page=home&downloadid=whatever">Home<a/>
Just play around with it a bit, and I'm sure it will work out for you.

#2 Firebird

Firebird

    Young Padawan

  • Members
  • Pip
  • 16 posts

Posted 05 November 2005 - 09:15 PM

Thats pretty much the same code I use on my site.

#3 Squid

Squid

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:Netherlands
  • Interests:Webdesign, php coding, visual basic programming

Posted 06 November 2005 - 04:13 AM

Yeah I'm sure many use it, since in my eyes it's one of the easiest codes to use.

#4 Chaos_Blader

Chaos_Blader

    Young Padawan

  • Members
  • Pip
  • 18 posts

Posted 06 November 2005 - 04:01 PM

just make sure you dont have any secret files cause someone might use

?id=../../secrets/ftppasswords.ext

#5 Squid

Squid

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:Netherlands
  • Interests:Webdesign, php coding, visual basic programming

Posted 06 November 2005 - 04:34 PM

just make sure you dont have any secret files cause someone might use

?id=../../secrets/ftppasswords.ext

Well that's not going to work, because you need to specify all the ?id= links into the code, so it wouldn't work.

#6 liveman

liveman

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Location:New Jersey

Posted 09 November 2005 - 11:58 AM

why don't you do it like

<?PHP
if (empty($id) || !file_exists('/' . $id . '.php')
 {
  include 'news.php'; //change to w.e
 }
else
 {
 include '/' . $id . '.php';
 }
?>
Thats what I use on my website ... a whole lot easier, and every time you add a page you do not need to add a case statement

#7 Squid

Squid

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:Netherlands
  • Interests:Webdesign, php coding, visual basic programming

Posted 09 November 2005 - 03:43 PM

Yup that's a better code, something I wasn't aware of yet. I'm using it myself now, thank you.

There's a lot of more ways to make such a php navigation. I just made a tutorial about the one I am/was using. Plus in my tutorial you'll learn about includes and the GET function. Slightly more advanced I believe. But the purpose of this tutorial mainly was to see how my tutorial writing skills are. Thanks again anyway :P

#8 Dark

Dark

    Young Padawan

  • Members
  • Pip
  • 68 posts

Posted 19 November 2005 - 08:40 AM

Livemans code but shorter
<?php
$id = $_GET['id'];
(!$id || !file_exists("$id.php")) ? include 'news.php' : include "$id.php";  
?>

A quick explanation on what livemans code does to those who dont know:
Basically it checks if the variable id is empty or if a php file exists with that id, if either condition is true it includes news.php else it includes the requested file

I would recommend the case method as its securer

#9 Ckristian

Ckristian

    Honored X Staff

  • Members
  • PipPipPipPip
  • 1,334 posts
  • Gender:Male
  • Location:A blue-green &quot;planet&quot; Mostly harmless
  • Interests:Programming...

Posted 19 November 2005 - 11:41 AM

I am working on exactly this sort of thing. So the tut helped me out a bit :D Thanks. :D

#10 Squid

Squid

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:Netherlands
  • Interests:Webdesign, php coding, visual basic programming

Posted 24 December 2005 - 04:16 AM

I am working on exactly this sort of thing. So the tut helped me out a bit ;) Thanks. :)


No problem, I'm glad it helped

#11 Nike

Nike

    Young Padawan

  • Members
  • Pip
  • 204 posts
  • Location:Ohio
  • Interests:Controlling the human and and duck race.

Posted 25 December 2005 - 01:28 PM

I fail to see what is so good about using this?

#12 Rob_BCFC

Rob_BCFC

    Young Padawan

  • Members
  • Pip
  • 18 posts

Posted 03 January 2006 - 04:45 PM

I dont understand how you get the extra bits like index.php?id=contact&contactmethod=email.

Any help?

Edited by Rob_BCFC, 03 January 2006 - 04:45 PM.


#13 Squid

Squid

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:Netherlands
  • Interests:Webdesign, php coding, visual basic programming

Posted 04 January 2006 - 04:14 PM

I fail to see what is so good about using this?


There's nothing good about using it. It's just a tutorial, meant to help out people using some PHP variables. It's not a script I made, it's a tutorial, which is designed in the first place to help out people. Anyways, that's all I have to say about it.

I dont understand how you get the extra bits like index.php?id=contact&contactmethod=email.

Any help?


Rob_BCFC. It's not the best method, but if you want such links, you should do the following:

On your index file youçe placed the code script. You've defined the id contact. Now, on your contact.php file, you'll also have to define the code. But this time instead of using id, you'll be using contactmethod. Since you work with cases, you'll have to pre-define everything first.

Example:

You index.php file is looking like this:

<?
switch($_GET['id']) {
default:
include('whatever.php');
break;
case "contact":
include('contact.php');
break;
}  
?>

And your contact.php should then look like the following:

<?
switch($_GET['contactmethod') {
default:
include('whatever.php');
break;
case "email":
include('email.php');
break;
}  
?>

I hope this answers your question.

#14 rus321

rus321

    Young Padawan

  • Members
  • Pip
  • 49 posts

Posted 11 January 2006 - 03:42 AM

ye this is a great tut :D

but isent mod_rewrite better:
its neater and good for SEO

#15 Maximus-x

Maximus-x

    Young Padawan

  • Members
  • Pip
  • 2 posts

Posted 25 January 2006 - 10:59 AM

I used this scipt before. But now i found one problem. When trying to include a file that resides in another folder, say:

case "site9": include('webdesign/site9.htm',);

the page falis to show the jpgs. The jpgs reside in the folder webdesign. So does the htm. It´s weird. Can´t figure it out.

When i used it for the first time, all the php and htms lived in the root folder, and there was no problem with those.
But now i´m using these sub-folders...Any help will be greatly appreciated.

Maximus-x :(

#16 Jynxis

Jynxis

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:The Shadows

Posted 26 January 2006 - 05:05 AM

you need to specify where all the images are


so if an image is in "contact/images/email.gif"

and you include the page "contact/contact.php"

in the contact.php you will need to specify that the images are located in

"contact/images/*.*"

ie <img src="contact/images/email.gif">



probably confused you... but eh.


my way to do this is much eaier.

and uses less files. like 4 total.
auth.inc.php // for login
functions.php // functions such as "pages"
admin.php // admin control panel
index.php // main file.

Edited by PlaGuEX, 26 January 2006 - 05:09 AM.


#17 ICT Helpers

ICT Helpers

    Young Padawan

  • Members
  • Pip
  • 56 posts
  • Location:England

Posted 14 April 2006 - 06:13 AM

I sure am liking the tutorials here. Great advice, thanks :)

#18 fedekiller

fedekiller

    Young Padawan

  • Members
  • Pip
  • 18 posts

Posted 25 April 2006 - 03:16 PM

lol i prefer

<?php
if(isset($_GET['fd']) && isset($_GET['page']) && file_exists($_GET['fd']."/".$_GET['page'].".php")){
include ($_GET['fd']."/".$_GET['page'].".php");
}else if(isset($_GET['page']) && file_exists($_GET['page'].".php")){
include ($_GET['page'].".php");
}else if (empty($_GET['page'])){
include("main.php");//main page
}else{
echo "Error, that page does not exist.
";
}
?>

basycally make a folder called test and a file called testing.php
make yoursite.com?fd=test&page=testing
if it appear it worked :mellow:
and you dont have to add case...blablabla each time u wanna ad a page

#19 ICT Helpers

ICT Helpers

    Young Padawan

  • Members
  • Pip
  • 56 posts
  • Location:England

Posted 30 April 2006 - 07:37 AM

fedekiller, what is the advantage of your code and this post above? To me they do the same thing except yours also checks for a folder. Am I right with that understanding?

#20 fedekiller

fedekiller

    Young Padawan

  • Members
  • Pip
  • 18 posts

Posted 30 April 2006 - 02:27 PM

yes and the other big difference
each time you wanna add a page you ahve to do case(page):include.......
in my code you dont have to make it, just add a new page and call it ad ?page=newpage :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users