3

I have a php generated web page, which has several divs that are initially created using php includes.

<div id ="siteLeft" class ="site">
    <div id="divGauges"><?php include('gauges.php');?></div>
    <div id="divHistory"><?php include('history.php');?></div>
</div>
<div id="siteRight" class ="site">
    <div id="divLymGauges"><?php include('gaugesLym.php');?></div>
    <div id="divLymHistory"><?php include('historyLym.php');?></div>
</div>

These divs are then reloaded every 10 seconds, using an AJAX call, which is in a seperate scripts.js file:

function updateHistory(){
    $('#divHistory').load('history.php'+"?ms=" + new Date().getTime());
    $('#divLymHistory').load('historyLym.php'+"?ms=" + new Date().getTime());
}
setInterval( "updateHistory()", 10000 );

Within the history.php and historyLym.php, I have a function call, and the function is in another file called functions.php.

If I do:

include ('functions/functions.php');

within the index.php page, then the div's display nicely on initial load, but I get

Fatal error: Call to undefined function 

displaying on the webpage after the AJAX call refreshes the div with the history.php and historyLym.php (obviously, as the include for the function file isn't in the div, which is the only bit that gets called).

So I tried putting the functions.php include into the divs that are loaded. If I do that, then on initial page load I get:

Fatal error: Cannot redeclare getForce()

which also makes sense, as on initial load the two divs both contain the include for functions.php.

Putting the include in only one of the divs results in the page loading fine (as it should do), but on the AJAX refresh of the divs, I get this error:

Fatal error: Call to undefined function getForce()

which again makes sense as the two divs are being refreshed independently by AJAX, and so only the div with the include for functions.php will be able to use the function.

So my questions is:

Where on earth can I put the include for functions.php so that the divs can use the functions.php functions on initial page load AND on individual seperate AJAX refreshes??

4

3 回答 3

3

First include the function file in your index.php,
Then add this at the top of your include files (ie: gauges.php, history.php, etc..)

<?php
  if ( !function_exists( "getForce" ) ) {
    // include the function file
  }
?>

You can replace getForce with any other function names which is defined in your function file

于 2013-07-19T12:19:55.193 回答
1

try to use:

    include_once('functions/functions.php');

You can add this code to history.php and historyLym.php

于 2013-07-19T12:21:34.823 回答
0

Include functions.php file in history.php and historyLym.php and don't remove from index.php. Please note that there is no need to remove it from index.php file since it should load data on initial page load also.

于 2013-07-19T12:18:02.123 回答