Jump to content

Recommended Posts

Hi there,

 

Firstly I will apologise in advance, as I am sure that this question has been asked and answered countless times, as it's a basic implementation of PHP, however I have searched using many varying terms and have come up with nothing... maybe I am searching for the wrong stuff, however hopefully my question and thread title may help others doing similar searches to me in the future, so here goes....

 

 

I have a page with a menu (don't we all, you might reply)

 

I want to click the menu links and instead of it linking to another page, I just want the content of the 'content' div to change to the new page.

 

Years ago you used to do this with frames, but I hate 'em. How do you do it with PHP? I have an inkling it's something to do with 'GET' & 'POST' but not entirely sure, as I'm a novice and have plodded along with includes up until now to do basic stuff. I now want to start learning a little bit more if possible!!!

 

I also think it will give me a URL similar to the forum one... i.e. http://www.mysite.com/index.php?page=the-page-i-am-including.html

 

Thanks in advance!!!

The basics of the code would look like

 

<?php

// check that the page var is present in the url
// eg index.php?page=home.html
if(isset($_GET['page']) && GET['page'] != 'index.php')
{
    // now check to see if the requested file exists
    $reqFile = $_SERVER['DOCUMENT_ROOT'].'/'.$_GET['page'];
    if(file_exists($reqFile))
    {
        include $reqFile;
    }
    // request file does exist. Kill the script display error
    else
        die('404 Page Not Found');
}
// page is present, display the default page of your choice
else
    include 'your_default_page.php';


?>

i use a switch for this matter....

 

switch($_GET['page']){
//List produkter
case 'list' : include('source/list.php');
break;
default: include('start.php'); 
}

 

my links looks like this:

<a href="index.php?page=list">nameoflink</a>

Thank you!

 

How fast was that?! I shall give that a try....

 

I think I can see what's going on there.

 

Just one question - Would this code be used for each of the links in the nav part of the page, or in the content div that is going to change?

 

 

<div id="wrapper">
   <div id="header"></div>

   <div id="nav"><?php include("includes/nav.php") ?></div>

   <div id="content">

<?php

// check that the page var is present in the url
// eg index.php?page=home.html
if(isset($_GET['page']) && GET['page'] != 'index.php')
{
    // now check to see if the requested file exists
    $reqFile = $_SERVER['DOCUMENT_ROOT'].'/'.$_GET['page'];
    if(file_exists($reqFile))
    {
        include $reqFile;
    }
    // request file does exist. Kill the script display error
    else
        die('404 Page Not Found');
}
// page is present, display the default page of your choice
else
    include 'home.php';

?>

   </div>

   <div id="footer">Copyright info</div>

</div>


 

and nav.php would be something like :

 

<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Gallery</a></li>
<li><a href="#">Guestbook</a></li>
<li><a href="#">Contact</a></li>
</ul>

 

So what would the links be?... Similar to fesan's reply above?

 

However... Can you explain this a little more, as I am not too sure what's going on?

 

i use a switch for this matter....

 

switch($_GET['page']){
//List produkter
case 'list' : include('source/list.php');
break;
default: include('start.php'); 
}

 

my links looks like this:

<a href="index.php?page=list">nameoflink</a>

Just one question - Would this code be used for each of the links in the nav part of the page, or in the content div that is going to change?

Yes

 

and nav.php would be something like :

 

<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Gallery</a></li>
<li><a href="#">Guestbook</a></li>
<li><a href="#">Contact</a></li>
</ul>

 

So what would the links be?

No, the link will be like you said earlier, eg http://www.mysite.com/index.php?page=the-page-i-am-including.html

 

The switch checks the value of the variable page. It gets it page variable from the header.

so the header after clicking this link:

<a href="index.php?page=list">nameoflink</a>

will look like this:

http://www.yourdomain.com/index.php?page=list

 

By changing the value in page= you set the different cases for you switch.

I've added another case under so it might be a little more easier to understand.

 

switch($_GET['page']){
case 'list' : include('source/list.php'); // link would have to look like: <a href="index.php?page=list">nameoflink</a>
break;
case 'home' : include('source/home.php'); //link would have to look like: <a href="index.php?page=home">nameoflink</a>
break;
default: include('start.php'); // This is the default page that is loaded. If the $_GET['page'] dont get any values.
}

 

Hope this helps....

I guess that wildteen has a more complex script for including sites. The result will be the same i guess but i don't know if any of these methods is more preferred then the other...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.