Results 1 to 6 of 6

Thread: Problem with PHP include

  1. #1
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with PHP include

    Hello,

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

    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>
    Code:
    <a href="javascript:divpage('main.php', 'main');">Main</a>
    Now I have included
    Code:
    <?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?

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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');
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •