如何在Joomla组件上调用变量项

问题描述 投票:0回答:1

我正在尝试在Joomla中创建我的第一个组件,但我不明白它是如何工作的:D

我使用“组件创建器”创建一个基本组件。 (我节省了很多时间...)。

现在,我的数据库中有一个表,我在其中放置了数据:

id = 1
name = Andrea 
note = Ciao

现在,我想使用Joomla语言从页面中调用它。

在我的models / component.php中,我写道:

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

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

        return $value;
    }
}

并且在我的default.php上写了

$model=$this->Variabili();

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

//print
print_r($items);

但是在页面上我出现此错误:

0 Call to undefined method Calcolo_imposteViewCalcoloonline::Variabili()

哪里有错误?

[因为我是初学者,请对我保持温柔:D

提前感谢

安德里亚

variables joomla
1个回答
1
投票

您犯了一些错误。我已经重写了您的函数,以便您可以使用它。让我们看一下-

model,看起来还不错。只需将其更改为-

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;
    }
}

现在不从model调用default.php函数,而不是在view.html.php文件中编写代码。

首先在display文件的view.html.php函数内部,通过使用model函数获得getModel()实例。

$model = $this->getModel();

现在您可以通过使用此$model类实例来获取项目。

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

这将从数据库表中获取数据。并且您可以使用default.php文件中的数据。

只需尝试default.php文件-

print_r($this->items);
© www.soinside.com 2019 - 2024. All rights reserved.