Log in

View Full Version : Problem with PHP include



chrizzieej1981
08-05-2010, 06:29 PM
Hello,

I have a website which uses include-files for database-access. Al my pages are loaded into a div with the folowing code:


<script type="text/javascript">

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function divpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}
</script>


<a href="javascript:divpage('main.php', 'main');">Main</a>

Now I have included
<?php include ("db_settings.php"); ?> within my index.php page. Now if I'm using a query within main.php it says it can't connect unless I also incude the db_settings.php on that file.

Is there another way to include the db_settings.php once and not on al pages?

djr33
08-05-2010, 07:05 PM
The simple answer to your question, and I hope this solves the problem, is to use the function called:
include_once()
(This checks if a file has already been included; if not, it includes it.)

That should solve everything if I've read your post correctly.

The Javascript you posted is completely unrelated to a PHP include and that might be part of the confusion so I suggest you try to separate that while debugging.

If you need more help, try to post more information.

chrizzieej1981
08-05-2010, 07:10 PM
Sorry but that isn't working, when I lot another page into the div... it says that it can't find a function from that included file.

djr33
08-05-2010, 07:51 PM
Again, it's very hard to tell from the information above when most of the post is about the (unrelated) Javascript.

Could this be a path issue? The relative location is different?

You could try this:
include_once(dirname(__FILE__).'/myinclude.php');

chrizzieej1981
08-07-2010, 07:25 AM
I have an index.php which is loading the following includes:<?php
include('include/settings.php');
include('include/connect_db.php');
include('include/functions.php');
?>

The menu is being loaded from my MySQL database, which loads perfectly because I have included the above includefiles.

When a user clicks on a item of the menu it is being loaded with the folowing script: <a href="javascript:divpage('main.php', 'main');">Main</a><br>
This code says it has to load main.php in the main div in my index.php page.

When the main.php loads into the main div it goed wrong. If the main.php file uses a function from functions.php it says that the function is not being found, it hasn't got the include anymore. But the index.php is still the same, only one div has new content.

Is it a little more understandeble now?

djr33
08-07-2010, 07:23 PM
Isn't that solved by just adding the same includes to main.php? Using include_once() will guarantee that it isn't loaded twice causing errors.
An ajax request is a new and completely separate request. It will never have PHP shared with the rest of the page. The HTML and Javascript will in fact mix, but that's unrelated to how the PHP processes (separately, for each request).