patternphpMinor
PHP, MySQL quiz application
Viewed 0 times
phpmysqlquizapplication
Problem
I'm working on a quiz application for training purposes and I think I'm done with most of it.
However, I'm not comfortable with what I've done: it seems to be too complicated and unprofessional, and I would like to make it more organized and simple to understand.
first, the database contains a three simple tables:
quiz table:
id | name
questions table:
id | question | quiz_id (FK from quiz table) |
answers table:
id | name | questions_id (FK from questions table) | state (right or wrong)
1. the first task is a page to show all available quizzes:
the data part
the controller part
the view part to display data
the data part
controller part
```
// get single quiz
public function getquiz($id) {
// get the quiz
$quizes = $
However, I'm not comfortable with what I've done: it seems to be too complicated and unprofessional, and I would like to make it more organized and simple to understand.
first, the database contains a three simple tables:
quiz table:
id | name
questions table:
id | question | quiz_id (FK from quiz table) |
answers table:
id | name | questions_id (FK from questions table) | state (right or wrong)
1. the first task is a page to show all available quizzes:
the data part
public function getQuizes() {
$stmt = $this->dbconn->prepare("SELECT id, name FROM {$this->table}");
$stmt->execute();
return $stmt->fetchAll();
}the controller part
// get all quizes
$quizes = $this->quizModel->getQuizes();
// pass the data to the view
$this->callView('quiz/index',$quizes);the view part to display data
Available Quizes:
">
- the second task is a page to show quiz and it's questions and possible answers
the data part
public function getQuizById($id) {
$results = [];
$stmt = $this->dbconn->prepare(
"SELECT DISTINCT quiz.name quizName, questions.question question, answers.name answer, answers.id answerId
FROM quiz
inner Join questions
ON quiz.id = questions.quiz_id
inner join answers
on questions.id = answers.question_id
where quiz.id = :id"
);
$stmt->bindParam(':id', $id);
$stmt->execute();
// group the information
while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ){
$results[$row['quizName']][$row['question']][] = [$row['answerId'], $row['answer']];
}
return $results;
}controller part
```
// get single quiz
public function getquiz($id) {
// get the quiz
$quizes = $
Solution
Structure
I'm not comfortable with what I've done: it seems to be complicated and unprofessional
Honestly, you shouldn't be. Structurally, this seems far better than the average PHP code.
You use an MVC structure as is typical in web development, and you do it correctly. Neither the view nor the controller are doing things that they shouldn't be doing.
Model
Your models are currently really data access objects. You might consider adding actual models with fields and getters.
That way, your controller and view would be decoupled from the database and you would have self-documented objects that you pass around, which would also avoid accessing some magic array where you are not sure what it actually contains (eg
View
Your views are mostly well structured.
But you shouldn't include header and footer in the
Misc
I'm not comfortable with what I've done: it seems to be complicated and unprofessional
Honestly, you shouldn't be. Structurally, this seems far better than the average PHP code.
You use an MVC structure as is typical in web development, and you do it correctly. Neither the view nor the controller are doing things that they shouldn't be doing.
Model
Your models are currently really data access objects. You might consider adding actual models with fields and getters.
That way, your controller and view would be decoupled from the database and you would have self-documented objects that you pass around, which would also avoid accessing some magic array where you are not sure what it actually contains (eg
$answer[0]). View
Your views are mostly well structured.
But you shouldn't include header and footer in the
index, single, etc views, as it makes them not reusable. You may for example need to display a single question in multiple different contexts.Misc
- Your function names are not consistently using camelCase.
- You sometimes hardcode the
quiztablename, and sometimes use a variable, which is confusing and can lead to bugs in case you actually do decide to change the table name.
- Your variable names are sometimes not consistent and thus confusing. For example, what's a
record?
- upper-case all your SQL keywords, not just some of them.
- some of your comments are not adding any information, eg
get the quizorpass the data to the view.
- You should HTML encode your output to avoid XSS (not everyone who is allowed to add questions or answers should necessarily be allowed to execute JavaSCript).
- [you didn't post the code, but your database connect code is not ideal. For one, you shouldn't just die in a class as the calling code can't recover from that. Secondly, your models currently extend your database, but models aren't a kind of database. The database should be injected into the class. And finally, you open a new connection for each model]
Context
StackExchange Code Review Q#147648, answer score: 5
Revisions (0)
No revisions yet.