Jump to content

PHP Include- Dynamic/Default content


RavenAoki

Recommended Posts

Alright, im a super noob and I have not touched PHP for some time. But I've recently needed to get back into it to build a website and i've gotten myself a good bit frustrated on what to do here. Basicly (as simple as this is, and I USED to be able to do it.) I need a php include script that will take the url (IE: www.website.com/index.php?id=page.html) and include the file "Page.html" into the index.php where the include script is. 

 

Now, I got THAT part working. The issue i'm now having is I would like to have a default page (IE: Content.html) that will load if there is no defined "id" (IE: if you visit www.website.com/index.php, I want it to be as if you visted index.php?id=content.html by default, unless there is a defined ID.

 

Here is what I have sofar..

 

<?php
include $id = $_GET['id'];
?>
 
 
((Sorry if im posting this in the wrong area, or something. I'm new to the forums and desperate for assistance... :c ))
 
Link to comment
Share on other sites

Hi Raven,

 

I don't fully understand what you mean, but my only guess is that if the ID is not defined, then you want to automatically include the "content.html"; really I need to see the rest of your code to see exactly how you get the page from the ID, or if you're trying to grab someone else's website from the ID...

 

You would need to use an if statement to validate that the ID is not null, the easiest way to do this is probably...


$id = $_GET['id'];

if (!isset($id))
{
    // The ID is not set, therefore we would then load the "Content.html" page...

} else {

    // The ID IS set, therefore we continue with the script as normal...

}
Link to comment
Share on other sites

Thanks for the quick reply :D

 

You are correct, sorry if my original post was a bit confusing. Basicly if there is no defined id, I want it to load a default page.

 

As for the whole script, that really is it! Its inserted into an index page, and its manipulated by the url.

 

 

As for what you posted, where is the content.html page defined in that code? I see the if function (and i tried to use if earlier.. but alas, I screwed it up and kept breaking my server..) but I dont see where it will put the include 'content.html' in the else statement?

Link to comment
Share on other sites

Basically you need a script which will tell your application that if the ID is defined, then you need to check to ensure that the html file exists and then if it does, show that html file. Do you require the code for that? Because it all depends on your directory structure and the way your application needs to work, but the IF statement mentioned before will allow you to initially check for a valid ID input, and if so then it'll show the file, but you need some extra code to load the HTML files.

Link to comment
Share on other sites

Now i'm a bit confused by that last post?

 

Im not quite sure how to explain it.. ill try my best.. Basicly if someone goes to my website, lets say they type in www.website.com (it will automaticly go to www.website.com/index.php, because that is the home page.)

 

On that page, I have the php include script that will check to see if there is a defined "id". Obviously, there is no defined id, so the page will not display anything. Once a user clicks a link, the link will change the url to something like www.website.com/index.php?id=contactus, or www.website.com/index.php?id=home, etc.

Upon doing so, the space where the php include script is will be filled with "contactus.html" or "home.html" etc.

 

What I need is basicly the script to include a default page, when the id is not defined (Basicly when they first visit the website, i need it to load home.html, even if the url is not "index.php?id=home")

 

If that makes any more sense >.o?

 

((Edit: I can link the website or so, with a few experimental url's so that you can see what im trying to do, if that will help explain things?))

Edited by RavenAoki
Link to comment
Share on other sites

Actually.. I think i just got it working!

 

<?php
include $id = $_GET['id'];
if (!isset($id))
{
include "home.html";
} else { 
}
?>
 
Seems to work fine, it took me a moment to realize what you had said. Either way, thank you, your a life saver! :D
Link to comment
Share on other sites

No worries! It's basically like a routing system, in the "else" tags you'll need to create something that allows the application to load the HTML file according to the GET request (in the URL, e.g ?id=pagehere.html. You'll have to create a validation too which checks to see if the html exists in correspondance with the ID request...

 

For example:

 

If user has "index.php?id=pagehere.html" and "pagehere.html" is in your directory, then include it, else you'll show a 404.html template, if that makes sense? You can normally code that using the "file_exists" method in PHP.

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.