Page 1 of 1

include function not working..

Posted: Thu Aug 22, 2002 10:08 am
by Zoram
I have been trying to get the include function to return a section for a template i am constructing. But when I try to use the include function it just returns 1.

Header.php

Code: Select all

<?php

$date = date("F j, Y");

$display = "
<table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr> 
            <td><table width="100%" border="0" cellspacing="0" cellpadding="3">
                <tr> 
                  <td width="38%"><div align="center"><img src="http://www.navigatormag.com/images/TopLogo.gif" width="269" height="60"></div></td>
                  <td width="62%"><div align="center"> 
                      <?php
    require(getenv('DOCUMENT_ROOT').'/Ads/phpadsnew.inc.php');
    if (!isset($phpAds_context)) $phpAds_context = array();
    $phpAds_id = view ('468x60', 0, '', '', '0', $phpAds_context);
?>
                    </div></td>
                </tr>
              </table></td>
          </tr>
          <tr> 
            <td height="15" background="http://www.navigatormag.com/GUI/RowBack.jpg"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr> 
                  <td width="82%"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr> 
                        <td width="7%">&nbsp;</td>
                        <td width="93%"><table border="0" cellspacing="0" cellpadding="0">
                            <tr> 
                              <td><a href="http://www.navigatormag.com/index.php">Home</a></td>
                              <td width="30">&nbsp;</td>
                              <td><a href="http://www.navigatormag.com/Archive.php">Article 
                                Archive</a></td>
                              <td><img src="http://www.navigatormag.com/GUI/VertSpacer.gif" width="15" height="9"></td>
                              <td><a href="http://www.navigatormag.com/Contests.php">Contests</a></td>
                              <td><img src="http://www.navigatormag.com/GUI/VertSpacer.gif" width="15" height="9"></td>
                              <td><a href="http://www.navigatormag.com/Coupons.php">Coupons</a></td>
                              <td><img src="http://www.navigatormag.com/GUI/VertSpacer.gif" width="15" height="9"></td>
                              <td><a href="http://www.navigatormag.com/TravelGames.php">Travel 
                                Games</a></td>
                            </tr>
                          </table></td>
                      </tr>
                    </table></td>
                  <td width="18%"><div align="center">$date</div></td>
                </tr>
              </table></td>
          </tr>
        </table>

";

return $display;

?>
Then on my main page I have :

Test.php

Code: Select all

<?php
$header = include 'http://www.navigatormag.com/Header.php';
?>
then later on...

Code: Select all

<? echo $header; ?>
any suggestions?

Posted: Thu Aug 22, 2002 10:47 am
by llimllib
well, right off the bat, you shouldn't nest <?php ... ?> statements, nor should you use a 'return' statement outside of a function. An include file is directly included in your script, it is not a function. Therefore, neither do you need to 'echo $header'. Once you include it, it's in the file, it needs no echoing, and the variable $display will be available to you directly - not through return values. As I see it, here's what you want:

Code: Select all

<?php

$date = date("F j, Y");

print "
<table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="3">
                <tr>
                  <td width="38%"><div align="center"><img src="http://www.navigatormag.com/images/TopLogo.gif" width="269" height="60"></div></td>
                  <td width="62%"><div align="center">";

require(getenv('DOCUMENT_ROOT').'/Ads/phpadsnew.inc.php');
if (!isset($phpAds_context)) $phpAds_context = array();
    $phpAds_id = view ('468x60', 0, '', '', '0', $phpAds_context);

print "
                    </div></td>
                </tr>
              </table></td>
          </tr>
          <tr>
            <td height="15" background="http://www.navigatormag.com/GUI/RowBack.jpg"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="82%"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td width="7%">&nbsp;</td>
                        <td width="93%"><table border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td><a href="http://www.navigatormag.com/index.php">Home</a></td>
                              <td width="30">&nbsp;</td>
                              <td><a href="http://www.navigatormag.com/Archive.php">Article
                                Archive</a></td>
                              <td><img src="http://www.navigatormag.com/GUI/VertSpacer.gif" width="15" height="9"></td>
                              <td><a href="http://www.navigatormag.com/Contests.php">Contests</a></td>
                              <td><img src="http://www.navigatormag.com/GUI/VertSpacer.gif" width="15" height="9"></td>
                              <td><a href="http://www.navigatormag.com/Coupons.php">Coupons</a></td>
                              <td><img src="http://www.navigatormag.com/GUI/VertSpacer.gif" width="15" height="9"></td>
                              <td><a href="http://www.navigatormag.com/TravelGames.php">Travel
                                Games</a></td>
                            </tr>
                          </table></td>
                      </tr>
                    </table></td>
                  <td width="18%"><div align="center">$date</div></td>
                </tr>
              </table></td>
          </tr>
        </table>

