php 格式化来自MYSQL的响应

cyvaqqii  于 2022-12-21  发布在  PHP
关注(0)|答案(1)|浏览(135)

我使用codeigniter 3创建了一个测验应用程序,它将从MySQL中的3个表(quiz_table,question_table和answer_table)中获取数据,以显示4个测验问题及其答案。

function getSingleQuizQuestionDataFromDB($quizId)
{        //insert query
    try {
        $this->db->select('answer_table.quizId');
        $this->db->select('answer_table.questionId');
        $this->db->select('question_table.questionTitle');
        $this->db->select('question_table.correctAnswer');
        $this->db->select('answer_table.answerId');
        $this->db->select('answer_table.answer');
        $this->db->from('answer_table');
        $this->db->where('answer_table.quizId',$quizId);
        $this->db->join('question_table','answer_table.questionId= question_table.questionId','LEFT');
     
        //$this->db->group_by(['answer_table.quizId', 'answer_table.questionId']);
        $result = $this->db->get();

        $singleQuizQuestionData= $result->result_array();
        return $singleQuizQuestionData;
    } catch (Exception $e) {
        // log_message('error: ',$e->getMessage());
        return;
    }
}

下面是我得到的回应,

{
 "singleQuizQuestionData": [
  {
     "quizId": "68",
     "questionId": "76",
     "questionTitle": "q1q1",
     "correctAnswer": "q1a",
     "answerId": "269",
     "answer": "q1q1a1"
  },
  {
     "quizId": "68",
     "questionId": "76",
     "questionTitle": "q1q1",
     "correctAnswer": "q1a",
     "answerId": "270",
     "answer": "q1q1a2"
  },
  {
     "quizId": "68",
     "questionId": "76",
     "questionTitle": "q1q1",
     "correctAnswer": "q1a",
     "answerId": "271",
     "answer": "q1q1a3"
  },
  {
     "quizId": "68",
     "questionId": "76",
     "questionTitle": "q1q1",
     "correctAnswer": "q1a",
     "answerId": "272",
     "answer": "q1q1a4"
  },
  {
     "quizId": "68",
     "questionId": "77",
     "questionTitle": "q1q2",
     "correctAnswer": "q1a",
     "answerId": "273",
     "answer": "q1q2a1"
  },
  {
     "quizId": "68",
     "questionId": "77",
     "questionTitle": "q1q2",
     "correctAnswer": "q1a",
     "answerId": "274",
     "answer": "q1q2a2"
  },
  {
     "quizId": "68",
     "questionId": "77",
     "questionTitle": "q1q2",
     "correctAnswer": "q1a",
     "answerId": "275",
     "answer": "q1q2a3"
  },
  {
     "quizId": "68",
     "questionId": "77",
     "questionTitle": "q1q2",
     "correctAnswer": "q1a",
     "answerId": "276",
     "answer": "q1q2a4"
  },
  {
     "quizId": "68",
     "questionId": "78",
     "questionTitle": "q1q3",
     "correctAnswer": "q1a",
     "answerId": "277",
     "answer": "q1q3a1"
  },
  {
     "quizId": "68",
     "questionId": "78",
     "questionTitle": "q1q3",
     "correctAnswer": "q1a",
     "answerId": "278",
     "answer": "q1q3a2"
  },
  {
     "quizId": "68",
     "questionId": "78",
     "questionTitle": "q1q3",
     "correctAnswer": "q1a",
     "answerId": "279",
     "answer": "q1q3a3"
  },
  {
     "quizId": "68",
     "questionId": "78",
     "questionTitle": "q1q3",
     "correctAnswer": "q1a",
     "answerId": "280",
     "answer": "q1q3a4"
  },
  {
     "quizId": "68",
     "questionId": "79",
     "questionTitle": "q1q4",
     "correctAnswer": "q1a",
     "answerId": "281",
     "answer": "q1q4a1"
  },
  {
     "quizId": "68",
     "questionId": "79",
     "questionTitle": "q1q4",
     "correctAnswer": "q1a",
     "answerId": "282",
     "answer": "q1q4a2"
  },
  {
     "quizId": "68",
     "questionId": "79",
     "questionTitle": "q1q4",
     "correctAnswer": "q1a",
     "answerId": "283",
     "answer": "q1q4a3"
  },
  {
     "quizId": "68",
     "questionId": "79",
     "questionTitle": "q1q4",
     "correctAnswer": "q1a",
     "answerId": "284",
     "answer": "q1q4a4"
  }
 ]
}

我得到的回答是好的,但是否有可能得到共同的,使它像这样,(这是显示只有问题1,但应该有所有4个问题)

{
 "singleQuizQuestionData": [
   "quizId": "68",
     "questionId": "76",
     "questionTitle": "q1q1",
     "correctAnswer": "q1a",
  "answers":[
  {
    "answerId": "269","answer": "q1q1a1",
  },
  {
    "answerId": "270","answer": "q1q1a3",
  },
  {
    "answerId": "271","answer": "q1q1a3",
  },
  {
    "answerId": "272","answer": "q1q1a4",
  },
],
]}
1sbrub3j

1sbrub3j1#

最简单的方法是先从questions表中只获取问题,然后循环遍历结果集,从answers表中获取每个问题的答案,并将这些答案添加到相应问题下的answers数组中:

$this->db->select('quizId');
$this->db->select('questionId');
$this->db->select('questionTitle');
$this->db->select('correctAnswer');
$this->db->from('question_table');
$this->db->where('quizId',$quizId);
$result = $this->db->get()->result_array();

foreach ($result as $key => $row) {
    $this->db->select('answerId');
    $this->db->select('answer');
    $this->db->from('answer_table');
    $this->db->where('questionId',$row['questionId']);
    $result[$key]['answers'] = $this->db->get()->result_array();
}

return $result;

相关问题