0

我很确定我将几个任务混合在一起,但请耐心等待,我是 mySQL 和 php 的新手。

我正在使用json_encode从 mySQL 查询创建 JSON。但在回显之前,我想修改 JSON,以添加更多有价值的数据。目前我的 JSON 看起来像这样:

[
  {
    "question": "1",
    "questionText": "question text 1",
    "categoryID": "1",
    "answerID": "1",
    "answerText": "answer text 1",
    "isTrue": "1"
  },
  {
    "question": "1",
    "questionText": "question text 1",
    "categoryID": "1",
    "answerID": "2",
    "answerText": "answer text 2",
    "isTrue": "0"
  },
  {
    "question": "1",
    "questionText": "question text 1",
    "categoryID": "1",
    "answerID": "3",
    "answerText": "answer text 3",
    "isTrue": "0"
  },
  {
    "question": "1",
    "questionText": "question text 1",
    "categoryID": "1",
    "answerID": "4",
    "answerText": "answer text 4",
    "isTrue": "0"
  },
  {
    "question": "2",
    "questionText": "question text 2",
    "categoryID": "2",
    "answerID": "1",
    "answerText": "answer text 1",
    "isTrue": "0"
  },
  {
    "question": "2",
    "questionText": "question text 2",
    "categoryID": "2",
    "answerID": "2",
    "answerText": "answer text 2",
    "isTrue": "1"
  },
  {
    "question": "2",
    "questionText": "question text 2",
    "categoryID": "2",
    "answerID": "3",
    "answerText": "answer text 3",
    "isTrue": "0"
  },
  {
    "question": "2",
    "questionText": "question text 2",
    "categoryID": "2",
    "answerID": "4",
    "answerText": "answer text 4",
    "isTrue": "0"
  }
]

我希望它看起来像这样(伪代码):

    exam:{
    questions:[
        question:{
            questionID: string
            questionTest: string
            categoryID: string
            correctAnswerID: string
            chosenAnswerID: string
            answers:[
                answer:{
                    answerID = string
                    answerText = string
                    isTrue = bool
                }
                answer:{}
            ]
        }
        question:{}
     ]
     categoryID: string
 }

这是到目前为止的代码:

<html>
<body>

<?php
    header('Content-Type: text/html; charset=utf-8');
    $con=mysqli_connect("localhost","root","root","Theory");
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con,"SELECT `questions`.`questionID` AS questionID,
                           `questions`.`questionText` AS questionText,
                           `questions`.`categoryID` AS categoryID,
                           `answers`.`answerID` AS answerID,
                           `answers`.`answerText` AS answerText,
                           `answers`.`isTrue` AS isTrue
                                 FROM `questions`,`answers`
                                 WHERE `questions`.`questionID` = `answers`.`questionID`");

    if (!$result)
    {
        die('Error: ' . mysqli_error($con));
    }

   $rows = array();
   while($r = mysqli_fetch_assoc($result)) {
        $rows[] = $r;
   }
   print json_encode($rows);

    mysqli_close($con);
    ?>

</body>
</head>
4

2 回答 2

0

在实际编码之前更改您的对象。

如果您从第三方来源收到 JSON(以防万一,这与当前情况无关),您需要对其进行解析、修改然后重新编码。

比尝试对 JSON 进行字符串解析要高效得多……

于 2013-07-16T18:28:45.267 回答
0

我的个人喜好和易用性我会避免将问题和答案表连接在一起。特别是因为无论如何您都需要在 json 中将它们分开。这是一个带有一些评论的重写,以帮助您了解正在发生的事情。注意:代码尚未使用数据库进行测试,因此它可能无法完全正常工作,因为我不知道您的表的结构,但它应该可以帮助您入门。

<?php 

// header information needs to be set before any output or it will error.
header('Content-Type: text/html; charset=utf-8');

?><html>
    <body>

<?php

$con=mysqli_connect("localhost","root","root","Theory");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$questions = array();

// Lets get a list of the questions
$questions_results = mysql_query("SELECT `questions`.`questionID`, `questions`.`questionText`, `questions`.`categoryID` FROM `questions`") or die('Error: ' . mysqli_error($con));
while($question = mysqli_fetch_object($questions_results))
{

    // Now that we have a $question object lets add an answers property and set it to a blank array
    $questions[$question->questionID]->answers = array();

    // Now query for answers table to build on the answers array
    $answers_results = mysql_query("SELECT `answers`.`answerID`, `answers`.`answerText`, `answers`.`isTrue` FROM `answers` where `answers`.`questionID` = '".$question->questionID."'") or die('Error: ' . mysqli_error($con));
    while($answer = mysqli_fetch_object($questions_results))
    {
        $questions[$question->questionID]->answers[] = $answer;
    }

    // Now lets add this nifty new question to the $questions array
    $questions[] = $question;
}
mysqli_close($con);


// Now we can prin the json version of $questions
json_encode($questions);

?>

    </body>
</head>

上面的代码所做的是它构建了一个数组/对象,如果手动构建,它将像这样构建:

        // Question 1 - Notice the 0 key index on the q
        $questions[0] = new stdClass(); // Create a new object
        $questions[0]->questionID = 1;
        $questions[0]->questionText = "Question";
        $questions[0]->categoryID = 1;
        $questions[0]->answers = array(); // Create a new array for answers

            // Answer 1 for question 1 - notice the 0 key index on the answer
            $questions[0]->answers[0] = new stdClass(); // Create a new object for answer 1
            $questions[0]->answers[0]->answerID = 1;
            $questions[0]->answers[0]->answerText = "Answer";
            $questions[0]->answers[0]->isTrue = 1;

            // Answer 2 for question 1 - notice the 1 key index on the answer
            $questions[0]->answers[1] = new stdClass(); // Create a new object for answer 1
            $questions[0]->answers[1]->answerID = 1;
            $questions[0]->answers[1]->answerText = "Answer";
            $questions[0]->answers[1]->isTrue = 1;


        // Question 2 - notice the 1 key index on the questions
        $questions[1] = new stdClass(); // Create a new object
        $questions[1]->questionID = 1;
        $questions[1]->questionText = "Question";
        $questions[1]->categoryID = 1;
        $questions[1]->answers = array(); // Create a new array for answers

            // Answer 1 for question 2
            $questions[1]->answers[0] = new stdClass(); // Create a new object for answer 1
            $questions[1]->answers[0]->answerID = 1;
            $questions[1]->answers[0]->answerText = "Answer";
            $questions[1]->answers[0]->isTrue = 1;
?>
于 2013-07-16T20:36:40.140 回答