Jump to content

Need Help With Very Simple Include Function!


citrn

Recommended Posts

Hi there!

 

I am a novice when it comes to PHP, hence the reason why I'm having trouble with such a simple aspect of it.

 

I am creating a new website for my company and I am using an index.php file for the main structure of the site as well as the header and navigation. I want to use PHP to include seperate files that will appear in the main content area of the page.

 

This is what I have placed where I want the content to go:

 

<?php include("$id.html"); ?>

 

With the above code, I should be able to navigate to a specific page by using a URL such as http://www.domain.com/index.php?id=page

 

I also need to have a specific page (welcome, news, etc.) load automatically when accessing index.php, so I have placed the following code above my HEAD tag:

 

<?
if ( $id=='' ) {
$id="main";
$l="?";
}
else {
$id="$id";
}
?>

 

The above code does work in the sense that when I access index.php, main.html appears in the main content area.

 

However, the problem I am facing now is that any other file I try to include, results in main.html appearing instead of any other page.

 

For example, if I want index.php to include news.html, I should use the URL http://www.domain.com/index.php?id=news , but main.html keeps coming up. No errors or warnings are being displayed.

 

I am sure I have made an error somewhere in the second code, but I cannot solve this problem.

 

Your kind assistance will be greatly appreciated.

 

Thank you,

 

Robert

Link to comment
Share on other sites

<?php

if (isset($_GET['id'])) {
  $id = $_GET['id'];
  if (file_exists("$id.html")) {
    $page = "$id.html";
  } else {
    $page = "main.html";
  }
} else {
  $page = "main.html";
}

?>

 

Then simply use....

 

<?php include $page; ?>

 

where you want the content to appear.

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.