Jump to content

Multiple instances of PHP include function on one page.


mrs_spence

Recommended Posts

Hello all!

 

I'm hoping you will be able to help me, I've built a templated site using dreamweaver, so it's a bit clunky.  I'm using very 3 x very simple include functions on each page to call the navigation file on the left, a footer file on the bottom, and a teaser navigation menu on the right of each page.

 

Problem is, now I want to include a print version, which I have a snippet of code for in PHP.  But as this is placed in the content section of the page, it's second on the list of functions to be called.  The first function, include <?php include_once "http://www.mydomain.net/development/nav.php";?> is always called first, which prevents my print friendly php document at http://www.mydomain.net/development/pfv.php from loading anything. Obviously, 'cos it's trying to load another file altogether!

 

In order to resolve this problem, could I specify some javascript or similar in the body onload function?

 

I'm a real newbie to coding, so any help is really appreciated.

 

Thanks

 

Mrs_Spence

 

 

 

Lets see the script for 1, and 2: genericnumber1 is right about including externally, which also slows down your page processing. 3: I think you are confusing the difference of including a function and including another script.  4: Seeing the code will let us beter determine your particular problem(s). In the long run it will only help you.

Ok,  here goes!!

 

Index.php file has three separate instances of included pages when it loads in the browser.

 

1 - <?php include_once "../nav.php";?> (this is the nav of the page on the left)

2 - <?php include_once "../header.htm";?> (this is the header at top of page)

3 - <a href="pfv.php"> (this is the 'print' function)

4 - <?php include_once "../footer.htm";?> (this is the footer included on the page)

 

<!-- CONTENT --> and <!-- /CONTENT --> tags specify body text to be printed.  In terms of load order on the page, the browser would load this fifth or sixth down the line.

 

OK.

 

Number 3 is reference to a 'print friendly' version file called pfv.php.  Whenever the user clicks 'print', of course the entire page of code is sent to pfv.php with links to 1 and 2 getting in the way and preventing any of the body text being sent to the pfv.php page.  I'm assuming it's causing some sort of loop to occur.

 

How can I make the pfv.php page load without the references to the first two files?  Can I specify some sort of load order, specifying that 3 is loaded using an onload function or similar?

 

Thanks

 

Mrs Spence

 

 

Assuming I understand what you mean, you wish to include nav.php and header.php only if the user is not viewing the print friendly version?

 

In that case, something like this would suffice...

 

page.php:

<?php
if(!isset($_GET['printer_version']) || $_GET['printer_version'] != true) {
    include_once "../nav.php";
    include_once "../header.htm";
}
?>

<a href="page.php?printer_version=true">Printer friendly version</a><br />
<a href="page.php">Non-Printer friendly version</a>

<?php include_once "../footer.htm"; ?>

 

I'd also advise switching from include_once to require_once if it's a very important file.

    Of course make note that using require will result in a fatal error if the file is not included, which can spit out a error message that will give away your server's file structure, to eliminate this you can use include and then check for a variable that is defined in the included file and if the variable does not exist then you know the file was not included and you can use your own error handling procedures.

Well, to be fair, when include fails an error message will still be displayed, it just won't be fatal. Checking flag variables in included files is a bit messy for me, I prefer to just use some of the error functions such as set_error_handler to define my own error handling procedures.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.