Jump to content

Archived

This topic is now archived and is closed to further replies.

mynamesleo

New here armed with a php question

Recommended Posts

Hi all  ;) I am new to the forum, googled php help and was located here!

I am a complete beginner in PHP, I know some basic [i]html[/i] though.

[b]Question[/b]

On my site I want it to load a page within a page, so I can keep the logo and "naviation" links at the top without having to have that code in every page on the site. A guy a while back did this for me bud sadly my FTP got wiped and I lost all the files.

I remember it was something like; site.com/?p=home, site.com/?p=contact, etc.. etc...


Hope this is enough information for you to know what I mean

Thanks in advance

Share this post


Link to post
Share on other sites
Of course there are many ways to do this, a simple example though. Call this index.php. then name all your other file contact.php, home.php etc etc...

[code=php:0]
// put your header code here.

<?php
if (isset($_GET['p'])) {
  $file = $_GET['p'].'php';
  if (file_exists($file)) {
    include $file;
  }
}
?>

// put your footer code here.
[/code]

Simply call your site via http://yoursite/index.php?p=home

Share this post


Link to post
Share on other sites
Actually I had to edit it on this line:

  [code]$file = $_GET['p'].'php';[/code]

Had to add a . in 'php'

[code]$file = $_GET['p'].'.php';[/code]


Ok now I wanted it to default to home.php whenever the site is visited (site.com) instead of index, I got this so far:

[code]
<?php



if (isset($_GET['p'])) {
  $file = $_GET['p'].'.php';
} else {
include "home.php";
}



if (file_exists($file)) {
    include $file;
  }


?>
[/code]


It seems to work, but is the code OK? I am just starting out in php but program in Visual Basic  ::)

Share this post


Link to post
Share on other sites
I think the logic would be clearer if it were written as
[code]
<?php
if (isset($_GET['p'])) {
    $file = $_GET['p'].'.php';
    if (file_exists($file)) {
        include $file;
    }
}
else {
    include "home.php";
}
?>
[/code]

Share this post


Link to post
Share on other sites

×

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.