Jump to:

23373 Posts in 18175 Topics by 2865 members

General Questions

SilverStripe Forums » General Questions » How to send parameters from template to a function in PHP

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

Page: 1
Go to End
Author Topic: 141 Views
  • maga
    Avatar
    Community Member
    11 Posts

    How to send parameters from template to a function in PHP Link to this post

    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

  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: How to send parameters from template to a function in PHP Link to this post

    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%>

  • maga
    Avatar
    Community Member
    11 Posts

    Re: How to send parameters from template to a function in PHP Link to this post

    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

  • Mo
    Avatar
    Community Member
    505 Posts

    Re: How to send parameters from template to a function in PHP Link to this post

    Hi maga,

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

    Cheers,

    Mo

    141 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.