Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions

Help Returning Current Page ID


3 Posts   2043 Views


14 October 2010 at 5:13pm (Last edited: 14 October 2010 10:24pm), Community Member, 10 Posts

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:


jQuery('#bannerimages').cycle({ delay: 5000, speed: 4000 });


<div id="bannerimages">
<% control RandomBanners %>
<img src="$URL" width="$width" height="$height" alt="" />
<% end_control %>

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.



15 October 2010 at 6:34pm (Last edited: 15 October 2010 6:42pm), Community Member, 103 Posts

I think you're setting the ID to 1

if($this ->ID = 1)

Should be

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

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);


16 October 2010 at 2:27pm Community Member, 10 Posts

That's awesome, thanks JonoM! That makes sense and I have it all working as per your code example :-)