Jump to content

Recommended Posts

Hi all,

 

I have created an e-commerce website which includes a common.php file at the top of each page.

 

This file contains the link to my mysql database, a few common functions and a query that is used to retreive a list of product categories for my navigation menu.

 

I can't help but think there is a better way to do this so that the same query isn't run each time for each page. What would be the preferred solution here as i need to reduce loading times?

 

thanks for any help/tips with this.

 

Dan

Link to comment
https://forums.phpfreaks.com/topic/152899-reduce-number-of-queries-on-website/
Share on other sites

You could use define to tell when to execute the query and when not. Just before you include the common.php file on a page for which you want to execute the query, set:

 

define("DoQuery", true); // second argument is not used in this case, but required for define()
require_once('common.php'); // your include

 

In your common.php file you check whether "DoQuery" is defined and if so, you execute the query.

 

if(defined('DoQuery'))
{
// We were called from a page that requires us to execute the query, so do it!
} 

Hi,

 

Thanks for your reply. Not sure that i explained myself well enough.

 

The query needs to be run each time so that the menu can be populated, i was just hoping there was another way so that i didn't have to make a database call for every page?

 

Could i hold the values from the db in an array and store this in a Session variable? I coould then check if the variable exists on each page and populate from this. Is that possible?

 

Thanks again.

If the menu is stored in the DB then I can't see any other way unless you store the menu system inside an array defined in common.php

 

$arrMenu=array('index'=>'Home Page',
    'cart'=>'Shopping Cart',
    'account'=>'Your Account');
foreach ($arrMenu as $k => $v) {
  echo '<a href="'.$k.'.php">'.$v.'</a>';
}

 

That sort of thing.

as i need to reduce loading times?

 

Have you determined that the product category query is what is causing your loading time problem? Have you checked what the actual page generation time is? Perhaps you have a lot of large images that is causing the problem and the page generation time (which would include the time taken for the query) is acceptable but the time taken to send all the other content on the page is where the problem is.

 

If the query turns out to be the problem, you need to start by making sure that the table and the query are optimized. Posting the table definition and the query would allow someone to help determine if they can be optimized.

 

If the product categories are basically static (only change when the site administrator/owner adds or removes categories), you can create a cached version of the menu each time it is changed as an include file that is included on each page instead of performing the query on each page.

If you posted your existing query and code that outputs the menu, it would help.

 

If we assume that your existing code is echoing the content that makes up the menu, you would need to change it so that it builds the output in a variable. Then you just write that variable to the file that you will be including where you want the menu to appear.

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.