dongliao8069 2018-03-15 18:22 采纳率: 0%
浏览 72
已采纳

使用Sessions PHP的电影预告片页面视图

Here is my problem.. I have Session with Array looking like this

array(3) { [0]=> array(1) { ["trailer"]=> array(1) { ["id"]=> string(5) "id-10" } } [1]=> array(1) { ["trailer"]=> array(1) { ["id"]=> string(5) "id-11" } } [2]=> array(1) { ["trailer"]=> array(1) { ["id"]=> string(5) "id-10" } } } 

and an php method .. oh and 1 more thing, im using laravel 5.6

My Code:

public function updateTrailerViews($id = 1){

        $trailerViews = array(array());
        $trailerViewsCount = \Session::get('trailerViews') == NULL ? 1 : count(\Session::get('trailerViews'));

        if($trailer['id'] == NULL){
            $id = 1;
        }

        if (\Session::get('trailerViews') == NULL) {
                for ($i=0; $i < $trailerViewsCount; $i++) {
                    $trailerViews[$i]['trailer']['id'] = 'id-'.$id;

                    //$trailer['views'] = $trailer['views']+1;
                    //$trailer->save();
                }

                \Session::put('trailerViews', $trailerViews);
        }else{
            for($i=0;$i<$trailerViewsCount;$i++){
                if(\Session::get('trailerViews')[$i]['trailer']['id'] != 'id-'.$id){
                        $idNotExist = 'true';
                }else{
                    $idNotExist = 'false';
                }
            }

            if($idNotExist == 'true'){
                $trailerViewsCount1 = $trailerViewsCount+1;
                for($int=0;$int<$trailerViewsCount1;$int++){
                    if($int == $trailerViewsCount1-1){
                        // if is the last integer of the loop add the new record
                        $trailerViews[$int]['trailer']['id'] = 'id-'.$id;

                    }else{
                        for($int1 = 0; $int1 < $trailerViewsCount; $int1++){
                            // if the integer is not the last number of the loop add the previous records from the session
                            $trailerID = \Session::get('trailerViews')[$int1]['trailer']['id'];
                            $trailerViews[$int1]['trailer']['id'] = $trailerID;
                        }
                    }

                    \Session::put('trailerViews', $trailerViews);
                }
            }
        }

        $trailerViewsCountReturn = count(\Session::get('trailerViews'));
        //$trailerViewsNumber = \AppHelper::instance()->short_number_format($trailer['views']);
        return var_dump(\Session::get('trailerViews'));
    }

The problem is when im trying to check the unique id of the page sometimes have a bug, when open page with id=2 then id=5 or other.. and again open id=2 the id is going to be added again.

I wanna make it with unique page id's only.

  • 写回答

1条回答 默认 最新

  • douweiduo7526 2018-03-15 20:29
    关注

    SOLVED!

    public function updateTrailerViews($id = 1){
        $trailer = Trailers::find($id);
    
        $trailerViews = array(array());
    
        $trailerViewsCount = \Session::get('trailerViews') == NULL ? 1 : count(\Session::get('trailerViews'));
    
        if($trailer['id'] == NULL){
            $id = 1;
        }
    
        if (\Session::get('trailerViews') == NULL) {
            for ($i=0; $i < $trailerViewsCount; $i++) {
                $trailerViews[$i] = 'id-'.$id;
    
                $trailer['views'] = $trailer['views']+1;
                $trailer->save();
            }
    
            \Session::put('trailerViews', $trailerViews);
        }else{
            if(!in_array('id-'.$id, \Session::get('trailerViews'))){
                if($trailer['id'] == $id){
                    $idNotExist = 'true';
    
                    $trailer['views'] = $trailer['views']+1;
                    $trailer->save();
                }else{
                    $idNotExist = 'false';
                }
            }else{
                $idNotExist = 'false';
            }
    
            if($idNotExist == 'true'){
                $trailerViewsCount1 = $trailerViewsCount+1;
                for($int=0;$int<$trailerViewsCount1;$int++){
                    if($int == $trailerViewsCount1-1){
                        // if is the last integer of the loop add the new record
                        $trailerViews[$int] = 'id-'.$id;
                    }else{
                        for($int1 = 0; $int1 < $trailerViewsCount; $int1++){
                            // if the integer is not the last number of the loop add the previous records from the session
                            $trailerID = \Session::get('trailerViews')[$int1];
                            $trailerViews[$int1] = $trailerID;
                        }
                    }
                    $trailerViews = array_unique($trailerViews);
                    \Session::put('trailerViews', $trailerViews);
                }
            }
        }
    
        $trailerViewsCountReturn = count(\Session::get('trailerViews'));
        $trailerViewsNumber = \AppHelper::instance()->short_number_format($trailer['views']);
        return $trailerViewsNumber;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记