Forum Moderators: coopster

Message Too Old, No Replies

php include causes unwanted gap

         

pstar99

3:06 am on Jun 15, 2009 (gmt 0)

10+ Year Member



Hi, I found this script:

<?php
$pages = array('xyz.html','123.html');
$i = rand(0, count($pages) - 1);
include($pages[$i]);
?>

which I have saved as rotate.php
The script is supposed to enable rotating between the content of two pages on my home page.

When I add it via
<!--#include virtual="rotate.php" -->
on my home page it works fine, but it causes a gap of at least 25px between the header above and where the script generated content starts.

When I replace the include with something else it lines up just fine. I also have other php includes on the page that line up.

Does anybody know what is causing this gap and how it can be fixed / eliminated?
Please let me know

Thank you

lavazza

3:23 am on Jun 15, 2009 (gmt 0)

10+ Year Member



First guess:
Is there some CSS that might be 'imposing' a 25px margin (or padding) above the first element in xyz.html AND/OR rotate.php?

-----
Dunno if it matters... but...

I include my php files like so:
<?php include ("path/filename.php"); ?>
not
<!--#include virtual="path/filename.php" -->

pstar99

4:55 am on Jun 15, 2009 (gmt 0)

10+ Year Member



that was my first guess that something in the css is pushing it down, but if I replace the include with something else (paragraph, image, etc) it lines up perfect.

brotherhood of LAN

5:47 am on Jun 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ensure there is no whitespace or newlines before and after the PHP encapsulated code, that can also cause an unintended difference in layout.

stevenjm

9:09 am on Jun 15, 2009 (gmt 0)

10+ Year Member



If not it must be in your "rotate.php" code then.

pstar99

4:39 pm on Jun 15, 2009 (gmt 0)

10+ Year Member



yep, that's what I'm thinking.

When I include something static it lines up ok, but as soon as I set it back to include the rotator.php I get the 25px gap.

Another thing that I tried is to put a negative margin in to move it up, which works fine in IE8, FF and Safari, but when I look at it in Opera it's now 25px to high ;-(

Is there anything in the rotator script I posted above that could be causing this? Btw, in case it matters, the doc is xhtml strict and utf-8.

Thank you

pstar99

5:11 pm on Jun 15, 2009 (gmt 0)

10+ Year Member



little update on this:

I looked for "php include gap" on the web and ran into some sites that talk about BOM being an issue.

I also noticed that when I look at my page with the included rotator.php on lynx it shows an upside down question mark where the gap is.

I used Expression Web 2 and have the BOM features turned off.

How can this be fixed?

jatar_k

5:35 pm on Jun 15, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it could also be in the pages that you are including, not just rotator but these

$pages = array('xyz.html','123.html');

pstar99

5:51 pm on Jun 15, 2009 (gmt 0)

10+ Year Member



the code of the xyz.html and 123.html pages looks like this:

<p><img src="images/alero.jpg" height="120" width="180" alt="" /><br />Oldsmobile Alero<br />$4,000<br /><a href="#">more info</a></p>

I also tried to save these files as txt files with utf-8 encoding, but it doesn't make a difference as far as the gap and the upside down question mark once they are pulled in through the rotator.php

jatar_k

5:57 pm on Jun 15, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could try copying the content of rotator to a new file and saving it under a different name, then including that

I have run into scenarios where odd chars get into a file and doing this gets rid of them. I have run into this with mac to pc file switches.

stevenjm

2:53 pm on Jun 17, 2009 (gmt 0)

10+ Year Member



get rid of the <p> tag

belfasttim

5:17 pm on Jun 17, 2009 (gmt 0)

10+ Year Member



I had a very similar problem when generating a dynamic XML page using the PHP DOM tools-- I included the database connect function I used sitewide, which was a very simple script with no HTML or wasted spaces, then queried the data and moved on to the DOM.

Every time I'd run the page it would return errors for invalid XML. It took me a couple hours hunting through every line of code before I realized it was the include. It was adding space at the top of the document and therefore rendering the XML invalid for the "junk before" reason.

I searched online but never did find a good explanation of this, though plenty of other people had experienced it. I ended up having to hard code the DB connect script rather than include it, and the page works fine. Not an ideal solution though!

stevenjm

12:59 am on Jun 18, 2009 (gmt 0)

10+ Year Member



try getting rid of the <p> tag and if thats not it then don't bother showing us the php show us the resulting html in your broswer including the first line before the include.