如何使用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"
}
]
答案 0 :(得分:1)
从您的评论中,您似乎在询问如何使用数据库水合这些对象。而不是从数据库,到JSON,到一个对象,看看mysqli_fetch_object
。
使用mysqli_fetch_object()
,您可以使用查询中的数据指定要初始化的类。在你的情况下:
$result = mysqli_query('SELECT * FROM Answers;');
while ($answer = mysqli_fetch_object($result, 'AnswersClass')) {
var_dump($answer);
}
注意:我今天已经回答了您的其他一个问题,看起来您似乎已经开始使用其中一些内容了。我建议您阅读Data Mapper和Active Record模式。
答案 1 :(得分:0)
这可以为您提供有关如何将数据源映射到类的一些想法。
class Exam
{
// property declaration
public $questions = array();
public $categoryID;
}
class Question
{
// property declaration
public $questionID;
public $questionTest;
public $categoryID;
public $correctAnswerID;
public $chosenAnswerID;
public $answers = array();
}
class Answer
{
// property declaration
public $answerID;
public $answerText;
public $isTrue;
}
class ExamBuilder{
public function buildExam($data){
$e = new Exam();
$e->categoryID = $data->categoryID;
$this->addQuestions($data->questions, $e);
return $e;
}
protected function addQuestions ($questions, Exam $exam){
foreach ($questions as $question){
$q = new Question();
$q->questionID = $question->questionID;
$q->questionTest = $question->questionTest;
$q->categoryID = $question->categoryID;
$q->correctAnswerID = $question->correctAnswerID;
$q->chosenAnswerID = $question->chosenAnswerID;
$this->addAnswers($question->answers, $q);
$exam->questions[] = $q;
}
}
protected function addAnswer($answer, Question $question){
$a = new Answer();
$a->answerID = $a->anwerID;
$a->answerText = $answer->answerText;
$a->isTrue = $answer->isTrue;
}
}
使用
$builder = new ExamBuilder();
$exam = $builder->buildExam($data);