Topic: PHP include() not diplaying contents

EDIT
Okay, this PHP crap has left my brain in a pretzel. I figured out a way to get the same effect with iframes, so I'm just not going to use the PHP include function. When, if ever, I really have to use it, I'll figure it out then.

Anyway, I really appreciate the help from everyone. This is the best web coding forum ever! big_smile

Hello,
I recently learned about PHP includes() and decided to try and use it on a header and footer for a website I'm building. I saved the header contents as a PHP file, and I put the include code into my index.htm page with the correct path name. But for some reason, when I view the index.htm document it doesn't display the contents of header.php. It doesn't give me an error either. I tried to upload it to my hosting to see if it would work then, but again just a blank page.

This wouldn't be the first time PHP has stumped me, and I really appreciate the help. smile

My header.php code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="CSS/navbarAWARE.css">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<div align="center"><img id="logo" src="Images/logoAWAREwhite440.png" width="440" height="396"><br>
</div>
<div id="navcontainer">
<ul id="list-nav">
  <li><a href="index.htm">Home</a></li>
  <li><a href="whoweare.htm">Who We Are</a></li>
  <li><a href="hawkpierce.htm">Hawk Mystery</a></li>
  <li><a href="eduprograms.htm">Training &amp; Educational Programs</a></li>
  <li><a href="rehabworld.htm">A Rehabilitator's World</a></li>
  <li><a href="needslist.htm">Needs List</a></li>
</ul>
</div>
</body>
</html>

The PHP include() code in the index.htm document:

<body>
<div id="container">
<div class="menu"><?php include("header.php"); ?></div>

</div>
</body>

Thanks again,
-Raven

Last edited by rsrwebs (2009-10-29 15:01:04)

Re: PHP include() not diplaying contents

I saved the header contents as a PHP file, and I put the include code into my index.htm page with the correct path name.

In order to use PHP includes, your main page needs to use the .php extension. In this case, you'll need to change index.htm to index.php.

Re: PHP include() not diplaying contents

falkencreative wrote:

I saved the header contents as a PHP file, and I put the include code into my index.htm page with the correct path name.

In order to use PHP includes, your main page needs to use the .php extension. In this case, you'll need to change index.htm to index.php.

Okay, so in order for this set-up to function, everything has to be the PHP extension?

Re: PHP include() not diplaying contents

I just tested it again, saving index.htm to .php. Still nothing.

Perhaps I should say f-it and just use an iframe? I understand those at least! lol

Re: PHP include() not diplaying contents

Are you running this on a server (either on a server on your local machine or uploaded to your web hosting)? If you have uploaded it to your web host, are you sure your host supports PHP?

If you have posted it to your hosting, post a link or send me a private message with the link?

Re: PHP include() not diplaying contents

I looked over your code a bit, and I have to admit I'm a bit confused... Maybe it's that you haven't posted the entire files, but they seem to be set up wrong. For example, with those two files you have two sets of body tags... it just isn't what I would expect.

For example, here is a brief example showing a php include used to include navigation elements:

Index.php file:

<!-- doctype here -->
<html>
<head>
    <title>title</title>
</head>
<body>
  <div id="wrapper">
      <div id="header">
          <!-- logo here -->

          <?php include("nav.php"); ?>

      </div>
      <div id="content">
          <!-- content here -->
      </div>
   </div>
</body>
</html>

nav.php:

<ul id="nav">
    <li>nav item</li>
    <li>nav item</li>
    <li>nav item</li>
    <li>nav item</li>
</ul>

Re: PHP include() not diplaying contents

falkencreative wrote:

I looked over your code a bit, and I have to admit I'm a bit confused... Maybe it's that you haven't posted the entire files, but they seem to be set up wrong. For example, with those two files you have two sets of body tags... it just isn't what I would expect.

For example, here is a brief example showing a php include used to include navigation elements:

Index.php file:

<!-- doctype here -->
<html>
<head>
    <title>title</title>
