Jump to content

Combining 20 queries into just one


floridaDev
 Share

Recommended Posts

Below is just an example of how I'm currently calling pages that start with the letter "M". I'm doing this for A-Z and my template file has the script below x 26 times. Is there anyway this could be improved?

$m_links = "";

$find_m_pages = $page->children("template=article, City^=M");
//if pages exist build the outer ul.
if($find_m_pages->count()) {
$m_links .="<li id='m'><ul>";


//build the single li links
foreach($find_m_pages as $m) {
$m_links .= '<li><a href="'.$m->url.'">Link</a></li>';
} 

$m_links .= "</ul></li>";

} else {
//nothing exist just leave it blank
$m_links .= "";

}

 

Link to comment
Share on other sites

$children = $page->children("template=article");
foreach(['A', 'B', …] as $letter) {
	$startingWithLetter = $children->find("City^=$letter");
	if(!$startingWithLetter->count()) continue;

	// Do things here
}

Only the first find is a call to the database, while all the other ones in the foreach are just in memory searches and therefore quite a bit faster.

  • Like 9
Link to comment
Share on other sites

8 hours ago, LostKobrakai said:

$children = $page->children("template=article");
foreach(['A', 'B', …] as $letter) {
	$startingWithLetter = $children->find("City^=$letter");
	if(!$startingWithLetter->count()) continue;

	// Do things here
}

Only the first find is a call to the database, while all the other ones in the foreach are just in memory searches and therefore quite a bit faster.

Just an addition. You could use the range() function from PHP to create the array. For example like this: 

range('A', 'Z')

 

  • Like 6
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...