Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts

    Templates and Includes

    I've mostly use Dreamweaver templates for content that is the same on each page. I can see a lot of large websites might prefer something else as you have to upload all the files each time? Whereas if you use PHP includes you can just upload that one or two files.

    I was wondering if this is the correct way to set up the includes:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    </head>
    <body>
    <div id="container">
    <?php include("header.php"); ?>
    <?php include("leftside.php"); ?>
    <div id="content">
    <h1>Content</h1>
    <p>Something here</p>
    </div>
    <?php include("footer.php"); ?>
    </div>
    </body>
    </html>
    Assume the leftside is floated to the left and the content area is floated right beside it. Let's say I wanted to add a right sidebar or get rid of the container, or add something around the container. Wouldn't this mean I would need to open every single one of my webpages and add another include, or to edit an element? But if I were using a template and I had the editable region as the content only, then I could easily change the layout. Maybe this is not how I would use the includes? What would be the proper way to accommodate for any future changes of the layout?

  • #2
    The Apostate Apostropartheid's Avatar
    Join Date
    Oct 2007
    Posts
    3,215
    Thanks
    16
    Thanked 265 Times in 263 Posts
    You mean like get the content dynamically? That’s perfectly possible, but you’d need knowledge with SQL or a similar database. I guess you could add the sidebar to an existing include.

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,495
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Your code example above is the correct way to use includes.
    And you're basically saying how it normally gets used.

    I think the part you're not getting, is that the "includes" don't need
    to be "fixed" files ... they can be variables.

    Take your script for example, you have:
    <h1>Content</h1>
    <p>Something here</p>
    </div>

    What if you did this?
    <h1>Content</h1>
    <p>
    <?php
    $content="c".$_GET['c'].".db";
    include("$content");
    ?>
    </p>
    </div>

    Now, when you call your page, you add a variable to the URL,
    like: index.php?c=6

    The "include" is now a text file called: c6.db
    You could have 50 text files c1.db, c2.db. c3.db ...

    So, to expand more on that ... what if your text files were written
    articles that were saved by date, and you only wanted to show
    the one for today?

    you have files like these ...
    20080501.txt
    20080502.txt
    20080513.txt

    <h1>Content</h1>
    <p>
    <?php
    $content=date("Ymd").".txt";
    include("$content");
    ?>
    </p>
    </div>

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I think you are misunderstanding how includes are meant to work. If done correctly you would really need only one page to begin with however in what you are describing includes would be useful if you had a menu on your site. Rather than opening up each file and editing the menu you simply edit one include file. This is how the index page of my site is setup.
    PHP Code:
    <?php
    # default page
    $default 'inc/home.php';
    # list of all site pages + the id they will be called by
    $pages = array('about' => 'inc/about.php','contact' => 'inc/contact.php','portfolio' => 'inc/portfolio.php','tutorials' => 'inc/tutorials.php','thankyou' => 'inc/thankyou.php','error_404' => '404.php');
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>untitled</title>
    <meta http-equiv="content-type" content="application/xhtml+xml;charset=utf-8" />
    <meta name="description" content="" />
    </head>
    <body>
    <?php include('inc/header.php'?>
            <div id="content">
                <?php
                
    if(array_key_exists($_GET['page'], $pages))
                {
                    foreach(
    $pages as $pageid => $pagename)
                    {
                        if(
    $_GET['page'] == $pageid && file_exists($pagename))
                        {
                            
    /* if somebody's making a request for ?page=xxx and
                            the page exists in the $pages array, we display it
                            checking first it also exists as a page on the server */
                            
    include $pagename;
                          }
                       } 
    // end foreach
                
    }
                else
                {
                          
    /* if the page isn't listed in $pages, or there's no ?page=xxx request
                          we show the default page, again we'll also just make sure it exists as a file
                          on the server */
                          
    if(file_exists($default)) include $default;
                }
                
    ?>
            </div>    
    <?php include('inc/footer.php'); ?>
    </div>
    </body>
    </html>
    Basically the urls to my pages look something like http://mysite.com/index.php?page=contact
    and the contact.php file inside of the includes folder is pulled up.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by mlseim View Post
    I think the part you're not getting, is that the "includes" don't need to be "fixed" files ... they can be variables.
    I think you are misunderstanding how includes are meant to work. If done correctly you would really need only one page to begin with however in what you are describing includes would be useful if you had a menu on your site. Rather than opening up each file and editing the menu you simply edit one include file. This is how the index page of my site is setup.
    Yea, still new to PHP

    That's pretty cool. So basically if I have 100 pages that need the same layout, I can simply just edit that one index.php page, and add/remove any includes where I want them. I notice a lot of sites that have all separate php pages (index.php, contact.php, feedback.php, etc.) instead of the using index.php?page=whatever. So does that mean if those websites only had a left side bar and wanted to add a right side bar to all their pages, they'd have to open all their files and add the new include to them? I guess those are poorly designed ones I saw?

    What would be the difference in using txt, db, php? And for the method aerospace mentioned, is that also good for a large website or just a small website? Let's say you have a hundred or even a thousand pages on your website, you would put all hundred or thousand pages in that array?
    Last edited by PeaTearGriffin; 05-02-2008 at 05:27 PM.

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,495
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Large websites get into using a database SQL, MySQL

    File extensions not important ... .txt, .db, .dat whatever
    I was just indicating they were simple text files.

    Some sites do use separate pages for the main ones, and then a
    database for the "online store", or "gallery", etc.

    There are so many combinations to the methods you can use,
    hard to say which one would be best for you.

    You're concerned about the layout changing between pages ... well,
    you can use PHP scripting to control how the page displays, with or without
    the side bars. It can still be done with one script (such as index.php).

    Some people get concerned about using variables in a URL, such as "index.php?page=6"
    If that's a concern, there are ways to eliminate or reduce that by using .htaccess
    "rewrite rules". That's a whole different topic though.

    I usually keep my "index.php" page as one page, and all of the others are
    dynamically generated ( like page.php?p=2&id=123 )... I guess it's a personal preference.

    The method you choose will depend on how many pages, how they are layed-out
    with navigation, and how much data is stored as content (MySQL or simple text files?).

    Also know that you can make the navigation part an "include" too. If your navigation
    menu (or menu bar) is a PHP script, it can control the style (colors, etc) of the links.
    All pages will use the same navigation script.

    It's so abstract, that you can pretty much do whatever you want.

  • Users who have thanked mlseim for this post:

    PeaTearGriffin (05-05-2008)

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Quote Originally Posted by PeaTearGriffin View Post
    Let's say you have a hundred or even a thousand pages on your website, you would put all hundred or thousand pages in that array?
    You could use a database as stated and then just call the appropriate page that the database contains and then display the page. On a site I just redid I used this.
    PHP Code:
    if ($handle opendir('inc')) {
        while (
    false !== ($file readdir($handle))) {
            if (
    $file != "." && $file != "..") {
                
    $pages[basename($file,'.php')] = 'inc/'.$file;
            }
        }
        
    closedir($handle);

    It builds an array from the files in a directory called 'inc'. Much easier than adding each page to the array manually.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    PeaTearGriffin (05-05-2008)

  • #8
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts
    Ok cool that is very useful. How would I set a unique title on each page? I have tried making a variable $title in the included file and echoing it out in the index, but that didn't really work. Last question about the includes is how do websites usually use cookies/sessions with it? Since the code for creating the cookie/session needs to be at the top of the code.

    I am doing some tutorials atm, and basicly we start off with checking if the submit button doesn't exist, then we show the form data, otherwise, the data is stored as a cookie and results are printed. We are also using die() if a requirement is not met, which would mean the footer wouldn't be included. Maybe there is a better way to do things...

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,495
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    How many pages do you have that need titles?

    You're starting to know too much ... which is a good thing, but it means
    that you're wanting to add more and more features. That might change
    our answers. If your site is going to grow into a huge CMS system,
    we will be steering you into another direction.

    Define ahead of time how big your site will be, the purpose of your site,
    and the required features. This might not be a simple thing anymore.

  • #10
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts
    Hehe, well I actually am only practicing php from my own computer. But just hoping that I am doing things as efficient as possible. I guess I won't worry about this for now. Thanks for the help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •