Jump to content

php include - simple code?


Recommended Posts

Hi everyone!

I'm just starting to use PHP and am trying to figure out a simple (i think) code that uses the include() function.  I want the index.php to automatically include the main content page, and then each link on my menu loads in the space where the main content page loaded.  I had something like this, which didn't work:

 

<?php

if ($page && file_exists("$page.htm")) {

 

@ require_once ("$page.htm");

} else {

$page = "misc/intro";

@ require_once ("$page.htm");

}

?>

 

Basically I want it to call up content.php automatically, and when a menu link is clicked on, it loads in that same space without changing index.php.

 

How can I do this?

 

Acorn!

 

Link to comment
Share on other sites

just call   index.php?page=content

This should work...but you may have to pull this value from the "GET" variable (if your server is not set to do this automatically...  make your first line (within teh <?php section of course) the following:

 

$page = $_GET['page'];

 

so your whole script is:


<?php
$page = $_GET['page'];

if ($page && file_exists("$page.htm")) {

@ require_once ("$page.htm");
} else {
$page = "misc/intro";
@ require_once ("$page.htm");
}
?>

 

your links will look like this:

http://www.yoursitehere.com/index.php?page=welcome

http://www.yoursitehere.com/index.php?page=about

http://www.yoursitehere.com/index.php?page=contact

etc...

Link to comment
Share on other sites

one other modification...i always prefer to set up teh COMPLETE filename in a variable ahead of time, rahter than on the fly...this gives me more control, and also allows me to display it for troubleshooting...try this if you are still having trouble...and once it works, comment out the line that begins with "echo" by placing two slash marks before it (//)

 

<?php
$page = $_GET['page'];

$filename = "$page.htm";

echo $filename;

if ($page && file_exists("$filename")) {
   @ require_once ("$filename");
} else {
   $filename = "misc/intro.htm";
   @ require_once ("$filename");
}
?>

Link to comment
Share on other sites

Friendly advice.

 

change the requires to includes and remove the error suppression symbol @ from the start of the lines while you are writing your script so you can see what is going wrong.

 

Also, if you are using apache (I don't know how IIS or other servers do it) then go to the apache logs and check out the last few entries in the error log.

Link to comment
Share on other sites

This is what I used:

 

<a href="index.php?page=green">Green</a><br>

<a href="index.php?page=black">Black</a><br>

<a href="index.php?page=red.htm">Red</a>

<?php

$page = $_GET['page'];

 

if ($page && file_exists("$page.htm")) {

 

@ require_once ("$page.htm");

} else {

$page = "white.php";

@ require_once ("$page.htm");

}

?>

 

I used colors to help see the changes better.

It seems to be working, loading different pages in the same space, like I wanted, but only the text of the linked-to page comes up, and not a different bg color.  Also, if I wanted the title to change with each page/link, is that possible?

Link to comment
Share on other sites

but only the text of the linked-to page comes up, and not a different bg color.  Also, if I wanted the title to change with each page/link, is that possible?

 

Can we see one of these included pages?

Link to comment
Share on other sites

if you view the source of one of your "generated" pages you will see that you have the header twice.  If you truly only want the content of the second page...then your original index.php file should not contain any output...just the PHP include() code.  basically, wherever the include statement is, you will insert the ENTIRE code of the page you are including...so if headers or other info is already set, you end up with extra info and browsers get confused.  If you are looking to include the WHOLE page though...then why not simply navigate to the page directly?...if you are trying to simply have the menu (or other common elements) appear on every page...include those instead of the rest of the page...so (example)...your "contact us" page might look like this (obviously fill in stuff where the ... is):

 

<html>
<head....


<body>

include("page_top.php");
include("menu.php");

<p>
Please contact us at:<br />
123-456-7890<br />
Or Mail:<br />
123 Nowhere Lane
</p>

include("footer.php");

</body>
....

 

 

then your file (example: menu.php) might only contain the following (no header/body tags needed, as it is not ever displayed directly)

 

<p>
  <a href="http://www.yoursitehere.com/welcome.php">welcome</a><br />
  <a href="http://www.yoursitehere.com/about.php">about</a><br />
  <a href="http://www.yoursitehere.com/contact.php">contact</a><br />
</p>

Link to comment
Share on other sites

All right!  It works.  I'm using the following code:

 

<?php

$page = $_GET['page'];

 

if ($page && file_exists("$page.htm")) {

 

@ require_once ("$page.htm");

} else {

$page = "content/home";

@ require_once ("$page.htm");

}

?>

 

If the page is missing, how do I get it to go to an error page or at least display an error message?

 

Also, if I want the title to change with the links, I know there is some php title command.  How do I do that?

Link to comment
Share on other sites

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.