A simple include problem

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

Post Reply
Aqua
Forum Newbie
Posts: 6
Joined: Sun May 25, 2003 6:04 am

A simple include problem

Post by Aqua »

Hi, I'm a relative newbie in PHP. I've been using PHP for some time now just for the include function. This time however, I can't seem to get it to work, even after exhausting all (I think) trials to find what's wrong. Here's the problem.

In index.php, I have the following:

Code: Select all

<BODY <?php include "PHP/PHP_Body_Tag.php" ; ?> >
PHP/PHP_Body_Tag.php:

Code: Select all

<?
echo "BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#B9B9B9"
ALINK="#B9B9B9" VLINK="#B9B9B9"" ;
?>
The path of the include statement is correct as both /PHP and index.php are located in root.

I know this is very basic but I really need your help please (I'm stuck!). Can you please tell me what I'm doing wrong here? Thank you!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if you could tell us what should happen and what does happen?


If you open your browser's source view do you see the code of PHP_Body_Tag.php instead of the result?
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

Your error would be a parse error because you haven't escaped your double quotes inside your echo.

Try this:

Code: Select all

<?php

echo "BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#B9B9B9" 
ALINK="#B9B9B9" VLINK="#B9B9B9" " ; 


?>
Aqua
Forum Newbie
Posts: 6
Joined: Sun May 25, 2003 6:04 am

Post by Aqua »

Sorry, forgot to mention that loading the page produces a half empty page where there is nothing on top. When I view the source code, I cannot see the php output.

evilcoder: I tried escaping the double quotes but the problem is still the same.
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

ok give this a try:

Put this at the top of the page your including into:

Code: Select all

<?php
$bodyvars = include( "filewithechoinit.php" );
?>
<body <?=$bodyvars ?> >
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

first of all, you let the script write the contents into the tag. At least it must be

Code: Select all

<BODY><?php include "PHP/PHP_Body_Tag.php" ; ?></BODY>
if you change it to

Code: Select all

<BODY><?php error_reporting(E_ALL); ini_set('display_errors', TRUE); include("include.php"); ?></BODY>
does it display any errors?

edit: oops, you want to write properties. forget about the <body></body> but do enable error_reporting/displaying ;)
Aqua
Forum Newbie
Posts: 6
Joined: Sun May 25, 2003 6:04 am

Post by Aqua »

evilcoder: I tried that and now I get the contents of the include file echoed on the page instead of it being used for the <body> properties.

volka: I tried your suggestion, I get the contents of the php include file on the page with no errors.

I know the problem is so tiny and that is why I can't seem to find it, atleast I hope.
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post by werlop »

Mabye im missing something here, but if all he wants to include is what he has here:

Code: Select all

<? 
echo "BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#B9B9B9" 
ALINK="#B9B9B9" VLINK="#B9B9B9"
?>
Then why does he need php at all for the include file?

Couldn't the file to be included just be:

Code: Select all

"BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#B9B9B9" 
ALINK="#B9B9B9" VLINK="#B9B9B9"" ;
Then just do <body <?php include( "filetoinclude.php" ); ?>></body>

Sorry if I'm missing something

David
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

not at all, werlop. It can also be handled via css without any php-support.
But it should work either way ;)
I've tried the code

Code: Select all

<?php // PHP_Body_Tag.php
echo "BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#B9B9B9" ALINK="#B9B9B9" VLINK="#B9B9B9" " ;
?>

Code: Select all

<?php // test.php
<BODY <?php include "PHP/PHP_Body_Tag.php" ; ?> >
	test
</BODY>
and the output was (as expected)

Code: Select all

&lt;body BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#B9B9B9" ALINK="#B9B9B9" VLINK="#B9B9B9"  &gt;
	test
&lt;/body&gt;
can you describe the problem again (expectiations, visible results, generated output), Aqua?
Aqua
Forum Newbie
Posts: 6
Joined: Sun May 25, 2003 6:04 am

Post by Aqua »

volka: It's like you said, trying to substitute PHP for CSS. I would like the body tag to acquire its attributes from the included PHP/PHP_Body_Tag.php file. When I load the page (index.php which includes the include function), I don't see the top part of the page. After I started following your suggestions along with evilcoder's, I started getting the output displayed on the page instead of it being used as an attribute for the <body> tag. To explain further, I want to see the contents of the include file in the page's html source code and not on the page itself (http://www.dzp.com/Web). Do I make sense hehe?
Aqua
Forum Newbie
Posts: 6
Joined: Sun May 25, 2003 6:04 am

Post by Aqua »

Just to add (sorry I just remembered), all my php include coomands on the same page are not working as their output is not visible on the page's source code. Thanks again!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ok, I think it's time for a little rollback before you destroy your script ceompletely because of our help ;)
After I started following your suggestions along with evilcoder's, I started getting the output displayed on the page instead of it being used as an attribute for the <body> tag.
that's right.mark my
edit: oops, you want to write properties. forget about the <body></body> but do enable error_reporting/displaying
so my suggestion was

