473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Load file once for whole site

170 100+
Hi,

I have an include('header.php') line in each of my site pages. Is it possible to load this file once instead of every time a new page is reached? It's significantly slowing things down as it contains some css and on-the-fly generated php.

Thanks
May 19 '08 #1
16 3334
realin
254 100+
Hi,

I have an include('header.php') line in each of my site pages. Is it possible to load this file once instead of every time a new page is reached? It's significantly slowing things down as it contains some css and on-the-fly generated php.

Thanks
i believe objects can be loaded automatically into the application using _autoload function, also it doesnt saves the state instead, it intializes the object automatically without having to intialize them manually..

rest if you are asking to preload, css and js, i guess images can be preloaded, i never read anything like preloading css etc, as this is what header container and everytime a request is sent to server, header has to be sent automatically :)

I may be completely wrong, but its just a suggestion, even i want to see if this can be done..

** Thread Subscribed **

cheers !!
May 19 '08 #2
dlite922
1,584 Expert 1GB
i believe objects can be loaded automatically into the application using _autoload function, also it doesnt saves the state instead, it intializes the object automatically without having to intialize them manually..

rest if you are asking to preload, css and js, i guess images can be preloaded, i never read anything like preloading css etc, as this is what header container and everytime a request is sent to server, header has to be sent automatically :)

I may be completely wrong, but its just a suggestion, even i want to see if this can be done..

** Thread Subscribed **

cheers !!
Let me be the first to say your completely wrong.

