Forum Moderators: coopster

Message Too Old, No Replies

PHP Includes

         

john_m

9:21 am on Aug 12, 2005 (gmt 0)

10+ Year Member



Hi all,
2 questions: first, how can I get a php page to have 2 includes? One works fine, but then I copy and paste the script
(<?php include("page.htm");?>) and the second page doesn't appear. How can I get the page to display?

Second question is: Is there a php function to have an include page that just includes the HTML in between the <BODY> tags onto the page?

Thanks!

stuartc1

12:13 pm on Aug 12, 2005 (gmt 0)

10+ Year Member



you can include as many 'includes' in a page as you like:
<?
include 'page1.htm';
include 'page2.htm';
?>
second question - no, you need to read in the file and run a regular expression to grab the contents between the body tags

hexdj

1:34 pm on Aug 12, 2005 (gmt 0)

10+ Year Member



I am new in using includes in my php but I was told to use it like this:

<?php include("any.html")?>

notice there's no semi-colon in that line, I have used it that way and load about 3 HTML files into my index.php and it works fine.

Sarah Atkinson

2:32 pm on Aug 12, 2005 (gmt 0)

10+ Year Member



Second question is: Is there a php function to have an include page that just includes the HTML in between the <BODY> tags onto the page?

second question - no, you need to read in the file and run a regular expression to grab the contents between the body tags

What aboult reading in the body from another webpage on another server? although I think it would just be easier to use frames and frames was my first thought.

stuartc1

3:57 pm on Aug 12, 2005 (gmt 0)

10+ Year Member



<?php include("any.html")?>

Yes that works to, that is a semi shortened version of embedding PHP within HTML, when doing it in shorthand like this the ; is not always required, for example <?=$var?> would output the value of $var (notice the missing 'php' and the use of the = instead of echo, also no ; at the end).

Also note that although include is understood by some to be a function, may people do not class it as a function - i.e. you do not need to brackets.... for example:
<?
include 'page.php';
?>

is perfectly feasible.

What aboult reading in the body from another webpage on another server? although I think it would just be easier to use frames and frames was my first thought.

You can read files in from any server (providing you have access) - for example you can use:

if(!$fp = fopen("http://www.google.com", "r")) {
die("Cannot open google.com.");
}
$contents = (string) fread($fp, 2000000);

This would place all the HTML from google.com in the variable $contents. The regular expressions can be applied before echo'ing out the string.