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   252 Views

Avatar
maga

Community Member, 11 Posts

9 May 2014 at 2:27am

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
copernican

Community Member, 189 Posts

9 May 2014 at 5:21am

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

Community Member, 11 Posts

9 May 2014 at 8:55pm

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

Community Member, 510 Posts

22 May 2014 at 11:55am

Hi maga,

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

Cheers,

Mo