PHP Code or CMSB PHP Code?

5 posts by 3 authors in: Forums > CMS Builder
Last Post: May 5, 2011   (RSS)

By Perchpole - May 3, 2011 - edited: May 3, 2011

Much of what I know about PHP has come from my association with CMSB and this forum. As a result my approach to PHP is very CMSB-centric! I don't know if the way I go about constructing code is typical or if I'm just copying the CMSB style - without even knowing it!

Take list() functions.

In CMSB it's normal to park all the list() functions at the top of the page. This seems fine if you're dealing with a simple site which only draws on a limited number of records.

What about a more complex site? What happens when the lists get into double figures? I'm guessing there must be a price to pay?

Should I be thinking about putting some kind of conditional code around these functions? Surely, if the records they call aren't required on every page then I could free-up precious resources by switching them off until they are?

Alternatively, should I consider putting the list() functions into the actual page code itself? That way the records wouldn't be called until a particular page element is loaded.

I'm guessing there are 3 factors at play here:
  • Overhead

  • Security

  • Best Practice

Perhaps people could share their experience and give me some advice.

Thanks,

:0)

Perchpole

Re: [Perchpole] PHP Code or CMSB PHP Code?

By Jason - May 3, 2011

Hi Perch,

One of the main reasons why we tend to put all of our getRecords calls up at the top of the page is for readability, so when we're going over a page, you can see fairly quickly all the different database calls being made. If they're scattered throughout the page, you can spend a lot of extra time searching through the code to find your queries.

A good way to increase readability and reduce your database overhead is to limit the number of queries you make on a page. If you find that you have a lot of queries up at the top of your page, go through and remove any queries that you don't need on that page.

The problem with having a large conditional block for queries on each page is that it adds a lot of extra code that can make it harder to debug.

Hope this helps. Please let us know if you have any other questions.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Perchpole] PHP Code or CMSB PHP Code?

By Dave - May 3, 2011

Hi Perch,

In general my advice is usually to not even worry about performance issues until they become an issue or you know from past experience they will. Most of the time performance won't be an issue or there will be other issues that come up first (like the max number of simultaneous connections you web host allows, etc).

As for loading 10+ sections at the top of the page, I'd be curious how your structuring your sections. Maybe there is a way to combine some of them?

If you have a viewer you can attach that has a lot of code at the top I'd be happy to take a look and see if I could offer any suggestions.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [Perchpole] PHP Code or CMSB PHP Code?

By Jason - May 5, 2011

Hi Perch,

For the situation you describe, it might be a good idea to put your getRecords() calls inside your include files. This would mean that you're only making the calls you need and will help to keep your code cleaner so it's easier to find what you need.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/