Load file once for whole site

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beary
    New Member
    • Nov 2006
    • 170

    Load file once for whole site

    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
  • realin
    Contributor
    • Feb 2007
    • 254

    #2
    Originally posted by beary
    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 !!

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      Originally posted by realin
      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

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

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

        Comment

        • beary
          New Member
          • Nov 2006
          • 170

          #5
          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

          Code:
          <ul id="nav">
          	<li class="top"><a href="<?php echo $up; ?>index2.php" class="top_link"><span>Home</span></a></li>
          	<li class="top"><a href="<?php echo $up; ?>calendar/mysqlcalindex.php" class="top_link"><span>Calendar</span></a></li>
          	<li class="top"><a href="<?php echo $up; ?>javascript: void(0)" id="classes" class="top_link"><span class="down">Classes</span></a>
          		<ul class="sub">
          			<li><a href="<?php echo $up; ?>classnew.php">New</a></li>
          			<li><a href="<?php echo $up; ?>classview.php">View or Edit</a></li>
          			<li><a href="javascript: void(0)" class="fly">Usernames to Parents</a>
          				<ul>
          					<?php
          					for($i=0; $i<mysql_numrows($result);$i++)
          						{
          						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>";
          						}
          					?>
          				</ul>
          			</li>
          		</ul>
          	</li>
          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
          Last edited by Atli; May 21 '08, 12:23 PM. Reason: Fixed the huge line so it wouldn't streach the frame

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

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

            Comment

            • beary
              New Member
              • Nov 2006
              • 170

              #7
              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
              Code:
              .preload1 {background: url(images/blank_over.gif);}
              .preload2 {background: url(images/blank_overa.gif);}
              
              #nav {padding:0; margin:0; list-style:none; height:36px; background:#eeeefe; position:relative; z-index:500; font-family:verdana, sans-serif;}
              #nav li.top {display:block; float:left;}
              #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);}
              #nav li a.top_link span {float:left; display:block; padding:0 24px 0 12px; height:36px; background:url(images/blank.gif) right top;}
              #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;}
              #nav li a.top_link:hover {color:#fff; background: url(images/blank_over.gif) no-repeat;}
              #nav li a.top_link:hover span {background:url(images/blank_over.gif) no-repeat right top;}
              #nav li a.top_link:hover span.down {background:url(images/blank_overa.gif) no-repeat right top;}
              
              #nav li:hover > a.top_link {color:#fff; background: url(images/blank_over.gif) no-repeat;}
              #nav li:hover > a.top_link span {background:url(images/blank_over.gif) no-repeat right top;}
              #nav li:hover > a.top_link span.down {background:url(images/blank_overa.gif) no-repeat right top;}
              
              /* Default list styling */
              
              #nav li:hover {position:relative; z-index:200;}
              
              /* keep the 'next' level invisible by placing it off screen. */
              #nav ul, 
              #nav li:hover ul ul,
              #nav li:hover ul li:hover ul ul,
              #nav li:hover ul li:hover ul li:hover ul ul,
              #nav li:hover ul li:hover ul li:hover ul li:hover ul ul
              {position:absolute; left:-9999px; top:-9999px; width:0; height:0; margin:0; padding:0; list-style:none;}
              
              #nav li:hover ul.sub
              {left:0; top:31px; background: #eeeefe; padding:3px; border:1px solid #3a93d2; white-space:nowrap; width:140px; height:auto; z-index:300;}
              #nav li:hover ul.sub li
              {display:block; height:20px; position:relative; float:left; width:140px; font-weight:normal;}
              #nav li:hover ul.sub li a
              {display:block; font-size:11px; height:20px; width:140px; line-height:20px; text-indent:5px; color:#000; text-decoration:none;}
              #nav li ul.sub li a.fly
              {background:#eeeefe url(images/arrow.gif) 130px 7px no-repeat;}
              #nav li:hover ul.sub li a:hover 
              {background:#3a93d2; color:#fff;}
              #nav li:hover ul.sub li a.fly:hover
              {background:#3a93d2 url(images/arrow_over.gif) 130px 7px no-repeat; color:#fff;}
              
              #nav li:hover ul li:hover > a.fly {background:#3a93d2 url(images/arrow_over.gif) 130px 7px no-repeat; color:#fff;} 
              
              #nav li:hover ul li:hover ul,
              #nav li:hover ul li:hover ul li:hover ul,
              #nav li:hover ul li:hover ul li:hover ul li:hover ul,
              #nav li:hover ul li:hover ul li:hover ul li:hover ul li:hover ul
              {left:140px; top:-4px; background: #eeeefe; padding:3px; border:1px solid #3a93d2; white-space:nowrap; width:140px; z-index:400; height:auto;}
              Now the JS
              Code:
              stuHover = function() {
              	var cssRule;
              	var newSelector;
              	for (var i = 0; i < document.styleSheets.length; i++)
              		for (var x = 0; x < document.styleSheets[i].rules.length ; x++)
              			{
              			cssRule = document.styleSheets[i].rules[x];
              			if (cssRule.selectorText.indexOf("LI:hover") >= 0)
              			{
              				 newSelector = cssRule.selectorText.replace(/LI:hover/gi, "LI.iehover");
              				document.styleSheets[i].addRule(newSelector , cssRule.style.cssText);
              			}
              		}
              	var getElm = document.getElementById("nav").getElementsByTagName("LI");
              	for (var i=0; i<getElm.length; i++) {
              		getElm[i].onmouseover=function() {
              			this.className+=" iehover";
              		}
              		getElm[i].onmouseout=function() {
              			this.className=this.className.replace(new RegExp(" iehover\\b"), "");
              		}
              	}
              }
              if (window.attachEvent) window.attachEvent("onload", stuHover);

              Comment

              • dlite922
                Recognized Expert Top Contributor
                • Dec 2007
                • 1586

                #8
                Originally posted by beary
                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

                Code:
                <ul id="nav">
                	<li class="top"><a href="<?php echo $up; ?>index2.php" class="top_link"><span>Home</span></a></li>
                	<li class="top"><a href="<?php echo $up; ?>calendar/mysqlcalindex.php" class="top_link"><span>Calendar</span></a></li>
                	<li class="top"><a href="<?php echo $up; ?>javascript: void(0)" id="classes" class="top_link"><span class="down">Classes</span></a>
                		<ul class="sub">
                			<li><a href="<?php echo $up; ?>classnew.php">New</a></li>
                			<li><a href="<?php echo $up; ?>classview.php">View or Edit</a></li>
                			<li><a href="javascript: void(0)" class="fly">Usernames to Parents</a>
                				<ul>
                					<?php
                					for($i=0; $i<mysql_numrows($result);$i++)
                						{
                						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>";
                						}
                					?>
                				</ul>
                			</li>
                		</ul>
                	</li>
                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_ass oc($result)) {
                echo $row["userid"];
                echo $row["fullname"];
                echo $row["userstatus "];
                }

                mysql_free_resu lt($result);


                [/PHP]

                Comment

                • beary
                  New Member
                  • Nov 2006
                  • 170

                  #9
                  dlite922, Thanks for this, I knew about the mysql_fetch_ass oc but I don't use it. I didn't realise it would make any difference. So is mysql_fetch_ass oc always quicker than mysql_result? And if so, why? I am going to change all my mysql_results to mysql_fetch_ass oc

                  Thanks

                  Comment

                  • arggg
                    New Member
                    • Mar 2008
                    • 91

                    #10
                    dlite is correct and yes you should change all your queries to mysql_fetch_arr ay or mysql_fetch_ass oc.

                    Comment

                    • arggg
                      New Member
                      • Mar 2008
                      • 91

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

                      Comment

                      • beary
                        New Member
                        • Nov 2006
                        • 170

                        #12
                        Originally posted by arggg
                        dlite is correct and yes you should change all your queries to mysql_fetch_arr ay or mysql_fetch_ass oc.
                        Thanks arggg,

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

                        Cheers

                        Comment

                        • TheServant
                          Recognized Expert Top Contributor
                          • Feb 2008
                          • 1168

                          #13
                          Originally posted by beary
                          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.field name argument.

                          Comment

                          • beary
                            New Member
                            • Nov 2006
                            • 170

                            #14
                            Originally posted by arggg
                            dlite is correct and yes you should change all your queries to mysql_fetch_arr ay or mysql_fetch_ass oc.
                            OK well I changed all my mysql_results to mysql_fetch_ass oc. 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

                            Code:
                            while($row=mysql_fetch_assoc($result))
                            	{
                            	echo "<li><a href='".$up."parentemail.php?class=".$row['id'].":".$row['classyear'].":".$row['classname']."'>".$row['classyear']." ".$row['classname']."</a></li>";
                            	}
                            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

                            Code:
                            while($row=mysql_fetch_assoc($result))
                            	{
                            	echo "<li><a href='".$up."awards.php?class=".$row['id'].":".$row['classyear'].":".$row['classname']."'>".$row['classyear']." ".$row['classname']."</a></li>";
                            	}
                            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_ass oc???

                            Comment

                            • beary
                              New Member
                              • Nov 2006
                              • 170

                              #15
                              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?

                              Comment

                              Working...