Code: Select all

&lt;BODY &lt;?php error_reporting(E_ALL); ini_set('display_errors', TRUE); include("include.php"); ?&gt; &gt;
to enable the error output. since it would happen withing the body-tag the rendered result (note the difference between script-output and rendered result within the client/browser) might be broken and you had to open the browser's source view (where you can see the script's output).

now for evilcoder's suggestion
evilcoder wrote:<?php
$bodyvars = include( "filewithechoinit.php" );
?>
<body <?=$bodyvars ?> >
it includes and executes filewithechoinit.php, so anything that is echoed in filewithechoinit.php will be added to the output stream. $bodyvars would fetch a global scope return value of filewithechoinit.php, but there isn't any. So the output of filewithechoinit.php (PHP/PHP_Body_Tag.php in your case) will take place before the <body>-tag and <?=$bodyvars ?> might be executed (if short-tags are enabled) but print nothing (in the worst case it would print 1, which you might check in the browser's source view)

css seperates the layout from the contents, e.g. try

Code: Select all

&lt;html&gt;
	&lt;head&gt;
		&lt;style type="text/css"&gt;
			body	&#123; background-color: white; color: black; &#125;
			p &#123; background-color: silver; &#125;
			p.redbg &#123; background-color: red; &#125;
			a &#123; color: #B9B9B9 &#125;
			a:hover &#123; color: green &#125;
		&lt;/style&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;a name="anchor1" &gt;&lt;/a&gt;
		&lt;p&gt;bla bla bla&lt;/p&gt;
		&lt;a name="anchor2" &gt;&lt;/a&gt;
		&lt;p class="redbg"&gt;bla bla bla&lt;/p&gt;
		
		&lt;a href="#anchor1"&gt;goto anchor #1&lt;/a&gt;
		&lt;a href="#anchor2"&gt;goto anchor #2&lt;/a&gt;
		&lt;a href="javascript:void()"&gt;do nothing&lt;/a&gt;
	&lt;/body&gt;
&lt;/html&gt;
but the stylesettings are still within the page. You can write them to a different file and let the browser include it.
stylesA.css:

Code: Select all

body	&#123; background-color: white; color: black; &#125;
p &#123; background-color: silver; &#125;
p.redbg &#123; background-color: red; &#125;
a &#123; color: #B9B9B9 &#125;
a:hover &#123; color: green &#125;
index.html:

Code: Select all

&lt;html&gt;
	&lt;head&gt;
		&lt;link rel="stylesheet" type="text/css" href="stylesA.css"&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;a name="anchor1" &gt;&lt;/a&gt;
		&lt;p&gt;bla bla bla&lt;/p&gt;
		&lt;a name="anchor2" &gt;&lt;/a&gt;
		&lt;p class="redbg"&gt;bla bla bla&lt;/p&gt;
		
		&lt;a href="#anchor1"&gt;goto anchor #1&lt;/a&gt;
		&lt;a href="#anchor2"&gt;goto anchor #2&lt;/a&gt;
		&lt;a href="javascript:void()"&gt;do nothing&lt;/a&gt;
	&lt;/body&gt;
&lt;/html&gt;
if you define different stlyes in different files all your php script has to do is to write the url to the href-property of the link-element

Code: Select all

<?php $urlCSS = 'stylesA.cc'; // or somthing more complex ?><html>
	<head>
		<link rel="stylesheet" type="text/css" href="<?php echo $urlCSS; ?>">
	</head>
	<body>
		<a name="anchor1" ></a>
		<p>bla bla bla</p>
		<a name="anchor2" ></a>
		<p class="redbg">bla bla bla</p>
		
		<a href="#anchor1">goto anchor #1</a>
		<a href="#anchor2">goto anchor #2</a>
		<a href="javascript:void()">do nothing</a>
	</body>
</html>
Aqua
Forum Newbie
Posts: 6
Joined: Sun May 25, 2003 6:04 am

Post by Aqua »

Sorry for the delayed reply, I was away at work. Anyway, I decided to start over and replaced the above problem-causing script with CSS (although I still prefer it to be PHP). Everything is working fine now. Thank you everyone for your kind help.
Post Reply