Prestashop CMS页面查看计数器

时间:2017-03-22 14:57:24

标签: php mysql prestashop prestashop-1.6

我正在努力使用CMS页面查看计数器。

我将新功能添加到 classes / controller / FrontController.php 如下所示

public static function getCMSViewed($id_cms){
    $viewcms = Db::getInstance()->getRow('SELECT SUM(pv.counter) AS total 
        FROM '._DB_PREFIX_.'page_viewed pv
        LEFT JOIN '._DB_PREFIX_.'cms c ON pv.id_page = c.id_cms 
        LEFT JOIN '._DB_PREFIX_.'page p ON pv.id_page = p.id_page
        LEFT JOIN '._DB_PREFIX_.'page_type pt ON p.id_page_type = pt.id_page_type
        WHERE pt.name = \'cms\'');
        return isset($viewcms['total']) ? $viewcms['total'] : 0;
}

在cms.tpl文件中我添加了 {FrontController :: getCMSViewed(Tools :: getValue('id_cms'))}

现在计数器正在“正常工作”,但在所有Prestashop CMS页面上显示其中一个ID = 15的页面的相同增加值

enter image description here

1 个答案:

答案 0 :(得分:0)

我看到你将CMS ID传递给你的函数,但我没有看到你在任何地方使用它。您还可以使用与列id_page和id_cms匹配的cms连接。这意味着您的查询不会获得您期望的计数。你想要更接近这一点。

public static function getCMSViewed($id_cms){
$viewcms = Db::getInstance()->getRow('SELECT pv.counter AS total 
    FROM '._DB_PREFIX_.'page_viewed pv
    LEFT JOIN '._DB_PREFIX_.'page p ON pv.id_page = p.id_page
    LEFT JOIN '._DB_PREFIX_.'page_type pt ON p.id_page_type = pt.id_page_type
    WHERE pt.name = \'cms\' AND p.`id_object` = '.intval($id_cms) );
    return isset($viewcms['total']) ? $viewcms['total'] : 0;
}

表格page包含id_object列中引用的任何网页的ID,因此您希望将$id_cms传入p.id_object进行匹配。通过该匹配,sql可以直接从page_viewed表返回计数。根本不需要加入cms表。