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

时间:2018-03-15 18:22:50

标签: php laravel session views

这是我的问题.. 我的Session with 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方法..哦和另外1件事,我使用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来制作它。

1 个答案:

答案 0 :(得分:0)

解决了!

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