Dynamic navigation menu - run code only once?

Hi there,

I have a website where the menu (and several submenus) are dynamically generated through php. It takes a moment to load the page because of this, but once it is loaded once I would like to have future pages “remember” the html for the menu instead of doing the php again.

Any thoughts?

Thanks very much for your time.

Okay, first, by “Dynamic navigation”, we assume you mean you want to use dropdowns or menu buttons to move around the screen or data INSIDE the browser??? If that is correct, it can not be done in PHP.

PHP is server-side only. HTML is client-side only. So, now that I have said that, there are many many ways to move dynamically around your HTML page. You can use values loaded into fields on the page from PHP, you can use Javascript in many ways to handle data on the HTML page. You can even load PHP pages into hidden areas on the page and copy the data from those.

I would suggest just using standard SESSION variables. They are easy to use. You would remember a setting when a dropdown was changed and the new options loaded. This setting would be saved in a SESSION variable. Then, when the page is refreshed, it will remember the previous settings and display the dropdowns at the remembered versions. These settings will stay until you change them or the session is lost.

The other way would be to use a dynamic dropdown loader which is in Javascript and works well. It basically will load the next dropdown with data based on the first one. It can also hide dropdowns until the first one has been selected…

Either way you want to go, show some code posted inside the tags above and we can help you debug it…

Thanks for the kind reply.

To hopefully answer your question, this is a simple horizontal navigation menu at the top of a website, and one of the menu items is Products, and it expands to reveal about ten product families. And each of those families expands to reveal about 10 products.

FOr reasons I won’t get into, all the products themselves are held in one long php file and named product_01, product_02, etc… along with other properties (price_01, price_02). It isn’t the best I know, but it is what it is.

So whenever the page loads, there’s a small php script that runs through those products and properly throws them all into the correct submenus. It works perfectly, it just takes a moment to show the navigation menu at every new page load. I was hoping that somehow after the php had properly generated the menu structure the first time, subsequent page views could use that html for the menu rather than re-running the php script to re-generate the menu structure.

Hope this is a bit more clear.

Thanks again.

Thanks for the extra info… I understand now what you are needing…

Well, there are lots of ways to do this procedure. First, you could save the data in a text file and load it instead of parsing thru the data. You can load a text file fairly quickly into one variable with a couple lines of code. And, then echo it wherever your menu is located. Another way would be to pass them in SESSION variables, although that would depend on their size. If you need a hand doing this by use of a file, it would not be too hard. Basically, you would build the menu the first time and store it into a file, any format would work, text easiest, binary next… Then, inside each page, load the entire file into a variable and use it as needed. If the file is small enough, you can load it into a variable and pass that variable in session variable…
Hope that info helps, if not, let us know…

Ok, thank you for the reply. I have looked into different ways to do this. If I pass it as a variable, this website shows how we output the entire page to a php variable:

I could probably find a way to therefore output just my menu html to a variable. Then when I load a new page, I assume I will do an if else where I check if that variable exists and use it if it does, and if it doesn’t I will run the php script?

That being said, the text file seems like a better option since the menu is quite large. Do you happen to have a brief code snippet to share which could output a portion of my HTML to such a file, and then recall that file for subsequent page loads with an if else?

Thanks again for your generous time.

Well, first, is there a reason you are not just using a database for this issue?
You could create the header and store it in your database and load it as needed…

Now, here is a simple code to read a text file. Easy to use and understand.
[php]
$myFile = “HeaderHTML.txt”;
$fh = fopen($myFile, ‘r’);
$HeaderData = fread($fh, filesize($myFile));
fclose($fh);
[/php]
*** NOTE: this will load an entire text file into a variable that can be passed by a session variable.
*** No error checking was added, as long as the file is always there, none is needed…
*** If the data is huge, then, do not pass with session variables. It loads quickly anyways…

You would just use echo $HeaderData; to put it into your HTML page…

So, to create the file, it must be create whenever you start the system up, I assume.
This would be done by a PHP routine. It would have to create all of the HTML along with the content.
So, all the , , and all tags that are currently in the header. This includes all of the data inside these tags. So, you would use the page you posted with the starting and end an HTML OBJ and the results would be inside a variable. Then, use a similar code as I just posted to write the file out to a text file. Then, on every page that needs this header, just use the code above to read it in and echo it to the page… Simple enough… A large text file seems to load fast enough to get onto a page. Try it and see what happens, we are still here to fine tune it with you… Good luck…

Ok, thanks for you kind help. I am still unsure on a few things but will do some tests and try to learn with your helpful pointers before bugging you again!

Thanks.

Well, just use the OBJ on that link you posted to capture the header from wherever you created it.
Then, write it out using code like I posted (only in reverse, writing not reading).
At that point you have the header in a file and use my posted code to read it and echo it.

Good luck, post away when you get it tested…

Well ErnieAlex, you are a real superstar. That last post was exactly the recap I needed and I was able to code it in about 5 minutes.

The good news, I have learned something. The bad news, the menu still takes a few seconds to load (on a slow internet connection, a half second on high speed).

The text file outputted is 250kb, so I think it is quite simply the amount of stuff that is in the submenus that is causing the lag.

But, in case anyone wants to review this post in the future, here is the code I used:

1) Capture the html that my php script creates, then write it to a file:

[php]
ob_start();

/* MY PHP SCRIPT */

$HtmlCode= ob_get_contents();
ob_end_flush();

