floridaDev Posted December 13, 2016 Posted December 13, 2016 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 .= ""; }
LostKobrakai Posted December 13, 2016 Posted December 13, 2016 $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. 9
Orkun Posted December 14, 2016 Posted December 14, 2016 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') 6
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now