3063 Posts in 864 Topics by 646 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 442 Views |
-
Custom Included in Menu

15 March 2011 at 11:47pm Last edited: 16 March 2011 1:49am
Hi,
I am trying to write a simple function which gets every page via the SiteTree, loops through each page and adds it to an array if included (added field in page class) = true.
I'm not particularly experienced with OO PHP and feel there's something fundamentally wrong with my code here.
function GetTopMenu() {
$AllPages = DataObject::get("SiteTree");
$IncludedPages = array();
foreach($AllPages as $Page) {//select the pages with "included = true"
array_push($IncludedPages, $Page);
}
return $IncludedPages;
}This is just a test to see if I can take every page and put it into another array (not sure about putting objects into arrays, is that how it's done?), but returns nothing.
Any help would be greatly appreciated.
EDIT: Nevermind, got it.
function GetTopMenu() {
$AllPages = DataObject::get("SiteTree");
$OtherPages = new DataObjectSet();
foreach($AllPages as $Page) {//select the pages with "included = true"
if($Page->Included==true){
$OtherPages->push($Page);
}
}
return $OtherPages;
} -
Re: Custom Included in Menu

16 March 2011 at 6:00pm
You shouldn't really iterate over all the pages again after your query unless you need to. If include is a column in your database just filter it using the WHERE cause.
function GetTopMenu() {
return DataObject::get("SiteTree", "\"Included\" = true");
}
| 442 Views | ||
|
Page:
1
|
Go to Top |


