Skip to content Skip to sidebar Skip to footer

PHP Website Template Include Code

I am trying to have this code on my index.php page so that all I have to do is write short files with nothing but html in them to serve as separate pages of the site. For example:

Solution 1:

First off, a disclaimer about using query strings to modify your home page: it's not at all good for SEO. You'd be much better to use a form of .htaccess magic to create alias subdirectories that pass behind the scenes to your query structure. E.g. /about could be a hidden alias to index.php?id=about.html. Given that you didn't ask this though, I'll keep the how-to out of my answer.

$id = $_GET['id']; is also not going to work for you if the id query parameter isn't set, and though you're checking it, you're also setting the $id variable beforehand.

Try shorthand like this:

<?php 
    $id = isset($_GET['id']) ? $_GET['id'] : 0; //This is equivalent to a long-form if/else -- if $_GET['id'] is set, get the value, otherwise return 0/false
    if ($id)
    { 
        $number=5; include("news/newsfilename.php"); 
    }
    else 
    { 
        include($id.".html"); //also suggesting you strip out .html from your HTML and add it directly to the php.
    } 
?>

And then your html:

<a href="index.php?id=about">About</a> 
<a href="index.php?id=contact">Contact</a>

Solution 2:

I think this may be the result of your first line of code. Since you are trying to get 'id' before checking to see if it isset, if it is not set you are going to get an error.


Post a Comment for "PHP Website Template Include Code"