PHPを使用してmySQLデータからオブジェクトのインスタンスの変数を伝播するにはどうすればよいですか? 疑似コードでの私のオブジェクトは次のとおりです。
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
}
対応するクラスは次のとおりです(構文が間違っている可能性が高いです。私はphpが初めてです):
class ExamClass
{
// property declaration
public $questions = 'a default value';
public $categoryID = 'a default value';
}
class QuestionClass
{
// property declaration
public $questionID = 'a default value';
public $questionTest = 'a default value';
public $categoryID = 'a default value';
public $correctAnswerID = 'a default value';
public $chosenAnswerID = 'a default value';
public $answers = 'a default value';
}
class AnswersClass
{
// property declaration
public $answerID = 'a default value';
public $answerText = 'a default value';
public $isTrue = 'a default value';
}
データベースからデータを抽出する php コードは次のとおりです。
<html>
<body>
<?php
/*
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
}
*/
class ExamClass
{
// property declaration
public $questions = 'a default value';
public $categoryID = 'a default value';
}
class QuestionClass
{
// property declaration
public $questionID = 'a default value';
public $questionTest = 'a default value';
public $categoryID = 'a default value';
public $correctAnswerID = 'a default value';
public $chosenAnswerID = 'a default value';
public $answers = 'a default value';
}
class AnswersClass
{
// property declaration
public $answerID = 'a default value';
public $answerText = 'a default value';
public $isTrue = 'a default value';
}
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`
AND `questions`.`categoryID` = 2");
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>
現在抽出されているデータは次のようになります。
[
{
"questionID": "2",
"questionText": "question text 2",
"categoryID": "2",
"answerID": "1",
"answerText": "answer text 1",
"isTrue": "0"
},
{
"questionID": "2",
"questionText": "question text 2",
"categoryID": "2",
"answerID": "2",
"answerText": "answer text 2",
"isTrue": "1"
},
{
"questionID": "2",
"questionText": "question text 2",
"categoryID": "2",
"answerID": "3",
"answerText": "answer text 3",
"isTrue": "0"
},
{
"questionID": "2",
"questionText": "question text 2",
"categoryID": "2",
"answerID": "4",
"answerText": "answer text 4",
"isTrue": "0"
}
]