";
?>
Then, when you need it in the file, just:

Code: Select all

include_once 'header.php';

Posted: Thu Aug 22, 2002 10:56 am
by nielsene
I have to disagree. It is possible to return from include, see the manual. However, there is seldom a need to and it never seems to work exactly as expected. The other options, which I think might be closer to the original intent:

header.php

Code: Select all

<?php 

$date = date("F j, Y"); 

$display = " 
<table width="100%" border="0" cellspacing="0" cellpadding="0"> 
          <tr> 
            <td><table width="100%" border="0" cellspacing="0" cellpadding="3"> 
                <tr> 
                  <td width="38%"><div align="center"><img src="http://www.navigatormag.com/images/TopLogo.gif" width="269" height="60"></div></td> 
                  <td width="62%"><div align="center"> 
                      <?php 
    require(getenv('DOCUMENT_ROOT').'/Ads/phpadsnew.inc.php'); 
    if (!isset($phpAds_context)) $phpAds_context = array(); 
    $phpAds_id = view ('468x60', 0, '', '', '0', $phpAds_context); 
?> 
                    </div></td> 
                </tr> 
              </table></td> 
          </tr> 
          <tr> 
            <td height="15" background="http://www.navigatormag.com/GUI/RowBack.jpg"><table width="100%" border="0" cellspacing="0" cellpadding="0"> 
                <tr> 
                  <td width="82%"><table width="100%" border="0" cellspacing="0" cellpadding="0"> 
                      <tr> 
                        <td width="7%">&nbsp;</td> 
                        <td width="93%"><table border="0" cellspacing="0" cellpadding="0"> 
                            <tr> 
                              <td><a href="http://www.navigatormag.com/index.php">Home</a></td> 
                              <td width="30">&nbsp;</td> 
                              <td><a href="http://www.navigatormag.com/Archive.php">Article 
                                Archive</a></td> 
                              <td><img src="http://www.navigatormag.com/GUI/VertSpacer.gif" width="15" height="9"></td> 
                              <td><a href="http://www.navigatormag.com/Contests.php">Contests</a></td> 
                              <td><img src="http://www.navigatormag.com/GUI/VertSpacer.gif" width="15" height="9"></td> 
                              <td><a href="http://www.navigatormag.com/Coupons.php">Coupons</a></td> 
                              <td><img src="http://www.navigatormag.com/GUI/VertSpacer.gif" width="15" height="9"></td> 
                              <td><a href="http://www.navigatormag.com/TravelGames.php">Travel 
                                Games</a></td> 
                            </tr> 
                          </table></td> 
                      </tr> 
                    </table></td> 
                  <td width="18%"><div align="center">$date</div></td> 
                </tr> 
              </table></td> 
          </tr> 
        </table> 

"; 
// Note: I commented this out
// return $display; 
// ^^^^^^^^^^
?>
Then on your main pag::

Test.php

Code: Select all

<?php 
include 'http://www.navigatormag.com/Header.php'; 
/// later on
echo $display;
?>
This avoids the use of the return, but still lets you wait until later to display the string generated by the include. The variable $display has been set in the include and is availible to the script.

I would prefer to instead wrap the string into a function, but its basically the same.

Posted: Thu Aug 22, 2002 11:01 am
by llimllib
I agree with you, but I was shooting for simplicity, yours is cleaner.

Also, I only said you shouldn't return from an include, not that you couldn't.

Posted: Thu Aug 22, 2002 11:08 am
by nielsene
Oops, sorry llimllib you're right I misread that.

Posted: Thu Aug 22, 2002 11:14 am
by llimllib
i guess i'll forgive you, this time...</sarcasm>