Joomla: Write and call a helper function in a component

Fledgling Joomla / PHP developer, hitting the wall, understanding how to do it. All I found was for older versions of Joomla or other frameworks, and therefore it all got confused for the first time.

I want to have a helper function that I can call from anywhere in my component. This basically requires userID and returns its full name, let them say hair color and height. Here's the function:

function get_profile_info($userID) {

        $db =& JFactory::getDBO();          
        $query = $db->getQuery(true);

        $query->SELECT('u.id as UserID
                    , u.name AS Name
                    , up.profile_value AS Hair
                    , up2.profile_value AS Height
                    ');
        $query->FROM (' #__users AS u');
        $query->LEFTJOIN (' #__user_profiles AS up ON u.id = up.user_id AND up.ordering = 1');
        $query->LEFTJOIN (' #__user_profiles AS up ON u.id = up.user_id AND up.ordering = 2');
        $query->WHERE(' u.id = '.$userID.'');
        $query->GROUPBY (' u.id
                    , u.name
                    , up.profile_value
                    ');


       $db->setQuery($query);   
       return $query;                   
}

I would put this in a file called "lookups.php" in the "helpers" folder of my component ... but here, where I'm not sure what to do next. Top of lookups.php is required:

<?php defined ( '_JEXEC' ) or die;

So, two questions: 1) Do I put everything in a class or save it as a series of functions (since there will be others)?

2) Name, Hair Height (view.html.php/default.php)?

!

==================================

: @Shaz , ( , ):

lookups.php   

abstract class LookupHelper {

        var $Name;
        var $Hair;
        var $Height;

    public function get_profile_info($userID) {
                ... 
                (same as above until the next line)

                $db->setQuery($query);
                $result=$db->loadRow();
                $Name = $result[1];
                $Hair = $result[2];
                $Height = $result[3];
        $getprofileinfo = array(Name=>$Name, Hair=>$Hair, Height=>$Height);
                $return $getprofileinfo;
           }
    }

default.php(, view.html.php):

    $getprofileinfo = Jview::loadHelper('lookups'); //got an error without this
    $getprofileinfo = LookupHelper::get_profile_info($userID);

    $Name = $getprofileinfo[Name];
    $Hair = $getprofileinfo[Hair];
    $Height = $getprofileinfo[Height];

... - , ( , , ). ?

!

+1
2
  • :

    HelloWorldHelper { /* */ }

  • :

    $var = HelloWorldHelper:: get_profile_info ($ thatId);

0

Joomla, ?

Joomla , , , , .

0

All Articles