我很确定我将几个任务混合在一起,但请耐心等待,我是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>
答案 0 :(得分:0)
在实际编码之前更改对象。
如果您从第三方来源收到JSON(以防万一,这与当前情况无关),您需要解析它,修改它然后重新编码。
远比在JSON上尝试字符串解析更有效...
答案 1 :(得分: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;
?>