简体   繁体   English

Prestashop CMS页面浏览计数器

[英]Prestashop CMS page view counter

I'm struggling with CMS page view counter. 我在CMS页面浏览计数器方面苦苦挣扎。

I'm add new function to classes/controller/FrontController.php like below 我向class / 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;
}

and in cms.tpl file i have add {FrontController::getCMSViewed(Tools::getValue('id_cms'))} 并在cms.tpl文件中添加了{FrontController :: getCMSViewed(Tools :: getValue('id_cms'))}

For now the counter is "working" but showing on all Prestashop CMS pages the same increased value of one of the pages with ID=15 目前,该计数器正在“运行”,但在所有Prestashop CMS页面上显示ID = 15的页面之一的增加值相同

在此处输入图片说明

I see you are passing a CMS id into your function, but I do not see you using it anywhere. 我看到您正在将CMS ID传递到函数中,但是我看不到您在任何地方使用它。 You also have the join on cms matching against columns id_page and id_cms. 您还可以在cms上加入与id_page和id_cms列匹配的联接。 Which means your query is not going to grab the count you expect. 这意味着您的查询不会获取您期望的计数。 You want something closer to this. 您想要更接近此的东西。

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

The table page holds the id of whatever page it is referencing in the id_object column, so you want to match the $id_cms you pass in against p.id_object . 表格pageid_object列中保存了它所引用的任何页面的ID,因此您希望将传入的$id_cmsp.id_object With that match the sql can return the count directly from the page_viewed table. 通过该匹配,sql可以直接从page_viewed表返回计数。 No need to join in the cms table at all. 完全不需要加入cms表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM