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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

How to send parameters from template to a function in PHP


Reply

4 Posts   190 Views

Avatar
maga

9 May 2014 at 2:27am Community Member, 11 Posts

I created a new function (getBirthday) in my file CatalogoArtisti.php to calculate age, depending on your birthday, I would like to hook this function in my template but i do not know how to do.
To be more clear in my template I have the birthday ($DateBirthDay) then I would pass this data to the function (getBirthday) to display in the template the calculation of the age.

==== Silverstripe version 2.4 ====

CatalogoArtisti.php
...........

getBirthday function ($birthday)
{
$ datetime1 = new DateTime ($ birthday);
$ datetime2 = new DateTime (date ('Y-m-d'));
$ diff = $ datetime1-> diff ($ datetime2);

return $ diff-> format ('% y');

CatalogoArtisti.ss
.............

<% Control Artista_Album%> <p> class = "arialBold"> $DateBirthDay </ p>
   
   / / I need the age?

<% End_control%>

Thank you all in advance for the help that you will give me
Have a nice day
MG

Avatar
IOTI

9 May 2014 at 5:21am Community Member, 189 Posts

Hi Maga,

Try moving the getBirthday function to the class that your <% Control Artista_Album %> is looping through. In that class modify the getBirthday function like so.

getBirthday function ()
{
$birthday = $this->DateBirthDay; //assuming that DateBirthDay is a db field
$ datetime1 = new DateTime ($ birthday);
$ datetime2 = new DateTime (date ('Y-m-d'));
$ diff = $ datetime1-> diff ($ datetime2);

return $ diff-> format ('% y');

}

Then in your <% Control Artista_Album %> block, just replace $DateBirthDay with $Birthday

<% Control Artista_Album%>

<p> class = "arialBold"> $Birthday</ p>

<% End_control%>

Avatar
maga

9 May 2014 at 8:55pm Community Member, 11 Posts

HI YOTI,
thanks for your help but I tried to follow your directions but the template / html do not return any data?

For additional information:

The template catalogoartisti.ss is based on catalogoartisti.php,
into album.php there is the relationship with artista.php and i use the <% control Artista_Album%> to retrieve the field DataNascita (BirthDay)

The function that you suggested I inserted it in catalogoartisti.php and then I called from template catalogoartisti.ss

Artista.php
.....
class Artista extends DataObject {
   public static $db = array(
      
      "DataNascita" => "Date", //BirthDay
      
   );

Album. php
......
class Album extends DataObject {
   
   public static $has_one = array(
   "Artista_Album" => "Artista"    
   );

catalogoartisti.php
.....

public function getBirthday()
      {
         $birthday = $this->DataNascita; // DataNascita (BirthDay ) is a db field
         $datetime1 = new DateTime($birthday);
       $datetime2 = new DateTime (date ('Y-m-d'));
         $diff = $datetime1-> diff ($datetime2);
       return $diff-> format ('%y');      
      }

CatalogoArtisti.ss
......
<% control Artista_Album %>                  
<p class="arialBold">$Birthday - sss </p>
                                             
                                             
Definitely something wrong I am a novice in programming.
I look forward to your help
thanks
greetings
MG

Avatar
Mo

22 May 2014 at 11:55am Community Member, 509 Posts

Hi maga,

Could you post up all the contents of your catalogoartisti.php?

Cheers,

Mo