Jump to content


Photo

New here armed with a php question


  • Please log in to reply
4 replies to this topic

#1 mynamesleo

mynamesleo
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 24 October 2006 - 06:29 PM

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 html though.

Question

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



#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 24 October 2006 - 06:34 PM

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...

// 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.

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

#3 mynamesleo

mynamesleo
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 26 October 2006 - 01:44 PM

Thanks that works perfect

#4 mynamesleo

mynamesleo
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 27 October 2006 - 10:13 PM

Actually I had to edit it on this line:

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

Had to add a . in 'php'

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


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:

<?php



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



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


?>


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

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 27 October 2006 - 11:18 PM

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users