php记录点击量

作者: riyihu | 来源:发表于2018-11-24 10:47 被阅读1次

    以下demo是记录某一按钮点击次数,延伸记录文章浏览次数,app里功能使用频率统计
    先建一张点击量统计表


    statistisc.png
    CREATE TABLE `statistics` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '统计表',
      `type` int(11) DEFAULT NULL COMMENT '要统计的字段,文章id,按钮id...',
      `numbers` int(10) unsigned DEFAULT NULL COMMENT '点击量',
      `date` date DEFAULT NULL COMMENT '年月日',
      `created_at` timestamp NULL DEFAULT NULL,
      `updated_at` timestamp NULL DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
    

    我这里按天统计,懒得截取所以加了个date
    思路:某一按钮在当天点击一次,点击量增加一次(大于1时不新增条数),不记录谁点击(点赞的话是点一下新增一条,并记录谁点赞),这样方便数据统计。
    当然也可以按月,或者不要时间统计(文章昨日浏览量,今日浏览量,总浏览量)

    html页面

    .
    .
    .
    //省略头部
    <div>
       <fieldset class="layui-elem-field site-demo-button" style="margin-top: 30px;">
         <legend>按钮测试</legend>
    
         <div>
           <button class="layui-btn layui-btn-primary" data-type="1">按钮1</button>
           <button class="layui-btn" data-type="2">按钮2</button>
           <button class="layui-btn layui-btn-normal" data-type="3">按钮3</button>
           <button class="layui-btn layui-btn-warm" data-type="4">按钮4</button>
           <button class="layui-btn layui-btn-danger" data-type="5">按钮5</button>
         </div>
       </fieldset>
    
       <div style="margin: auto;">
           <table class="layui-table">
           <colgroup>
             <col width="150">
             <col width="150">
             <col width="200">
             <col>
           </colgroup>
           <thead>
             <tr>
               <th>按钮</th>
               <th>点击次数</th>
               <th>日期</th>
               <th>最后访问时间</th>
             </tr> 
           </thead>
           <tbody>
           <...@if(非空判断,懒得写)..> 
              @foreach($recored as $re)
               <tr>
                 <td>按钮{{ $re['type'] }}</td>
                 <td>{{ $re['numbers'] }}</td>
                 <td>{{ $re['date'] }}</td>
                 <td>{{ $re['updated_at']}}</td>
               </tr>
              @endforeach
           </tbody>
          </table>
       </div>
      
    
      </div>
    
    <script>
      layui.use(['layer'], function(){
       var layer = layui.layer;
       $ = layui.jquery;
    //监听点击
        $('.layui-btn').on('click', function(data){
          var b_type = $(this).data('type');
          $.ajax({
                url: 'recored',
                type: 'POST',
                dataType: 'json',
                data: {type:b_type},
            })
            .done(function(respones) {
                location.reload();
            })
            .fail(function() {
                console.log("error");
            })
          return false;
        });
    });
    </script>
    

    php代码

    //Model
    <?php
    
    namespace App\Model;
    
    use Illuminate\Database\Eloquent\Model;
    
    class StatisticsModel extends Model
    {
    
        protected $table= 'statistics';
        
        public  function updateOrAdd($type,$date)
        {   
            
            //按天统计
            $query = $this->whereTypeAndDate($type, $date)->first();
            
            if (!$query) {
                $add = $this->insert(['type'=>$type, 'date'=>$date,'numbers'=>1]);
            } else {
                $update = $this->whereTypeAndDate($type, $date)->increment('numbers', 1);
            }
            $arr = [
                'msg'=>'记录测试',
            ];
            return $arr;
            
        }
    
    
        public function result()
        {
            $query = $this->orderBy('type')->get()->toArray();
            return $query;
    
        }
    }
    
    
    

    Controllers

    <?php
    namespace App\Http\Controllers\Statistics;
    use App\Http\Controllers\Controller;
    use App\Model\StatisticsModel;
    use Illuminate\Http\Request;
    
    class StatisticsController extends Controller
    {   
        
        //统计列表
        public function index()
        {   
            $statistics =new  StatisticsModel;
            $recored = $statistics->result();
            
            return view('admin.statistics.index',['recored'=>$recored]);
           
        }
    
        
        public function recored()
        {  
           $type = request()->post('type');
           $date = date('Y-m-d');
           $statistics =new  StatisticsModel;
           $re = $statistics->updateOrAdd($type,$date);
    
           return $re;
        }
    
    }
    

    路由

    // 点击测试
    Route::get('/statistics', 'Statistics\StatisticsController@index');
    Route::post('/recored', 'Statistics\StatisticsController@recored');
    

    相关文章

      网友评论

        本文标题:php记录点击量

        本文链接:https://www.haomeiwen.com/subject/dwpeqqtx.html