3066 Posts in 866 Topics by 648 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1505 Views |
-
Help Returning Current Page ID

14 October 2010 at 5:13pm Last edited: 14 October 2010 10:24pm
I've been working on getting a random banner going and after wading through several tutorials all is working. In the Page_Controller of Page.php I have:
public function RandomBanners() {
$foldername = "bannerimgs";
$folder = Folder::findOrMake($foldername);
return DataObject::get("Image", "ParentID = $folder->ID", "RAND()", '', 3);
}Under the init function I added:
Requirements::javascript("mysite/javascript/jquery-1.3.2.min.js");
Requirements::javascript("mysite/javascript/jquery.cycle.lite.1.0.js");Requirements::customScript("
jQuery(document).ready(function(){
jQuery('#bannerimages').cycle({ delay: 5000, speed: 4000 });
jQuery('#bannerimages').show();
});"
);In Page.ss:
<div id="bannerimages">
<% control RandomBanners %>
<img src="$URL" width="$width" height="$height" alt="" />
<% end_control %>
</div>And no problems, however if there are better ways to do the above please feel free to let me know. But now I would like to be able to select a different folder based on the current page ID. For example, if I'm on the homepage and it has a page ID of 1 I might want to select the random images from a different folder. I've tried adding:
public function RandomBanners() {
if($this ->ID = 1) {
$foldername = "newfolder";
}
else {
$foldername = "bannerimgs";
}
$folder = Folder::findOrMake($foldername);
return DataObject::get("Image", "ParentID = $folder->ID", "RAND()", '', 3);
}And what it appears to be doing is checking to see if any of the pages using the Page class have an ID of 1, and not the CURRENT page. So of course, if I use the ID of any real page it always uses the same folder.
Basically, all I want to be able to do is to check inside a function if the page I am on has an ID of 1 and then proceed accordingly.
Any ideas? All help appreciated.TIA
-
Re: Help Returning Current Page ID

15 October 2010 at 6:34pm Last edited: 15 October 2010 6:42pm
I think you're setting the ID to 1
Should beif($this ->ID = 1)
if ($this->ID == 1)
You could add a database entry to the page class like 'BannerFolder' and fill it from a dropdown in the CMS that lets you choose a folder... if none is specified you could set a default in your function. I learned about that here http://ssorg.bigbird.silverstripe.com/widgets-2/show/253448
So in Page class of Page.php
static $db = array(
'BannerFolder' => 'Int'
);public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.BannerImages", new DropdownField('BannerFolder','Folder that contains Banner Images:', DataObject::get("Folder")->toDropdownMap()));
return $fields;
}In Page_Controller on Page.php
function RandomBanners(){
if ($this->BannerFolder) $BannerFolderID = $this->BannerFolder;
else $BannerFolderID = 4 //ID of default folder
return DataObject::get("Image", "ParentID = $BannerFolderID", "RAND()", '', 3);
} -
Re: Help Returning Current Page ID

16 October 2010 at 2:27pm
That's awesome, thanks JonoM! That makes sense and I have it all working as per your code example
| 1505 Views | ||
|
Page:
1
|
Go to Top |


