简体   繁体   English

PHP - 如何使用 php 和 mysql 在 for 循环中更新数组对象

[英]PHP - How to update array object in for loop using php and mysql

I try to get a json form two different table in php/mysql.我尝试在 php/mysql 中从两个不同的表中获取一个 json。 Purpose is to print JSON out for a rest API using php/mysql.目的是使用 php/mysql 为 rest API 打印 JSON。

Expected json is:预期的 json 是:

{
   "qid":"1",
   "qst":"OK",
   "qoption:
     [
      {"id":"o1","isrt":true},
      {"id":"o2","isrt":false},
      {"id":"o3","isrt":false},
      {"id":"o4","isrt":false}
     ]
},
{
   "qid":"2",
   "qst":"OK",
   "qoption:
     [
      {"id":"o1","isrt":flase},
      {"id":"o2","isrt":false},
      {"id":"o3","isrt":true},
      {"id":"o4","isrt":false}
     ]
}

What I had try with PHP:我在 PHP 上的尝试:

if ( isset($_GET['examsid']) && $_GET['examsid'] != "") {

    $questions = array();
    $conn = dbConnection();
    $examsid = $_GET['examsid'];

    $result = mysqli_query($conn, "SELECT `id`,`exams_id`,`title` FROM `tblquestions` WHERE exams_id='".$examsid."' ORDER BY RAND()");

    if(mysqli_num_rows($result) > 0){
       while($q = mysqli_fetch_array($result)){
           $questions[] = $q;
       }
    }

    foreach ($questions as $q) {
       $options = array();
       $oquery = mysqli_query($conn, "SELECT `id`,`question_id`,`title`,`iscorrect` FROM `tbloptions` WHERE `question_id`='".$q['id']."' ORDER BY RAND()");
       while($o = mysqli_fetch_array($oquery)){
         $options[] = $o;
       }
       array_push($q['options'], $options);
    }


    print_r($questions);
}

Current Output using json_encode($jsonobj):使用 json_encode($jsonobj) 的当前输出:

    {
      "0": "1",
      "1": "1",
      "2": "How many bones comprise the adult human skeleton?",
      "3": "2019-04-11 11:18:44",
      "4": "0000-00-00 00:00:00",
      "id": "1",
      "exams_id": "1",
      "title": "How many bones comprise the adult human skeleton?",
      "c_date": "2019-04-11 11:18:44",
      "m_date": "0000-00-00 00:00:00"
    },
    {
      "0": "3",
      "1": "1",
      "2": "Which of the following is the first calculating device?",
      "3": "2019-04-11 11:19:56",
      "4": "0000-00-00 00:00:00",
      "id": "3",
      "exams_id": "1",
      "title": "Which of the following is the first calculating device?",
      "c_date": "2019-04-11 11:19:56",
      "m_date": "0000-00-00 00:00:00"
    }

MySQL: Questions- MySQL:问题-

CREATE TABLE `tblquestions` (
  `id` int(11) NOT NULL,
  `exams_id` varchar(100) CHARACTER SET latin1 NOT NULL,
  `title` varchar(500) CHARACTER SET latin1 NOT NULL,
  `c_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `m_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

MySQL: Options - MySQL:选项 -

CREATE TABLE `tbloptions` (
  `id` int(11) NOT NULL,
  `question_id` int(100) NOT NULL,
  `title` varchar(300) NOT NULL,
  `iscorrect` varchar(100) CHARACTER SET latin1 NOT NULL,
  `c_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `m_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Seems its returns only question no options also pushed title twice on each questions.似乎它只返回问题没有选项也会在每个问题上两次推送标题。 Please any one help me to resolve this.请任何人帮我解决这个问题。

You are adding the options to the $q variable that is only visible inside the second foreach loop.您正在向$q变量添加选项,该变量仅在第二个 foreach 循环内可见。 Use your $questions array instead.请改用您的$questions数组。 Furthermore, you need to get the current index of your question to add by adding $key => $q in your foreach head.此外,您需要通过在 foreach 头中添加$key => $q来获取要添加的问题的当前索引。 Can you tell me what the output of this code is (I changed everything mentioned before)?你能告诉我这段代码的输出是什么吗(我改变了之前提到的所有内容)?

if ( isset($_GET['examsid']) && $_GET['examsid'] != "") {

    $questions = array();
    $conn = dbConnection();
    $examsid = $_GET['examsid'];

    $result = mysqli_query($conn, "SELECT * FROM `tblquestions` WHERE exams_id='".$examsid."' ORDER BY RAND()");

    if(mysqli_num_rows($result) > 0){
       while($q = mysqli_fetch_array($result)){
           $questions[] = $q;
       }
    }

    foreach ($questions as $key => $q) {
       $options = array();
       $oquery = mysqli_query($conn, "SELECT * FROM `tbloptions` WHERE `question_id`='".$q['id']."' ORDER BY RAND()");
       while($o = mysqli_fetch_array($oquery)){
         $options[] = $o;
       }
       $questions[$key]['options'] = $options;
    }


    print_r($questions);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM