PDA

View Full Version : How to make a quick navigation ?



thientanchuong
06-25-2010, 01:55 AM
Hello there,

I have idea on how to make a quick navigation.

I have 3 pages such as:

index.php
Content.php
AboutMe.php

INDEX.PHP is containning a web skeleton (web layout and images) and navigation menu which have home and about me naviagtion.

My idea is, when user click on home naviagtion in index.php page, it will show details of content.php page on index.php, or user click on about me navigation, details of AboutMe.php will be shown on index.php page

To sum up, I do not want to repeat the web skeleton on different pages.

index page will be the main page, another page will be active and only being shown on index page when user click on similar navigation.

Do you have any source code or tutorial relating to my question ? please, give me source code or the link to learn.

thank you

djr33
06-25-2010, 02:56 AM
The decision for how you approach this will be based on how complex the "skeleton" is.

At the most basic level, you can accomplish all of this in a single command:
<?php include('content.php'); ?>

You'll need to determine which page to include, so you can do it like this:
Use a variable in the URL like ?page=content
Then:
<?php include($_GET['page'].'.php'); ?>

Now, technically you need to verify:
1. That the page variable exists. You can just fix this by doing:
$page = 'default';
if (isset($_GET['page'])) { $page = $_GET['page']; }
2. That the value isn't something that will present a security risk. Here's a way to setup a list of possible pages to verify against:
$pages = array('content','AboutMe'); //list as needed
$page = 'content';
if (isset($_GET['page'])&&in_array($_GET['page'],$pages)) { $page = $_GET['page']; }

That will take the contents of content.php or aboutme.php, etc., and execute them from within index.php.

That will do it all at once, though. So that means you can't have different parts executed at different times if that's how your index.php template is setup.

If you need a more complex approach, you will still use include, but you'll want to then control it more. Use functions inside the pages and call them from within your index.php template. Alternatively, it might be easier to have contentmain.php, contentside.php, contenttitle.php, and you can include them at the right parts in the index.php template page.

I hope this gets you started.



Another way to approach this is to use the other pages as the main page, rather than using the index.php page to serve them all. This gets more complex.
You will need to include the index.php page into the other pages, the opposite of what I explained above. But then you'll need to use it somehow to generate the page. So you could do something else: you could create templatetop.php and templatebottom.php and put your content in the middle (with an include statement at the top and bottom of each page).
Or you can create a function (maybe class) in the index.php page that then can be used in the others. This gets complex if you don't know how to work with functions or classes though.



Finally, you could use the easier first method, but use URL rewriting to hide the URL and make it look like you're using those pages. This can be done using .htaccess and mod_rewrite. It's available on apache servers and requires access to .htaccess files, but that's usually possible (except on every limited hosts). This is very abstract and confusing, but nice when it works out. I wrote a tutorial (in the coding tips and tutorials section) if you want to look at that. It would work for what you're doing, probably. Or you could try a 'real' mod_rewrite tutorial, but be warned it gets pretty confusing.

thientanchuong
07-02-2010, 08:56 PM
Thanks for your help, I find out some ways but other error occur

1. When user view the index.php page, there is nothing to show => details of content.php should be displayed either when user view the website or user click on home button.
2. When I press ABOUT ME menu button, details of content.php are also displayed with details of aboutme.php

This is my code in the first way:


<div id="menu">
<ul>
<li><a href="index.php?page=content" name="content">Home </a></li>
<li><a href="index.php?page=aboutme" name="aboutme">About Me</a></li>
<li><a href="index.php?page=portfolio" name="portfolio">Portfolio</a></li>
<li><a href="index.php?page=contact" name="contact">Contact </a></li>
</ul>
</div>

<?php
if (isset($_GET['page']))
{
if($_GET['page']=="content")
include "content.php";
elseif($_GET['page']=="aboutme")
include "aboutme.php";
elseif($_GET['page']=="portfolio")
include "portfolio.php";
elseif($_GET['page']=="contact")
include "contact.php";
else
include "content.php";
}
?>

Besides that, I also use Switch Case to do quick navigation but error also come up:


<?php

switch($_GET["page"])
{
case "content":
require("content.php");
break;

case "aboutme":
require("aboutme.php");
break;

case "portfolio":
require("portfolio.php");
break;

case "contact":
require("contact.php");
break;

default:
require("default.php");
break;

}

?>


The error of this second way is :


Notice: Undefined index: page in C:\wamp\www\missvn\aa\index.php on line 11

and It is


switch($_GET["page"])

Can any one show me the solutions of 2 ways: using IF ELSE, INCLUDE Method and using SWITCH CASE method ?