0
Link to github project印刷MySQL的數據文件EJS與外鍵
我試圖從MySQL得到答案的問答題和節點/ Express應用程序使用EJS模板打印的形式。我無法弄清楚如何打印相應問題的正確答案。
我想數據看起來像這樣在瀏覽器:
問題1個#ID2
- 答案1#ID6
- 答案2#ID7
- 答案3#ID8
問題2#ID3
- 答案1#ID9
- 答案2#ID10
- 答案3#ID11
的數據看起來像這樣:
loadQuizes;
[ { quizId: 2,
quizName: 'Bergskedjor',
dateCreated: '2017-03-03T15:14:02.000Z',
dateFinished: '2017-03-02T23:00:00.000Z',
times: 2,
score: 20 } ]
quizQuestions;
[ { questionId: 2,
question: 'Vilket ├ñr v├ñrldens h├Âgsta berg?',
questionQuizid: 2 },
{ questionId: 3,
question: 'Vilket ├ñr v├ñrldens tredje h├Âgsta berg?',
questionQuizid: 2 } ]
answers;
[ { answerId: 6,
answer: 'Question 1 Answer 1',
correct: 0,
answerQuestionid: 2 },
{ answerId: 7,
answer: 'Question 1 Answer 2',
correct: 1,
answerQuestionid: 2 },
{ answerId: 8,
answer: 'Question 1 Answer 3',
correct: 0,
answerQuestionid: 2 },
{ answerId: 9,
answer: 'Question 2 Answer 1',
correct: 0,
answerQuestionid: 3 },
{ answerId: 10,
answer: 'Question 2 Answer 2',
correct: 0,
answerQuestionid: 3 },
{ answerId: 11,
answer: 'Question 2 Answer 3',
correct: 1,
answerQuestionid: 3 } ]
從表到達:
mysql> SELECT * FROM quiz;
+--------+------------------------+---------------------+--------------+-------+-------+
| quizId | quizName | dateCreated | dateFinished | times | score |
+--------+------------------------+---------------------+--------------+-------+-------+
| 1 | Solution to everything | 2017-03-03 16:14:02 | 2017-03-03 | 2 | 20 |
| 2 | Bergskedjor | 2017-03-03 16:14:02 | 2017-03-03 | 2 | 20 |
+--------+------------------------+---------------------+--------------+-------+-------+
mysql> SELECT * FROM question;
+------------+-------------------------------------------+----------------+
| questionId | question | questionQuizid |
+------------+-------------------------------------------+----------------+
| 1 | What color is the Sky? | 1 |
| 2 | Vilket ├ñr v├ñrldens h├Âgsta berg? | 2 |
| 3 | Vilket ├ñr v├ñrldens tredje h├Âgsta berg? | 2 |
+------------+-------------------------------------------+----------------+
mysql> SELECT * FROM answers;
+----------+---------------------+---------+------------------+
| answerId | answer | correct | answerQuestionid |
+----------+---------------------+---------+------------------+
| 1 | Red | 0 | 1 |
| 2 | Green | 0 | 1 |
| 3 | Blue | 1 | 1 |
| 4 | Pink | 0 | 1 |
| 5 | Red | 0 | 1 |
| 6 | Question 1 Answer 1 | 0 | 2 |
| 7 | Question 1 Answer 2 | 1 | 2 |
| 8 | Question 1 Answer 3 | 0 | 2 |
| 9 | Question 2 Answer 1 | 0 | 3 |
| 10 | Question 2 Answer 2 | 0 | 3 |
| 11 | Question 2 Answer 3 | 1 | 3 |
+----------+---------------------+---------+------------------+
這是我迄今爲止的解決方案:
<h1>
<% loadQuizes.forEach(function (quizes) { %>
<h2><%= quizes.quizName %></h2>
<% }) %>
</h1>
<form action="/takequiz" method="POST">
<% quizQuestions.forEach(function (questions) { %>
<div class="well well-sm">
<h3><%= questions.question %></h3>
<% answers.forEach(function (ans) { %>
<%= ans.answer %><br>
<% }) %>
</div> <!-- well -->
<% }) %>
</form>
但是,這給了我:
Question 1 #id2
- answer 1 #id6
- answer 2 #id7
- answer 3 #id8
- answer 1 #id9
- answer 2 #id10
- answer 3 #id11
Question 2 #id3
- answer 1 #id6
- answer 2 #id7
- answer 3 #id8
- answer 1 #id9
- answer 2 #id10
- answer 3 #id11
是的,我在想如何實現邏輯,但我自己的邏輯是累哈哈。 這工作!你先生是最棒的!謝謝 –