Brian S.
Brian S.

Reputation: 21

PHP Template Help

So I believe my previous question was entirely wrong as to what I wanted. My apologies.

Anyway, here is the question that I believe poses my problems: I am currently using a PHP template (I believe that is the correct wording). It is named "index.php" and it holds my layout as well as the following code, which calls the contents of the body, such as "About" "Contact", etc, from a directory called /content/.

<?php  
$default = 'index'; //Whatever default page you want to display if the file doesn't exist or you've just arrived to the home page.  
$page = isset($_GET['p']) ? $_GET['p'] : $default; //Checks if ?p is set, and puts the page in and if not, it goes to the default page.  
$page = basename($page); //Gets the page name only, and no directories.  
if (!file_exists('content/'.$page.'.php'))    { //Checks if the file doesn't exist  
    $page = $default; //If it doesn't, it'll revert back to the default page  
    //NOTE: Alternatively, you can make up a 404 page, and replace $default with whatever the page name is. Make sure it's still in the inc/ directory.  
}  
include('content/'.$page.'.php'); //And now it's on your page!  
?>  

The coding above is within my template's index.php page, and this calls the body contents, as I previously mentioned, such as "About", "Contact", etc. Now my template's page index.php calls the default page 'index.php' from the '/content' directory. However, I want my main index.php (not the one called from /content/) to have a different layout from the rest of my site. How do I achieve this whilst still being able to use the above coding for the content that utilizes a different layout?

... Does this make sense? or Am I just babbling? -- Help would be greatly appreciative.

Upvotes: 0

Views: 102

Answers (3)

Talisin
Talisin

Reputation: 594

If I understand you right you just want to switch the layout from your root index.php and if someone click on your links you will pass a "p" parameter to the root index.php which change the entire layout by loading the files from the /content folder.

<?php  
    if( isset($_GET['p'] ) ) {
        $default = 'index';
        if (!file_exists('content/'.$_GET['p'].'.php')) {  
            $page = $default;
        }
        $page = basename($page);
        include('content/'.$page.'.php'); //And now it's on your page! 

    } else {
            // your root index.php
    }
 ?>  

put this on the top of your root index.php and build your root index.php site in the else condition.

Upvotes: 0

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 175098

Simply make a condition:

if ($page == $default) {
    //different layout
}
else {
    ....
}

Upvotes: 1

Ricardo
Ricardo

Reputation: 173

Is the entire page laid out differently? OR does the header and footer remain the same with the content of the site changing?

If it's the latter, you could set it up in such a way that 2 files (like "header.php" and "footer.php") contain your header code and footer code, and that you have a content div between them. You can print whatever needs to go into the content div depending on what page you are on, in a similar fashion to the way you have your code set up above.

Does that make sense? So you'd have three different include files, one for header, one for footer and one for the page content (depending on what page you're on)

Upvotes: 0

Related Questions