为了账号安全,请及时绑定邮箱和手机立即绑定

一对多菲律宾比索

一对多菲律宾比索

PHP
撒科打诨 2022-09-17 17:44:05
我有2张桌子。1个表格问题,1个表格答案。我需要4个答案中的1个问题。因为我为学生创建了一个Web应用程序。我想要这个 JSON[  {    "id": "1",     "question": "Türkiye'nin Başkenti Neresidir?",      "answers": [        {         "id": "1",          "answer": "Ankara",          "query": "1"        },        {         "id": "2",          "answer": "Istanbul",          "query": "0"        }      ]   }],邮差[    {        "id": "1",        "question": "Türkiye'nin Başkenti Neresidir?",        "answers": {            "id": "1",            "answer": "Ankara",            "query": "1"        }    },    {        "id": "1",        "question": "Türkiye'nin Başkenti Neresidir?",        "answers": {            "id": "2",            "answer": "İstanbul",            "query": "0"        }    },]断续器代码public function GetQuestions($AppID){          $QuestionArray = [];          $Select = $this -> DBConnect -> prepare("SELECT questions.id, questions.question, answers.id AS answerid, answers.answer AS answer,answers.query AS query FROM questions INNER JOIN answers ON questions.id = answers.questionid WHERE active = 1 AND appid = ".$AppID);            $Select -> execute();            $Questions = $Select -> fetchAll(PDO::FETCH_ASSOC);             foreach($Questions as $Item){              $QuestionArray[] = Array(               'id' => $Item['id'],               'question' => $Item['question'],               'answers' => Array(                  'id' => $Item['answerid'],                  'answer' => $Item['answer'],                   'query' => $Item['query']                  )               );           }        return $JSON = json_encode($QuestionArray, JSON_UNESCAPED_UNICODE);    }
查看完整描述

2 回答

?
达令说

TA贡献1821条经验 获得超6个赞

请尝试此过程。


public function GetQuestions($AppID)

{

    $QuestionArray = [];

    $Select = $this->DBConnect->prepare('SELECT questions.id, questions.question, answers.id AS answerid, answers.answer AS answer,answers.query AS query FROM questions INNER JOIN answers ON questions.id = answers.questionid WHERE active = 1 AND appid = '.$AppID);

    $Select->execute();

    $Questions = $Select->fetchAll(PDO::FETCH_ASSOC);

    foreach ($Questions as $Item) {

        if (!array_key_exists($Item['id'], $QuestionArray)) {

            $QuestionArray[$Item['id']] = array(

            'id' => $Item['id'],

            'question' => $Item['question'],

            'answers' => array(),

           );

        }

        $QuestionArray[$Item['id']]['answers'][] = array(

         'id' => $Item['answerid'],

         'answer' => $Item['answer'],

         'query' => $Item['query'],

      );

    }


    return $JSON = json_encode($QuestionArray, JSON_UNESCAPED_UNICODE);

}


查看完整回答
反对 回复 2022-09-17
?
慕码人2483693

TA贡献1860条经验 获得超9个赞

这是我的想法,你可以参考和更新你的案例。


获取问题


public function GetQuestions($appID) {

   // TODO 

   // $quest_result = Get form DB;

   foreach ($quest_result as $quest) {

        $quest_response[id]      = $quest[id]; 

        $quest_response[quest]   = $quest[question]; 

        $quest_response[answers] = getAnswerByQuestId ($quest[id]);

   }

   return $quest_response;

}

GetAnswerByQuestId


public function getAnswerByQuestId(questionID) {

    // TODO 

    // $array_answer = Get form DB;


    return $array_answer;


    // ex: 

    // return array_answer[

    //  "id": "1",

    //  "answer": "Ankara",

    // "query": "1"

    //] 

}


查看完整回答
反对 回复 2022-09-17
  • 2 回答
  • 0 关注
  • 154 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号