Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jul 2006
    Posts
    110
    Thanks
    1
    Thanked 0 Times in 0 Posts

    are includes pre-loaded?

    If I put, say, 15 of these in a page that I call individually with javascript, will they be pre-loaded?
    Code:
    <div id="page1" style="display:none;"><?php include("page1.php");?></div>
    Or, will it only load after it is called by the javascript?

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,011
    Thanks
    2
    Thanked 312 Times in 304 Posts
    PHP is parsed on the server when the web page is requested by the browser. The resultant content is sent to the browser. For the example you show, the browser would receive -
    Code:
    <div id="page1" style="display:none;">Whatever HTML content page1.php generated</div>
    Just because you are using javascript to change the display status of this, the content is already present within the div tags and has already been received by the browser, the same as if the content was directly coded within the HTML of the web page instead of the result of a PHP include statement.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Regular Coder
    Join Date
    Jul 2006
    Posts
    110
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks, CFMaBiSmAd -
    That makes perfect sense and I feel like an idiot now...
    This makes me wonder -
    I was going to include 18 separate pages and hide them - then use the a javascript show / hide layer script in a menu to show the content without having to do a whole page reload....

    Even though there will be just a little text and maybe a few graphics in each layer, it probably doesnt make much sense to have all those includes....
    Am I right to assume this?

    In a sense, I will have to load all 18 of these pages right away before anything will work, right?

    Would you say I'd be better off the old fashioned way and having a separate page for each link?

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    From the sounds of it, it would indeed be a better idea to resort to the single page.

    There is almost never a reason to load more than 1 page at a time.

    The only time it would make sense if for example you have a FAQ page, and you hide the answers by default, and then show them as they click on a link.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    You could use AJAX to progressively retrieve the content of hidden sections so that they wouldn't slow the initial display of the page but would hopefully be there by the time that the appropriate section was displayed. You would need an alternate method of displaying each one separately for when Javascript isn't available though.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Yeah you are right. I think something like this would be better
    PHP Code:
    <?php
                
    # default page
                
    $default 'includes/home.php';

                
    # set document root path
                
    $base $_SERVER['/'];

                
    # list of all site pages + the id they will be called by
                
    $pages = array('about' => 'includes/about.php','contact' => 'includes/contact.php','faq' => 'includes/faq.php','help' => 'includes/help.php');
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>index</title>
    <style type="text/css">
    html, body {
    margin:0;
    padding:0;
    }
    #container {
    padding:10px;
    }
    #nav {
    border:1px solid #000;
    padding:10px;
    width:180px;
    float:left;
    margin:0;
    list-style:none;
    }
    #content {
    margin-left:210px;
    border:1px solid #000;
    padding:5px;
    }
    </style>
    </head>
    <body>
    <div id="container">
        <ul id="nav">
            <li><a href="index.php">home</a></li>
            <li><a href="index.php?page=about">about</a></li>
            <li><a href="index.php?page=contact">contact</a></li>
            <li><a href="index.php?page=faq">faq</a></li>
            <li><a href="index.php?page=help">help</a></li>
        </ul>
        <div id="content">
            <?php
                
    if(array_key_exists($_GET['page'], $pages))
                {
                    foreach(
    $pages as $pageid => $pagename)
                    {
                        if(
    $_GET['page'] == $pageid && file_exists($base.$pagename))
                        {
                            
    /* if somebody's making a request for ?page=xxx and
                            the page exists in the $pages array, we display it
                            checking first it also exists as a page on the server */
                            
    include $base.$pagename;
                          }
                       } 
    // end foreach
                
    }
                else
                {
                          
    /* if the page isn't listed in $pages, or there's no ?page=xxx request
                          we show the default page, again we'll also just make sure it exists as a file
                          on the server */
                          
    if(file_exists($base.$default)) include $base.$default;
                }
                
    ?>
        </div>
    </div>
    </body>
    </html>
    The result is this.
    Notice the url. Almost everything stays the same. Only thing changing is the content.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #7
    Regular Coder
    Join Date
    Jul 2006
    Posts
    110
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks, _Aerospace_Eng_ !

    This is great...
    I will have to spend some time trying to figure this out, as I'm getting a
    Notice: Undefined index: / in D:\Inetpub\WWWroot\blah\betatest\tests\test.php on line 6

    It's this line: $base = $_SERVER['/'];
    that is causing the problem.

  • #8
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Try this instead
    PHP Code:
    <?php
                
    # default page
                
    $default 'includes/home.php';

                
    # list of all site pages + the id they will be called by
                
    $pages = array('about' => 'includes/about.php','contact' => 'includes/contact.php','faq' => 'includes/faq.php','help' => 'includes/help.php');
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>index</title>
    <style type="text/css">
    html, body {
    margin:0;
    padding:0;
    }
    #container {
    padding:10px;
    }
    #nav {
    border:1px solid #000;
    padding:10px;
    width:180px;
    float:left;
    margin:0;
    list-style:none;
    }
    #content {
    margin-left:210px;
    border:1px solid #000;
    padding:5px;
    }
    </style>
    </head>
    <body>
    <div id="container">
        <ul id="nav">
            <li><a href="index.php">home</a></li>
            <li><a href="index.php?page=about">about</a></li>
            <li><a href="index.php?page=contact">contact</a></li>
            <li><a href="index.php?page=faq">faq</a></li>
            <li><a href="index.php?page=help">help</a></li>
        </ul>
        <div id="content">
            <?php
                
    if(array_key_exists($_GET['page'], $pages))
                {
                    foreach(
    $pages as $pageid => $pagename)
                    {
                        if(
    $_GET['page'] == $pageid && file_exists($pagename))
                        {
                            
    /* if somebody's making a request for ?page=xxx and
                            the page exists in the $pages array, we display it
                            checking first it also exists as a page on the server */
                            
    include $pagename;
                          }
                       } 
    // end foreach
                
    }
                else
                {
                          
    /* if the page isn't listed in $pages, or there's no ?page=xxx request
                          we show the default page, again we'll also just make sure it exists as a file
                          on the server */
                          
    if(file_exists($default)) include $default;
                }
                
    ?>
        </div>
    </div>
    </body>
    </html>
    I got rid of the $base thing all together.
    Last edited by _Aerospace_Eng_; 02-01-2007 at 12:24 AM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #9
    Regular Coder
    Join Date
    Jul 2006
    Posts
    110
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Wow - this works quite well...
    I did make one addition

    Code:
    if(!empty($_GET['page']))
    	{
                if(array_key_exists($_GET['page'], $pages))
                {
                    foreach($pages as $pageid => $pagename)
                    {
                        if($_GET['page'] == $pageid && file_exists($pagename))
                        {
                            /* if somebody's making a request for ?page=xxx and
                            the page exists in the $pages array, we display it
                            checking first it also exists as a page on the server */
                            include $pagename;
                          }
                       } // end foreach
                }
             }
    Notice that I added
    Code:
    if(!empty($_GET['page']))
    {
    }
    around the content section because I was getting a
    "Notice: Undefined index: page" on this line:
    Code:
     if(array_key_exists($_GET['page'], $pages))
    Thanks for your help! This is a really great method.... no javascript!


  •  

    Posting Permissions

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