电影预告片页面浏览量 PHP
Movie Trailer Page views with Sessions PHP
这是我的问题..
我的 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方法..哦还有一件事,我正在使用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。
已解决!
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;
}
这是我的问题.. 我的 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方法..哦还有一件事,我正在使用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。
已解决!
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;
}