Code Optimization Help

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

Hello,

I wanted to reach out in the forum to see if someone can help me optimize my php script.

My website is www.illumemag.com. As you can see from the home page, there are several sections on the site, and I am using below codes.

When the site was first setup, it had a simple script, which pulled data and arranged it in 4 sections.

// load records
list($articleRecords, $articlesMetaData) = getRecords(array(
'tableName' => 'am_articles',
'where' => "approved = 1 AND category NOT LIKE '%357%'" . $andDontRepeatSlideshowClause,
));

$articlesInBlock1 = array_slice($articleRecords, 0, 1); // article 1
$articlesInBlock2 = array_slice($articleRecords, 1, 2); // article 2-4 (3 articles)
$articlesInBlock3 = array_slice($articleRecords, 3, 8); // article 5-10 (5 articles)
$articlesInBlock4 = array_slice($articleRecords, 11, 11); // article 11-20 (10 articles)
$articlesInBlock5 = array_slice($articleRecords, 22, 1); // article 11-20 (10 articles)

// from the media
list($linksoutRecords, $linksoutMetaData) = getRecords(array(
'tableName' => 'am_articles',
'where' => "approved = 1 AND media = 1",
'orderBy' => "publishDate DESC",
'allowSearch' => false,
'limit' => 5,
));

// do not repeat
$andDontRepeatSlideshowClause = '';
if ($linksoutRecords) {
$slideshowNumsCsv = join(',', array_pluck($linksoutRecords, 'num'));
$andDontRepeatSlideshowClause = ' AND num NOT IN (' . $slideshowNumsCsv . ')';
}



I have had to modify the code and add several sections, which I think is causing latency issues and server crashes. Is there a way to have a simpler way of listing?

// load records
list($oneRecords, $oneMetaData) = getRecords(array(
'tableName' => 'am_articles',
'where' => "approved = 1 AND category LIKE '%176%'" . $andDontRepeatSlideshowClause1,
));

$oneInBlock1 = array_slice($oneRecords, 0, 1); // article 1
$oneInBlock2 = array_slice($oneRecords, 1, 4); // article 2-4 (3 articles)



Since the site's launch, I have had to add several more sections as evident by the home page. Each section has a similar script like this.


// load records
list($oneRecords, $oneMetaData) = getRecords(array(
'tableName' => 'am_articles',
'where' => "approved = 1 AND category LIKE '%176%'" . $andDontRepeatSlideshowClause1,
));

$oneInBlock1 = array_slice($oneRecords, 0, 1); // article 1
$oneInBlock2 = array_slice($oneRecords, 1, 4); // article 2-4 (3 articles)



I also use the navbar to load articles with a script like this:

// load main article list
list($category3Records, $category3MetaData) = getRecords(array(
'tableName' => 'am_articles',
'perPage' => '4',
'where' => "category LIKE '%282%' AND approved = 1",
'allowSearch' => false,
));


So in the end, there is close to 25 or so hits to the query mysql and display results.

Is there a more efficient way of achieving this? I think this causes my server to slow down and crash.