public function question($q_id) { $user = new User(); if (Auth::check()) { $user_data = $user->get_user_profile_data(); //id , username and user rank } else { $user_data['id'] = ""; } // Get question Data $question = new Questions(); $question_item = $question->get_question_by_id($q_id); $q_data['q_id'] = $question_item[0]['id']; $q_data['q_content'] = $question_item[0]['content']; $q_data['user_id'] = $question_item[0]['user_id']; // get all answer of givern question ID $answers = new Answers(); $get_all_answers = $answers->get_answers_ids_by_question_id($q_data['q_id']); $get_all_answers_ids = array(); $get_all_users_ids = array(); $get_all = array(); $answers_status = new Answers_status(); if ($get_all_answers != NULL) { // make array of all answer's IDs foreach ($get_all_answers as $answer) { array_push($get_all_answers_ids, $answer['id']); array_push($get_all_users_ids, $answer['user_id']); } } // get all usernames from user ids in array // $users_data = $user->get_user_names_by_ids($get_all_users_ids); // $username_array = array(); // get all statuses of all answers $gat_status_in_array = $answers_status->gat_status_in_array($get_all_answers_ids); $status_array = array(); if ($gat_status_in_array != null) { foreach ($gat_status_in_array as $key => $item) { $status_array[$item['answer_id']][$key] = $item; } ksort($status_array, SORT_NUMERIC); } if ($get_all_answers != NULL) { foreach ($get_all_answers as $answer) { $like_count = 0; $dislike_count = 0; $spam_count = 0; if ($status_array != null) { foreach ($gat_status_in_array as $status) { if ($answer['id'] == $status['answer_id']) { if ($status['status'] == 1) { $like_count++; } elseif ($status['status'] == 0) { $dislike_count++; } elseif ($status['status'] == 2) { $spam_count++; } } } } // get user name $username = $user->get_user_name_by_id($answer['user_id']); $all_array = array('answer_data' => $answer->toArray(), 'likes' => $like_count, 'dislikes' => $dislike_count, 'spam' => $spam_count, 'user_name' => $username); array_push($get_all, $all_array); } } return view('user/question_data')->withQuestionId($q_data['q_id'])->withQuestionUserId($q_data['user_id'])->withQuestionContent($q_data['q_content'])->withUserId($user_data['id'])->withAnswersArray($get_all); }