【问题标题】:Movie Trailer Page views with Sessions PHP使用 Sessions PHP 的电影预告片页面浏览量
【发布时间】:2018-08-24 16:34:51
【问题描述】:

这是我的问题.. 我的 Session 与 Array 看起来像这样

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

还有一个 php 方法 .. 哦,还有一件事,我使用的是 laravel 5.6

我的代码:

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

问题是当我试图检查页面的唯一 id 时有时会出现错误,当打开 id=2 的页面然后 id=5 或其他.. 再次打开 id=2 时,将添加 id再次。

我只想使用唯一的页面 ID。

【问题讨论】:

    标签: php laravel session views


    【解决方案1】:

    解决了!

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-05
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多