Advertisement
Guest User

Problem comment subcomment

a guest
Sep 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.22 KB | None | 0 0
  1. $commenti = Commenti::select('commenti.id AS id','users.username AS username',
  2.         'commenti.id_commenti AS id_commenti','commenti.commento AS commento','commenti.created_at AS created_at')
  3.         ->leftJoin('users', 'users.id', 'commenti.id_users')
  4.         ->where('id_schede', '=',  $request['id_schede'])
  5.         ->get();
  6.  
  7.         $array_commenti = [];
  8.         $array_subcommenti = [];
  9.  
  10.         foreach ($commenti as $cmt) {
  11.            
  12.             if(!$cmt->id_commenti){
  13.                 $temp_obj = new \stdClass();
  14.                 $temp_obj->id = $cmt->id;
  15.                 $temp_obj->username = $cmt->username;
  16.                 $temp_obj->id_commenti = $cmt->id_commenti;
  17.                 $temp_obj->commento = $cmt->commento;
  18.                 // $temp_obj->created_at = $cmt->created_at;
  19.                 $temp_obj->subcomment = [];
  20.                 array_push($array_commenti, $temp_obj);
  21.             }
  22.             else {
  23.                 $temp_obj = new \stdClass();
  24.                 $temp_obj->id = $cmt->id;
  25.                 $temp_obj->username = $cmt->username;
  26.                 $temp_obj->id_commenti = $cmt->id_commenti;
  27.                 $temp_obj->commento = $cmt->commento;
  28.                 // $temp_obj->created_at = $cmt->created_at;
  29.                 $temp_obj->subcomment = [];
  30.                 $temp_obj->bool = false;
  31.                 array_push($array_subcommenti, $temp_obj);
  32.             }
  33.         }
  34.  
  35.         foreach ($array_subcommenti as $cmt) {
  36.            
  37.             if($cmt->bool == false){
  38.                 $temp_obj = new \stdClass();
  39.                 $temp_obj->id = $cmt->id;
  40.                 $temp_obj->username = $cmt->username;
  41.                 $temp_obj->id_commenti = $cmt->id_commenti;
  42.                 $temp_obj->commento = $cmt->commento;
  43.                 // $temp_obj->created_at = $cmt->created_at;
  44.                 $temp_obj->subcomment = [];
  45.  
  46.                 foreach ($array_commenti as $ar_cmt) {
  47.                    
  48.                     if($ar_cmt->id == $temp_obj->id_commenti){
  49.                         array_push($ar_cmt->subcomment, $temp_obj);
  50.                         $cmt->bool = true;
  51.                         break;
  52.                     }
  53.  
  54.                 }
  55.             }
  56.  
  57.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement