PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Zoram
Forum Contributor
Posts: 166 Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:
Post
by Zoram » Thu Aug 22, 2002 10:08 am
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%"> </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"> </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...
any suggestions?
llimllib
Moderator
Posts: 466 Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD
Post
by llimllib » Thu Aug 22, 2002 10:47 am
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%"> </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"> </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:
nielsene
DevNet Resident
Posts: 1834 Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA
Post
by nielsene » Thu Aug 22, 2002 10:56 am
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%"> </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"> </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.
llimllib
Moderator
Posts: 466 Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD
Post
by llimllib » Thu Aug 22, 2002 11:01 am
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.
nielsene
DevNet Resident
Posts: 1834 Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA
Post
by nielsene » Thu Aug 22, 2002 11:08 am
Oops, sorry llimllib you're right I misread that.
llimllib
Moderator
Posts: 466 Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD
Post
by llimllib » Thu Aug 22, 2002 11:14 am
i guess i'll forgive you, this time...</sarcasm>