laural Posted July 8, 2013 Share Posted July 8, 2013 I am having speed issues with my site. I have determined that my navigation menu is the culprit. The navigation menu is comprised of a complicated query across a few tables, and then saved to sessions. The include file I created takes the sessions and loops through matching them to some user defined parameters. The problem is that I only need the file loaded once, by including it in the header, it re-does the query for every page causing the pages to load slowly. So, my question is, how can I include the navigation one time without having it reload every time the user clicks a link to a new page? I may be missing something really obvious Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/279966-concept-question-with-include-file/ Share on other sites More sharing options...
AbraCadaver Posted July 8, 2013 Share Posted July 8, 2013 Well, you say that you are saving the data to the session, so something along these lines in the header: session_start(); if(!isset($_SESSION['navigation_menu'])) { // do query and build session vars } Quote Link to comment https://forums.phpfreaks.com/topic/279966-concept-question-with-include-file/#findComment-1439922 Share on other sites More sharing options...
laural Posted July 8, 2013 Author Share Posted July 8, 2013 So, my menu is built like this, based on many different Sessions - can this whole include page be run once, and then used the entire time the user is logged in? From your response above, can I save the entire menu (below) to a session, even though it is made up of sessions? <a name="navigation"></a><!---------DIVISIONS_START----------> <div id="block_navigation"> <ul class="navigation"> <!------------ADD HOMEPAGE--------------> <?php echo '<li><a href="'.$_SESSION['pwfurl'].'/p_index.php?id_division=d00&id_module=m000">'.$_SESSION['home'].'</a></li>'; ?> <!------------LINK_START--------------> <?php $i = '00'; $num = '10'; for ($i = '00'; $i < $num; ++$i) { if (isset($_SESSION['id_d'.sprintf("%02d",$i).'_slot']) AND $_SESSION['id_d'.sprintf("%02d",$i).'_slot'] !== 'd'){ $id_division_menu = $_SESSION['id_d'.sprintf("%02d",$i).'_slot']; include $_SERVER['DOCUMENT_ROOT'].'/pwf/data/sp_services_content_navigation_division.inc'; if ($total > 0) { echo "<ul>"; include $_SERVER['DOCUMENT_ROOT'].'/pwf/data/sp_services_content_navigation_module.inc'; echo "</ul>"; } } echo "</li>";} ?> <!------------LINK_END--------------> <!------------CONTENT START--------------> <li class="right_item"> <a href="forms/contact.php?idDivision=00&id_module=m000&id_company="<?php echo $_SESSION['id_company']; ?>" onclick="window.open('forms/contact.php?id_company=<?php echo $_SESSION['id_company']; ?>', 'Feedback', 'toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=yes, scrollbars=yes, width=500, height=590'); return false"><?php echo $_SESSION['e140']; ?></a> </li> <!------------CONTENT_END----------------> </ul> </div> <!---------DIVISIONS_END----------> Quote Link to comment https://forums.phpfreaks.com/topic/279966-concept-question-with-include-file/#findComment-1439924 Share on other sites More sharing options...
AbraCadaver Posted July 8, 2013 Share Posted July 8, 2013 Yo stated that you are getting data from the database and using that data to populate the session vars? If so, then don't run the query if the session vars already exist. That was what I was trying to show in my example. Quote Link to comment https://forums.phpfreaks.com/topic/279966-concept-question-with-include-file/#findComment-1439926 Share on other sites More sharing options...
laural Posted July 8, 2013 Author Share Posted July 8, 2013 Thanks, but the query has to be run - this is a dynamic site and I have to check the session variables the user logs in with against what their service level allows them to see. But, I really only need to do this when they log in, and then I want the navigation to be static instead of regenerating the menu every time they click a link. I am trying to figure out how to run my queries on the homepage and from that have a static menu for the rest of their "travel" through my site. Quote Link to comment https://forums.phpfreaks.com/topic/279966-concept-question-with-include-file/#findComment-1439930 Share on other sites More sharing options...
Solution mac_gyver Posted July 8, 2013 Solution Share Posted July 8, 2013 the code you have posted is inefficient. you are evaluating the array string index multiple times and you are looping over 10 elements even if they don't exist. if the code building the $_SESSION variable is similar to what you have posted, i can see why there might be a noticeable lag due to the menus. you need to make the $_SESSION['id_d'] into an array. then you can simply loop over that array using a foreach loop. the LINK part of the code you posted would become something like - <!------------LINK_START--------------> <?php foreach($_SESSION['id_d'] as $id_division_menu){ if($id_division_menu !== 'd'){ include $_SERVER['DOCUMENT_ROOT'].'/pwf/data/sp_services_content_navigation_division.inc'; if ($total > 0) { echo "<ul>"; include $_SERVER['DOCUMENT_ROOT'].'/pwf/data/sp_services_content_navigation_module.inc'; echo "</ul>"; } } echo "</li>"; } ?> <!------------LINK_END--------------> including the same file multiple times is also inefficient. you should include each of them once (as a template into a php variable), then simply evaluate the template each time through the loop. Quote Link to comment https://forums.phpfreaks.com/topic/279966-concept-question-with-include-file/#findComment-1439934 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.