Jump to content

Concept question with include file


Go to solution Solved by mac_gyver,

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/279966-concept-question-with-include-file/
Share on other sites

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

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.

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. 

  • Solution

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.