Search code examples
variablesjoomla

Ho to call variables items on Joomla Component


I'm trying to create my first component in Joomla but I can't understand how it works really :D

I create a basic component with "Component Creator". (I saved a lot of time...).

Now, I have a table in my DB where I've put my data:

id = 1
name = Andrea 
note = Ciao

Now, I want to call it from the page using Joomla Language.

On my models/component.php i wrote:

class Variabili extends JModelLegacy
{
    function estraivariabili()
    {
        $db =& JFactory::getDBO(); 

        $db->setQuery("SELECT * FROM #__table")->loadObjectList();

        return $value;
    }
}

and on my default.php I wrote

$model=$this->Variabili();

//call the method
$items=$model->estraivariabili();

//print
print_r($items);

But on the page I have this error:

0 Call to undefined method Calcolo_imposteViewCalcoloonline::Variabili()

Where is a mistake?

Please be gentle with me because I'm a beginner: D

Thanks in advance

Andrea


Solution

  • You have committed a few wrongs. I've rewritten your functions so that you can get it. Let's have a look-

    The model, it looks almost okay. Just change it like-

    class Variabili extends JModelLegacy
    {
        // Make the function public
        public function estraivariabili()
        {
            $db = &JFactory::getDBO(); 
    
            // Put the result into a variable first, then return it.
            $value = $db->setQuery("SELECT * FROM #__table")->loadObjectList();
    
            return $value;
        }
    }
    

    And now call the model functions not from default.php, rather than write your code inside view.html.php file.

    Inside the display function of view.html.php file first, get the model instance by using getModel() function.

    $model = $this->getModel();
    

    Now you can get the items by using this $model class instance.

    $this->items = $model->estraivariabili();
    

    This will bring you the data from the database table. And you can use the data at default.php file.

    Just try at default.php file-

    print_r($this->items);