php - 通知图标点击后隐藏号码

标签 php mysql laravel octobercms

我使用十月,我已经使用构建器制作了插件,我只是在菜单中添加了通知图标。

我想显示“!”如果有新项目并隐藏“!”如果用户单击该按钮(它会打开引导模式)。当我添加新项目时,它显示“!”再次等等...我不知道最好的方法是什么。如果您能帮我解决这个问题,谢谢您。

菜单:

<li>
    <a data-toggle="modal" data-target="#itemModal"><i class="fa fa-bell"></i>
        <span>! {{ Item }}</span></a>
</li>

代码:

function onStart() 
{
   $this['Item'] = Db::table('items')->count();

}

最佳答案

为此,您需要数据库表,并且需要保存上次查看/点击日期。

所以这里的想法就像您将在用户单击!时添加当前日期,您将显示引导模式并触发ajax请求并设置当前日期。

当时触发ajax请求并为参数添加新日期。

为此,您可以使用 CMS 默认参数表

<?php
use System\Models\Parameter;
....

// something like this in ajax request
Parameter::set('your_plugin_name::items.last_click_date', time() );


// this is when page reload    
use System\Models\Parameter;
function onStart() 
{
    // now
    $lastClickDate = Parameter::get('your_plugin_name::items.last_click_date');
    if($lastClickDate == null) {
        // first time it will just count items directly as user never click on icon
        $count = Db::table('items')->count();
    }
    else {  
        // new this will call next time as user called that modal and we added click date 
        // so we compare if any record added after that date if it is then show `!`
        $count = Db::table('items')->where('created_at', '>', $lastClickDate)->count(); 
    }
    $this['item_count'] = $count; 
    $this['new_items'] = $count > 0 ? true : false;
}    

html

<li>
    <a data-toggle="modal" data-target="#itemModal">
        <i class="fa fa-bell"></i>
        {% if new_items %}
            <span>! {{ item_count }} </span>
        {% endif %}
    </a>
</li

如果您遇到任何问题,请添加评论。

关于php - 通知图标点击后隐藏号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52141783/

相关文章:

php - 出色地处理从代码中不同位置浮出的非对象错误的属性

php - 语法错误,意外 'endforeach'

php - 从 MySQL 将标签列表转换为结构化数组的最佳方法?

php - 如何将 MySQL 中的 id 添加到 session cookie

MySQL - 检查 InnoDB 表中的数据是否已更改的最快方法

mysql - 根据条件从其他 2 列中选择不同的一列(如何配对它们?)

mysql - 如何在事件日志的一个 "activity"中存储(在 MySQL 中)多个更新?

javascript - JS SDK facebook API 通行证

laravel - 不同的值(value)观与采摘

php - 空白/空搜索表单字段