$LogFileLocation = “/z_header/HeaderHTML.txt”;
$fh = fopen($_SERVER[‘DOCUMENT_ROOT’].$LogFileLocation,‘at’);
$myFile = “HeaderHTML.txt”;
fwrite($fh, $HtmlCode);
fclose($fh);

[/php]

2) Load this txt file rather than my script:

[php]
$LogFileLocation = “/z_header/HeaderHTML.txt”;
$fh = fopen($_SERVER[‘DOCUMENT_ROOT’].$LogFileLocation,‘r’);
$HeaderData = fread($fh, filesize($_SERVER[‘DOCUMENT_ROOT’].$LogFileLocation));
fclose($fh);

echo $HeaderData;
[/php]

Note that there would still need to be some coding done to ensure that the php script is run on the first page view (to refresh the text file with the latest menu, since it changes every so often) and that on subsequent page views in that same session, it loads the text file instead.

I am not an expert in this, but perhaps for closure of this thread you could just confirm what would be the best way to do this.

Thanks again for your kind and generous help, it was much appreciated. I am truly happy to have learned this.

Well, congrats on fixing it up to work. But, I dont think that 250k is much. You might just use the session variable to send it on to pages. It would have to be created once on the login page and then just passed to pages. 250 is a large file, but, not much these days for memory use… I think it would pass without any problems… Glad I could help. I will mark this one solved…

You may be right since all other resources load fine, and are equally large. Is there a quick change i would make to my above code to test out the session variable approach?

Sorry, I wasn’t home for awhile…

Well, using session variables is actually easier than a text file. First, once you create the menu in it’s text format, you just do not write it to the text file. You already have it in a variable. Just make that variable a session variable instead of a normal one. They are actually stored the same way, just session variables are passed by the server to any other pages.
First, on EVERY page that uses any session variable, you add this command to the beginning of every page in the PHP script: session_start(); This tell the server that the page is going to use the session variable array.
In the page that creates the menu, you use $_SESSION[‘variablename’] instead of $variablename !
This just creates the variablename data inside the SESSION array instead of normal variable. So, where you create the menu, like: $HtmlCode= ob_get_contents(); , you would just change that one line to save it into the session version like: $_SESSION[‘HtmlCode’]= ob_get_contents(); And, then remove
the text file writing code. Wherever you want to use the menu do a "echo $_SESSION[’‘HtmlCode’]; !!!
That easy… Just remember, do not forget the session_start() as the first line of your PHP code on all pages that use the menu…
I think that will do it for you… PS: to thank someone on this site, Up their karma… LOL Never hurts!
As far as superstar goes, Thanks! But, lots of others have helped me. I just tend to explain in terms that the questioner can understand! Lots of helpful people here! Glad you got it figured out!

First of all, I would love to up your Karma but despite my best efforts, can’t find where! Let me know :slight_smile:

And your explanation was spot on, more than I could have hoped for. I setup a session variable in a snap and it works flawlessly. But alas, that menu item still stalls the entire page load for about a second or two.

The session variable (and before that the text file) holds a rather large submenu structure, it is 130 products split up into about 10 submenus. So I have concluded that the delay is simply the time it takes to load that many ul and li tags, as opposed to the time to run the php script (my initial assumption and the reason for this thread).

I have confirmed that it is simply the amount of menu items, not the php, by copying the entire html output from the text file and pasting it right into the page as straight html. Indeed, the page still delays at that menu item

But I have learned a whole lot in this thread which I will surely reuse (and hopefully others will find it helpful). Thank you for the help and the kindness. You are indeed a superstar. Let me know how to up the karma and I will happily (sorry, normally I am not one of those people who misses the obvious, but I truly can’t find the karma upper anywhere!)

Well, now embarrassed! (But, Karma UP is the little (+) under the karma note under the ID…

Anyway, I was not looking for that, I am just here to help… So, after your last note, I found that you have multiple layers of menus. You did not mention that you had 130 products in your menus…

130 menu items will slow up any page unless on a dedicated server with lots of power… (Nobody has those!)

So, how to speed that up… There is a way. You can divide your 130 products into sub categories and use “Dynamic” menus. These would need some knowledge of Javascript as the menu manipulation would be with Javascript functions that control DIV’s. The problem with this solution is that it loads items dynamically and it would be best to layer your products so they are divided somewhat evenly. 130 products may be able to be broken down into, let’s say 4 drop-downs… That would be, maybe 4 major products with 35 in each, then another drop down with 10 in each, etc… Depends a lot on what types are in the total 130… Just an idea. I can send you a link to do this if you are unhappy with the “now” current way of Session variables… If you are interested, ask away…

But, overall, glad it is working and glad you learned about “SESSION” variables! They are useful in many ways you do not even have imagined yet… Every bit of new code is helpful… Hope you do some great programming with this knowledge… CYA in the bitstream…

Thanks for the follow-up. That is a bit beyond my energy level for now, so will leave it as-is! As for the Karma, I assumed that was where I could do it, but it won’t let me click the + or anything else under there. Perhaps new members don’t have the ability at first?

You are probably correct about the new member… Not an issue, I like to help!

One thing I was curious about, though… You mentioned lots of ul’s and li’s in your menu.
What kind of menu is it? Or can you private-message me a copy of it. You might be able
to streamline it so it is smaller. Just curious…

Ok, will check back in a week to try giving the karma, you deserve it!

It is actually something that is for a client and hasn’t launched yet so I can’t share, but thanks for the continued generosity!

Sponsor our Newsletter | Privacy Policy | Terms of Service