在Joomla增加一个点击计数器

时间:2022-04-09 15:19:02

I'm creating a custom component for Joomla 2.5. I want to add in a hit counter for the detailed view (like on Joomla content etc.)

我正在为Joomla 2.5创建一个自定义组件。我想为详细的视图添加一个点击计数器(如Joomla内容等)

So I've done some research and found the JTable::hit function which I'm assuming is what I need (Joomla docs page here). However I'm highly unclear as to where I need to implement this. Is it in the model somewhere when I retrieve the row from the database - or is it somewhere in the view? Advise would be much appreciated!

所以我做了一些研究,发现了JTable::hit函数,我假设这就是我需要的(Joomla docs页面)。然而,我非常不清楚我需要在哪里实现这一点。当我从数据库中检索行时,它是在模型中的某个位置,还是在视图中的某个位置?非常感谢您的建议!

1 个解决方案

#1


2  

If you have a look at how they did it in Joomla, you will see that they implemented the functionality in the model, and call the method from the view.

如果您了解他们在Joomla中是如何实现的,您将看到他们在模型中实现了功能,并从视图中调用方法。

For example, article.php model has

例如,文章。php模式

public function hit($pk = 0)
{
        $hitcount = JRequest::getInt('hitcount', 1);

        if ($hitcount)
        {
            // Initialise variables.
            $pk = (!empty($pk)) ? $pk : (int) $this->getState('article.id');
            $db = $this->getDbo();

            $db->setQuery(
                    'UPDATE #__content' .
                    ' SET hits = hits + 1' .
                    ' WHERE id = '.(int) $pk
            );

            if (!$db->query()) {
                    $this->setError($db->getErrorMsg());
                    return false;
            }
        }

        return true;
}

and

view.html.php has

view.html。php有

$model = $this->getModel();
$model->hit();

#1


2  

If you have a look at how they did it in Joomla, you will see that they implemented the functionality in the model, and call the method from the view.

如果您了解他们在Joomla中是如何实现的,您将看到他们在模型中实现了功能,并从视图中调用方法。

For example, article.php model has

例如,文章。php模式

public function hit($pk = 0)
{
        $hitcount = JRequest::getInt('hitcount', 1);

        if ($hitcount)
        {
            // Initialise variables.
            $pk = (!empty($pk)) ? $pk : (int) $this->getState('article.id');
            $db = $this->getDbo();

            $db->setQuery(
                    'UPDATE #__content' .
                    ' SET hits = hits + 1' .
                    ' WHERE id = '.(int) $pk
            );

            if (!$db->query()) {
                    $this->setError($db->getErrorMsg());
                    return false;
            }
        }

        return true;
}

and

view.html.php has

view.html。php有

$model = $this->getModel();
$model->hit();