www.webdeveloper.com
Results 1 to 7 of 7

Thread: How to use php include properly

  1. #1
    Join Date
    Nov 2013
    Posts
    72

    How to use php include properly

    With the website I am creating, I created a skeleton, if that is the correct word, file that has all the layout HTML. Every page will look the same except for what is in the body of the page.

    This file will include the login/logout code, a side bar that has a survey in it with the results written to a text file, and of course the nav bar and footer.

    I thought I could just use this

    PHP Code:
    <?php include 'header.php'?>
    at the top of the other pages and just have the code only necessary for each page on the other pages. My problem is that I cannot get any of the other stuff from the other pages go into the body where I want it. It just goes off to the side or in come cases, underneath everything else on the page.

    I can show the code if need be, but is there a special that this needs to be done?

  2. #2
    Join Date
    Aug 2006
    Posts
    1,934
    You'd need to post the code, but it's pretty hard to imagine all that stuff being included at the top of an html file, and still working. For example, usually a footer will appear toward the bottom of the html, not at the top.

  3. #3
    Join Date
    Nov 2013
    Posts
    72
    So I figured out what I as doing wrong mostly thanks to what tracknut said. "being included at the top of an html file"

    I thought I would share my error in case anyone else ever has the same problem.

    As tracknut said, included is only for the top of html, so first off, I could not include the footer in that file. I still have to add that to each page. The other thing I was doing wrong was with the html itself. I was opening and closing all html tags in each file and that does not work. Only the file that is being included should contain the doctype, head, title, and the opening html and body tags. Also, the html and body tags cannot be closed in the included file nor can the main container div if you happen to have one of those, which I did.

    So basically what is happening in my case is that I am starting an html file in the included header file and completing the file in all the other files.

    Thanks tracknut!

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,529
    You can include() at any point in the file (as long as it makes sense to include whatever you are including at that point).
    PHP Code:
    <?php
    include $_SERVER['DOCUMENT_ROOT'].'/includes/global_settings.php';
    include 
    $_SERVER['DOCUMENT_ROOT'].'/includes/page_header.php';
    ?>
    <h2>Content Specific to this page</h2>
    <p>The quick brown fox jumped over the lazy dog.</p>
    <?php
    include $_SERVER['DOCUMENT_ROOT'].'/includes/ad_banner.php';
    ?>
    <h3>Rest of page</h3>
    <p>blah blah blah . . . </p>
    <?php
    include $_SERVER['DOCUMENT_ROOT'].'/includes/page_footer.php';
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Nov 2013
    Posts
    72
    Quote Originally Posted by NogDog View Post
    You can include() at any point in the file (as long as it makes sense to include whatever you are including at that point).
    PHP Code:
    <?php
    include $_SERVER['DOCUMENT_ROOT'].'/includes/global_settings.php';
    include 
    $_SERVER['DOCUMENT_ROOT'].'/includes/page_header.php';
    ?>
    <h2>Content Specific to this page</h2>
    <p>The quick brown fox jumped over the lazy dog.</p>
    <?php
    include $_SERVER['DOCUMENT_ROOT'].'/includes/ad_banner.php';
    ?>
    <h3>Rest of page</h3>
    <p>blah blah blah . . . </p>
    <?php
    include $_SERVER['DOCUMENT_ROOT'].'/includes/page_footer.php';
    That makes sense. It can be used anywhere, I just have to put it where I want it to show up on the page. In my case, I could create a footer file and just include that at the bottom of each page?

  6. #6
    Join Date
    Nov 2013
    Posts
    3
    hope this code helps you:

    form.php

    <img src="<?php echo WEB_ROOT; ?>">



    include.php
    <?php
    define('WEB_ROOT', '../'); // relative path to /
    include('form.php');

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,529
    Quote Originally Posted by wevie View Post
    That makes sense. It can be used anywhere, I just have to put it where I want it to show up on the page. In my case, I could create a footer file and just include that at the bottom of each page?
    Yes. Another approach is to define your common page elements as PHP functions within one file. Then you just include that file (actually you probably want to require_once() that file), then call each function as you need it. One benefit of this is that you can add parameters to those functions, such as $title, $description, and $keyword parameters for a function that will output the HEAD section of the page. Then your actual page might look something like:
    PHP Code:
    <?php
    require_once $_SERVER['DOCUMENT_ROOT'].'/includes/page_functions.php';
    echo 
    page_head('Page Title''This is a test page''test,html,php');
    ?>
    <p>This is a test. It is only a test.</p>
    <?php
    echo page_foot();
    ?>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles