...

View Full Version : are includes pre-loaded?



aprestong
01-31-2007, 08:01 PM
If I put, say, 15 of these in a page that I call individually with javascript, will they be pre-loaded?


<div id="page1" style="display:none;"><?php include("page1.php");?></div>


Or, will it only load after it is called by the javascript?

CFMaBiSmAd
01-31-2007, 08:22 PM
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 -

<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.

aprestong
01-31-2007, 08:48 PM
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?

aedrin
01-31-2007, 09:12 PM
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.

felgall
01-31-2007, 09:17 PM
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.

_Aerospace_Eng_
01-31-2007, 09:20 PM
Yeah you are right. I think something like this would be better

<?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 (http://pr2006.freehostia.com/testing/).
Notice the url. Almost everything stays the same. Only thing changing is the content.

aprestong
01-31-2007, 09:56 PM
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.

_Aerospace_Eng_
02-01-2007, 12:21 AM
Try this instead

<?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.

aprestong
02-01-2007, 02:10 PM
Wow - this works quite well...
I did make one addition



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


if(!empty($_GET['page']))
{
}


around the content section because I was getting a
"Notice: Undefined index: page" on this line:

if(array_key_exists($_GET['page'], $pages))

Thanks for your help! This is a really great method.... no javascript!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum