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: [Dave] PHP Code or CMSB PHP Code?

By Perchpole - May 5, 2011

Hi, CHaps -

Thanks for your help.

My initial query is as result of my preference to develop CMSB websites through only a single page. I've created a number of portals using this approach already - some fairly complex - and I think the results can be very effective, not to say lean!

The idea is that everything is rendered through a single index.php page which sits in the root of the site. The various page layouts and other elements are pulled in as includes, which are either assigned manually or triggered by a strict $selectedCategory framework.

It works a treat.

The only downside is the list() and getRecords() functions mentioned above. As I'm only using a single output page, it has to include the complete list of getRecords() calls for the entire site - even though only one or two are actually in use at any one time. That's why I wondered if I should look at introducing some kind of conditional on/off system so that the calls are only made when a particular record/category requires them.

:os

Perch