__autoload() is used for classes. (Won't go there with you two)

The answer is plainly and simply no.

Upon refresh (loading each page, first time or again) the browser requests the page, PHP builds the page by doing all the calculations again.

Use a profiler to see if you can improve some of your functions. Show us the code and there maybe unnecessary halts/loops in there.

With CSS and Javascript however, if you include the same file the browser will load it faster. I'm no CSS expert but i've heard something to that affect.

Same with images. Given that cacheing is on.

"Peace Out"

-Dan
May 20 '08 #3
TheServant
1,168 Expert 1GB
I have use a similar set up with include(header.php) and that is where my CSS is called. Let me assure you, if you have done it like that your CSS and images will only be called once, unless you hit refresh, or the user has their cache turned off (unlikely).
Your speed problems with on-the-fly generated php should not be felt if you have a normal server (provider) and you code is not REALLY inefficient!
Do you have things like adverts, google analytics, etc on your page? I had that and after some testing realised they took up more time than the rest of my page (so I got rid of them all for now).
If you post your code we can maybe see what's slowing it down so much, but there is too many things to just guess.
May 20 '08 #4
beary
170 100+
Well thankyou Dan and TheServant for your replies. There are no google analytics or anything else like that. I notice that it loads faster with FF than with IE7, but I guessed that would happen. My main concern is to cut down the load time with IE since that seems to be the main browser used. The main header file looks like this

Expand|Select|Wrap|Line Numbers
  1. <ul id="nav">
  2.     <li class="top"><a href="<?php echo $up; ?>index2.php" class="top_link"><span>Home</span></a></li>
  3.     <li class="top"><a href="<?php echo $up; ?>calendar/mysqlcalindex.php" class="top_link"><span>Calendar</span></a></li>
  4.     <li class="top"><a href="<?php echo $up; ?>javascript: void(0)" id="classes" class="top_link"><span class="down">Classes</span></a>
  5.         <ul class="sub">
  6.             <li><a href="<?php echo $up; ?>classnew.php">New</a></li>
  7.             <li><a href="<?php echo $up; ?>classview.php">View or Edit</a></li>
  8.             <li><a href="javascript: void(0)" class="fly">Usernames to Parents</a>
  9.                 <ul>
  10.                     <?php
  11.                     for($i=0; $i<mysql_numrows($result);$i++)
  12.                         {
  13.                         echo "<li><a href='". $up. "parentemail.php?class=". mysql_result($result,$i,"id") .":". mysql_result($result,$i,"classyear") .":". mysql_result($result,$i,"classname"). "'>". mysql_result($result,$i,"classyear") ." ". mysql_result($result,$i,"classname") ."</a></li>";
  14.                         }
  15.                     ?>
  16.                 </ul>
  17.             </li>
  18.         </ul>
  19.     </li>
  20.  
and it keeps going like that for quite a while. And it has a css file and a js file linking from it at the start. I figure any problems in loading will be in the code above. As an idea, the main <li> above is for "Classes". That's one of about 10 such top-menus. Most of the others have more layers than that. Is this making any sense?

I'm looking forward to any tips or advice, but please be gentle...

Many thanks
May 20 '08 #5
TheServant
1,168 Expert 1GB
Hmmm, well nothing is standing out. Is there much more code, or just your connection to the server? How I debug my own code when something like this happens is break the code up into segments and try running it removing one segment at a time. That way you can see if there is one section which is chewing resources and then explore why.

How do you time your page load? I recommend using something like firebug (for firefox) which lets you explore html, css, javascript and page load time of every page. Just remember, your first load always takes longer because that it where it gets most of the images and CSS and then stores it in the cache.

I will have a closer look later, but if you can, try remove things that you think might be the problem and see if it makes a change.
May 20 '08 #6
beary
170 100+
Well I'll include the javascript and css in case anything there stands out. I should point out that I got the html, css and js from http://www.stunicholls.com/menu/pro_dropdown_1.html

I just looked at the original menu on his site and it loads quite a bit faster, which is what made me wonder whether it was my php.

First the CSS
Expand|Select|Wrap|Line Numbers
  1. .preload1 {background: url(images/blank_over.gif);}
  2. .preload2 {background: url(images/blank_overa.gif);}
  3.  
  4. #nav {padding:0; margin:0; list-style:none; height:36px; background:#eeeefe; position:relative; z-index:500; font-family:verdana, sans-serif;}
  5. #nav li.top {display:block; float:left;}
  6. #nav li a.top_link {display:block; float:left; height:36px; line-height:27px; color:#fff; text-decoration:none; font-size:11px; font-weight:bold; padding:0 0 0 12px; cursor:pointer; background: url(images/blank.gif);}
  7. #nav li a.top_link span {float:left; display:block; padding:0 24px 0 12px; height:36px; background:url(images/blank.gif) right top;}
  8. #nav li a.top_link span.down {float:left; display:block; padding:0 24px 0 12px; height:36px; background:url(images/blanka.gif) no-repeat right top;}
  9. #nav li a.top_link:hover {color:#fff; background: url(images/blank_over.gif) no-repeat;}
  10. #nav li a.top_link:hover span {background:url(images/blank_over.gif) no-repeat right top;}
  11. #nav li a.top_link:hover span.down {background:url(images/blank_overa.gif) no-repeat right top;}
  12.  
  13. #nav li:hover > a.top_link {color:#fff; background: url(images/blank_over.gif) no-repeat;}
  14. #nav li:hover > a.top_link span {background:url(images/blank_over.gif) no-repeat right top;}
  15. #nav li:hover > a.top_link span.down {background:url(images/blank_overa.gif) no-repeat right top;}
  16.  
  17. /* Default list styling */
  18.  
  19. #nav li:hover {position:relative; z-index:200;}
  20.  
  21. /* keep the 'next' level invisible by placing it off screen. */
  22. #nav ul, 
  23. #nav li:hover ul ul,
  24. #nav li:hover ul li:hover ul ul,
  25. #nav li:hover ul li:hover ul li:hover ul ul,
  26. #nav li:hover ul li:hover ul li:hover ul li:hover ul ul
  27. {position:absolute; left:-9999px; top:-9999px; width:0; height:0; margin:0; padding:0; list-style:none;}
  28.  
  29. #nav li:hover ul.sub
  30. {left:0; top:31px; background: #eeeefe; padding:3px; border:1px solid #3a93d2; white-space:nowrap; width:140px; height:auto; z-index:300;}
  31. #nav li:hover ul.sub li
  32. {display:block; height:20px; position:relative; float:left; width:140px; font-weight:normal;}
  33. #nav li:hover ul.sub li a
  34. {display:block; font-size:11px; height:20px; width:140px; line-height:20px; text-indent:5px; color:#000; text-decoration:none;}
  35. #nav li ul.sub li a.fly
  36. {background:#eeeefe url(images/arrow.gif) 130px 7px no-repeat;}
  37. #nav li:hover ul.sub li a:hover 
  38. {background:#3a93d2; color:#fff;}
  39. #nav li:hover ul.sub li a.fly:hover
  40. {background:#3a93d2 url(images/arrow_over.gif) 130px 7px no-repeat; color:#fff;}
  41.  
  42. #nav li:hover ul li:hover > a.fly {background:#3a93d2 url(images/arrow_over.gif) 130px 7px no-repeat; color:#fff;} 
  43.  
  44. #nav li:hover ul li:hover ul,
  45. #nav li:hover ul li:hover ul li:hover ul,
  46. #nav li:hover ul li:hover ul li:hover ul li:hover ul,
  47. #nav li:hover ul li:hover ul li:hover ul li:hover ul li:hover ul
  48. {left:140px; top:-4px; background: #eeeefe; padding:3px; border:1px solid #3a93d2; white-space:nowrap; width:140px; z-index:400; height:auto;}
  49.  
Now the JS
Expand|Select|Wrap|Line Numbers
  1. stuHover = function() {
  2.     var cssRule;
  3.     var newSelector;
  4.     for (var i = 0; i < document.styleSheets.length; i++)
  5.         for (var x = 0; x < document.styleSheets[i].rules.length ; x++)
  6.             {
  7.             cssRule = document.styleSheets[i].rules[x];
  8.             if (cssRule.selectorText.indexOf("LI:hover") >= 0)
  9.             {
  10.                  newSelector = cssRule.selectorText.replace(/LI:hover/gi, "LI.iehover");
  11.                 document.styleSheets[i].addRule(newSelector , cssRule.style.cssText);
  12.             }
  13.         }
  14.     var getElm = document.getElementById("nav").getElementsByTagName("LI");
  15.     for (var i=0; i<getElm.length; i++) {
  16.         getElm[i].onmouseover=function() {
  17.             this.className+=" iehover";
  18.         }
  19.         getElm[i].onmouseout=function() {
  20.             this.className=this.className.replace(new RegExp(" iehover\\b"), "");
  21.         }
  22.     }
  23. }
  24. if (window.attachEvent) window.attachEvent("onload", stuHover);
  25.  
May 20 '08 #7
dlite922
1,584 Expert 1GB
Well thankyou Dan and TheServant for your replies. There are no google analytics or anything else like that. I notice that it loads faster with FF than with IE7, but I guessed that would happen. My main concern is to cut down the load time with IE since that seems to be the main browser used. The main header file looks like this

Expand|Select|Wrap|Line Numbers
  1. <ul id="nav">
  2.     <li class="top"><a href="<?php echo $up; ?>index2.php" class="top_link"><span>Home</span></a></li>
  3.     <li class="top"><a href="<?php echo $up; ?>calendar/mysqlcalindex.php" class="top_link"><span>Calendar</span></a></li>
  4.     <li class="top"><a href="<?php echo $up; ?>javascript: void(0)" id="classes" class="top_link"><span class="down">Classes</span></a>
  5.         <ul class="sub">
  6.             <li><a href="<?php echo $up; ?>classnew.php">New</a></li>
  7.             <li><a href="<?php echo $up; ?>classview.php">View or Edit</a></li>
  8.             <li><a href="javascript: void(0)" class="fly">Usernames to Parents</a>
  9.                 <ul>
  10.                     <?php
  11.                     for($i=0; $i<mysql_numrows($result);$i++)
  12.                         {
  13.                         echo "<li><a href='".$up."parentemail.php?class=".mysql_result($result,$i,"id").":".mysql_result($result,$i,"classyear").":".mysql_result($result,$i,"classname")."'>".mysql_result($result,$i,"classyear")." ".mysql_result($result,$i,"classname")."</a></li>";
  14.                         }
  15.                     ?>
  16.                 </ul>
  17.             </li>
  18.         </ul>
  19.     </li>
  20.  
and it keeps going like that for quite a while. And it has a css file and a js file linking from it at the start. I figure any problems in loading will be in the code above. As an idea, the main <li> above is for "Classes". That's one of about 10 such top-menus. Most of the others have more layers than that. Is this making any sense?

I'm looking forward to any tips or advice, but please be gentle...

Many thanks
Your mysql result loop is horrible.

use mysql_fetch assoc()

Example:

[PHP]

if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}

mysql_free_result($result);


[/PHP]
May 20 '08 #8
beary
170 100+
dlite922, Thanks for this, I knew about the mysql_fetch_assoc but I don't use it. I didn't realise it would make any difference. So is mysql_fetch_assoc always quicker than mysql_result? And if so, why? I am going to change all my mysql_results to mysql_fetch_assoc

Thanks
May 21 '08 #9
arggg
91
dlite is correct and yes you should change all your queries to mysql_fetch_array or mysql_fetch_assoc.
May 21 '08 #10
arggg
91
as to your original question the only way to load it once is to use AJAX to load the other pages or use frames. One to use header.php and the other frame for the other pages. Frames are ugly but they will do what you are wanting.
May 21 '08 #11
beary
170 100+
dlite is correct and yes you should change all your queries to mysql_fetch_array or mysql_fetch_assoc.
Thanks arggg,

Interesting. So would there ever be a reason to use mysql_result? If not, why is it there?

Cheers
May 21 '08 #12
TheServant
1,168 Expert 1GB
Thanks arggg,

Interesting. So would there ever be a reason to use mysql_result? If not, why is it there?

Cheers
Yes.

Retrieves the contents of one cell from a MySQL result set.

When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mysql_result(). Also, note that specifying a numeric offset for the field argument is much quicker than specifying a fieldname or tablename.fieldname argument.
May 21 '08 #13
beary
170 100+
dlite is correct and yes you should change all your queries to mysql_fetch_array or mysql_fetch_assoc.
OK well I changed all my mysql_results to mysql_fetch_assoc. I have multiple instances on the same page, but am only making the initial query once. So now I am only getting the lists returned the first time but none after that. As follows

Expand|Select|Wrap|Line Numbers
  1. while($row=mysql_fetch_assoc($result))
  2.     {
  3.     echo "<li><a href='".$up."parentemail.php?class=".$row['id'].":".$row['classyear'].":".$row['classname']."'>".$row['classyear']." ".$row['classname']."</a></li>";
  4.     }
  5.  
This one works fine, but a few lines later I have basically the same thing but link is to a different page, so now it looks like

Expand|Select|Wrap|Line Numbers
  1. while($row=mysql_fetch_assoc($result))
  2.     {
  3.     echo "<li><a href='".$up."awards.php?class=".$row['id'].":".$row['classyear'].":".$row['classname']."'>".$row['classyear']." ".$row['classname']."</a></li>";
  4.     }
  5.  
and so on. But only the first instance will actually display results. Tell me I don't have to rewrite the query every time I use mysql_fetch_assoc???
May 21 '08 #14
beary
170 100+
Ignore me. I found mysql_data_seek($result,0)

On another note, does using AJAX mean that I could basically have one page load containing the header, and then just include different files depending on the link clicked?
May 21 '08 #15
beary
170 100+
I think I found another potential reason why the header takes a while to load, but I'm not sure how to fix it. Basically, within the 1st query, there are often other queries (and occasionally a 3rd query). So there's a 2nd level query for each record found in the 1st query. But the 2nd query uses a particular piece of info from the 1st query. So I think I need to leave the 2nd query within the first one. Is anyone able to advise me on how I can get around this. I reckon it's causing some slowdown. Here's some example code

Expand|Select|Wrap|Line Numbers
  1. while($row=mysql_fetch_assoc($result))
  2.     {
  3.     echo "<li>";
  4.     echo "<a href='javascript:void(0)' class='fly'>".$row['classyear']." ".$row['classname']." &rsaquo;</a>";
  5.         echo "<ul>";
  6.         $qtasks="select * from staffcba_tasks where user='$user' and pass='$pass' and classid='".$row['id']."' order by term desc,week desc";
  7.         $rtasks = mysql_query($qtasks) or die(mysql_error());
  8.         while($row2=mysql_fetch_assoc($rtasks))
  9.             {
  10.             echo "<li><a href='".$up."resultsview.php?tasknameetc=".$row2['id'].":".$row['classyear'].":".$row['classname']."'>".$row2['taskname']."</a></li>";
  11.             }
  12.         mysql_data_seek($rtasks, 0);
  13.         echo "</ul>";
  14.         echo "</li>";
  15.     }
  16. mysql_data_seek($result, 0);
  17.  
May 21 '08 #16
TheServant
1,168 Expert 1GB
Ignore me. I found mysql_data_seek($result,0)

On another note, does using AJAX mean that I could basically have one page load containing the header, and then just include different files depending on the link clicked?
Yes. But you need to know AJAX. And you will have to check with the others on PHP/AJAX compatability. But just try it if you have experience, because that is one very strong point that AJAX has.
May 21 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Donald Tyler | last post by:
Then the only way you can do it that I can think of is to write a PHP script to do basically what PHPMyAdmin is trying to do but without the LOCAL in there. However to do that you would need to...
12
by: Sharon | last post by:
I’m wrote a small DLL that used the FreeImage.DLL (that can be found at http://www.codeproject.com/bitmap/graphicsuite.asp). I also wrote a small console application in C++ (unmanaged) that uses...
2
by: Eric Falsken | last post by:
Eric Falsken <eric@db4o.com> wrote on 04 Dec 2005: > craigkenisston@hotmail.com wrote on 19 Nov 2005: > >> I'm working in the migration of an asp.net application in 1.1 to 2.0. >> I'm new to...
1
by: urs | last post by:
Two days ago, I built an ASP.NET 2.0 application and published it on a shared IIS 6 Web server. After publishing, and during the whole day, it worked fine. The server remained untouched since....
11
by: frizzle | last post by:
Hi there, I need a function to prevent a page from being loaded too often too fast. So say, one is only allowed to refresh a single page 5 times in 10 seconds, or 10 times in 5 seconds (or...
38
by: ted | last post by:
I have an old link that was widely distributed. I would now like to put a link on that old page that will go to a new page without displaying anything.
5
by: vunet | last post by:
I have some properties data to be loaded for the site use, such as: website url, admin email, mailing component to use, etc. for my ASP website. They will be stored in a database, or I also...
5
by: DR | last post by:
Why is its substantialy slower to load 50GB of gzipped file (20GB gzipped file) then loading 50GB unzipped data? im using System.IO.Compression.GZipStream and its not maxing out the cpu while...
7
Curtis Rutland
by: Curtis Rutland | last post by:
Building A Silverlight (2.0) Multi-File Uploader All source code is C#. VB.NET source is coming soon. Note: This project requires Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1 and...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.