</head>
<body>
  <div id="wrapper">
      <div id="header">
          <!-- logo here -->

          <?php include("nav.php"); ?>

      </div>
      <div id="content">
          <!-- content here -->
      </div>
   </div>
</body>
</html>

nav.php:

<ul id="nav">
    <li>nav item</li>
    <li>nav item</li>
    <li>nav item</li>
    <li>nav item</li>
</ul>

I was wondering the same thing. From what little I know about PHP and programming in general, the include file should contain just the navbar code and nothing else, as you have rightly shown above, because if the navbar were to be wrapped in body tags, the resulting index.php source would have body tags contained within body tags. (A page can only contain one set of body tags, and html tags for that matter, isn’t that right?).

But now that I mention it, something along the same line of reasoning has me puzzled about Includes: Last night, while viewing the PHP Includes video (part 2) and practicing my coding, I noticed that in order for the declared variable contained in footer.inc.php to work, it must be wrapped in PHP tags. But that doesn't make any sense to me because if I were to put that section straight into my_page.php without using the include function, it would generate an error (as expected) because there would be PHP tags wrapped in PHP tags:

<!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>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <title>Killerphp.com Video Tutorial</title>
    </head>
    <body>
        <h1>PHP INCLUDES</h1>
        <p>
            <?php

//The line of code below would cause a parse error because PHP tags can’t be contained within PHP tags. Yet if I were to place this SAME line of code into an include file (whether that be footer.inc.php, footer.inc or even just footer.txt) and use the include function, no error would be generated and "fish" would be printed to the source. How come?
                <?php $secret_password = "fish"; ?>
                print $secret_password;
            ?>

        </p>
    </body>
</html>

Clearly, there's something fundamental about includes that I’m not getting.

Re: PHP include() not diplaying contents

You can put PHP includes inside other php includes; but your code doesn't have the include function; perhaps it should be like this:-

<h1>PHP INCLUDES</h1>
        <p>
            <?php  include("footer.inc"); ?> 

</p>

and footer.inc file has only this:-

<?php $secret_password = "fish"; 
print $secret_password;
?>

then the first page will show the word fish inside the p tags after two php codes. (I haven't followed all the previous posts so I'm just showing how it could work).

Last edited by Wickham (2009-10-29 11:31:01)

Re: PHP include() not diplaying contents

(A page can only contain one set of body tags, and html tags for that matter, isn’t that right?).

Correct.

I noticed that in order for the declared variable contained in footer.inc.php to work, it must be wrapped in PHP tags. But that doesn't make any sense to me because if I were to put that section straight into my_page.php without using the include function, it would generate an error (as expected) because there would be PHP tags wrapped in PHP tags

If you are using PHP, anything that you expect the server to understand/render as PHP HAS to be within PHP tags. If you have two separate files, with two separate blocks of PHP, they'll each need their own sets of PHP tags in order for the server to properly understand it. I understand that you are including a file, so you might expect that the first set of PHP tags could work for both files, but the server can't assume that the included file necessarily contains PHP code -- it could contain javascript, regular html, or anything.

Re: PHP include() not diplaying contents

falkencreative wrote:

Are you running this on a server (either on a server on your local machine or uploaded to your web hosting)? If you have uploaded it to your web host, are you sure your host supports PHP?

If you have posted it to your hosting, post a link or send me a private message with the link?

I know for a fact that my server is running PHP 5.0. I'm unsure why it won't load.

Re: PHP include() not diplaying contents

falkencreative wrote:

I looked over your code a bit, and I have to admit I'm a bit confused... Maybe it's that you haven't posted the entire files, but they seem to be set up wrong. For example, with those two files you have two sets of body tags... it just isn't what I would expect.

For example, here is a brief example showing a php include used to include navigation elements:

Index.php file:

<!-- doctype here -->
<html>
<head>
    <title>title</title>
</head>
<body>
  <div id="wrapper">
      <div id="header">
          <!-- logo here -->

          <?php include("nav.php"); ?>

      </div>
      <div id="content">
          <!-- content here -->
      </div>
   </div>
</body>
</html>

nav.php:

<ul id="nav">
    <li>nav item</li>
    <li>nav item</li>
    <li>nav item</li>
    <li>nav item</li>
</ul>

So, should I take out the body tags from the file that's being included? This stuff is beginning to make less and less sense to me...

Re: PHP include() not diplaying contents

rsrwebs wrote:

...This stuff is beginning to make less and less sense to me...

If by "stuff" you're referring to PHP in general, take heart, rswebs. Have you viewed the beginner PHP videos by Stef Mischook (frog)? They're the most easy to understand tutorials on PHP I've ever come across. I have tried learning PHP before and quit because it just seemed too overwhelming at the time but after watching Stef's videos, I have resolved to learn as much as I can about it.

Re: PHP include() not diplaying contents

@rsrwebs:
Stefan does have two video tutorials on PHP includes that might be worth a look. They should help clear up most of your confusion.
http://killerphp.com/videos/02_php_incl … ludes.html
http://killerphp.com/videos/02_php_incl … art_2.html

Re: PHP include() not diplaying contents

abajan wrote:
rsrwebs wrote:

...This stuff is beginning to make less and less sense to me...

If by "stuff" you're referring to PHP in general, take heart, rswebs. Have you viewed the beginner PHP videos by Stef Mischook (frog)? They're the most easy to understand tutorials on PHP I've ever come across. I have tried learning PHP before and quit because it just seemed too overwhelming at the time but after watching Stef's videos, I have resolved to learn as much as I can about it.

Hey, thank you! Where can I find these tutorials? smile

Re: PHP include() not diplaying contents

falkencreative wrote:

@rsrwebs:
Stefan does have two video tutorials on PHP includes that might be worth a look. They should help clear up most of your confusion.
http://killerphp.com/videos/02_php_incl … ludes.html
http://killerphp.com/videos/02_php_incl … art_2.html

I really appreciate the help, thanks. smile

Re: PHP include() not diplaying contents

rsrwebs wrote:
abajan wrote:
rsrwebs wrote:

...This stuff is beginning to make less and less sense to me...

If by "stuff" you're referring to PHP in general, take heart, rswebs. Have you viewed the beginner PHP videos by Stef Mischook (frog)? They're the most easy to understand tutorials on PHP I've ever come across. I have tried learning PHP before and quit because it just seemed too overwhelming at the time but after watching Stef's videos, I have resolved to learn as much as I can about it.

Hey, thank you! Where can I find these tutorials? smile

Here's the entire list of the tutorials. You'll notice that not all of them can be linked to from that page. Most of others can be found at Idea22.com with slightly different titles in some instances but they are the same videos. I'll post the direct links when I get a chance.

(EDITED to correct original link to the beginners PHP tutorial.)

Last edited by abajan (Today 05:30:57)

Re: PHP include() not diplaying contents

Hey, basically whats happening when you include something is it literally pulls anything out of your included file and shows it to the browser.

So if you have say...

index.php :

<html>
<head>
</head>
<body>

<?
include("nav.php");
?>

</body>
</html>


And than this is nav.php:

<html>
<head>
</head>
<body>

Nav here

</body>
</html>

Than the code executes like this:

<html>
<head>
</head>
<body>

<html>
<head>
</head>
<body>

Nav here

</body>
</html>

</body>
</html>


And i bet that confuses the hell out of the browser, it could be your problem but i just quickly read over the thread so i could be wrong.

Also i notice sometimes you use <?php  and other times simply <?, i've never used the <?php.  So if <? works, i would stick to just using that one. It has always served me well in the past.

18

Re: PHP include() not diplaying contents

<?php is the suggested method. Not all servers allow short_tags. <? . since it conflicts with the XML tag.

Another thing, to add to what falkencreative posted above, an "included" file is always assumed to contain html and is parsed as such unless you add the php tokens. (<?php   ... code here ... ?>)

Last edited by jlhaslip (Today 02:06:55)