【发布时间】:2018-09-09 01:03:03
【问题描述】:
我想在下面的 json 数据中循环。 console.log 可以工作,但不能在 javascript 函数中工作。 我希望 for for 循环在 javascript 函数中工作。 我希望 javascript 函数中的结果像
<script>
(function() {
var questions = [ {
question: "What is the value of 2 * 4";
choices :[4, 8, 99, 11];
correctAnswer: 1;},
{
question: "What is the value of 2 * 8";
choices :[4, 8, 16, 11];
correctAnswer: 2;},
];
</script>
但是在控制台中出现错误。 控制台错误“预期的表达式,得到关键字'for'”这些是我的代码
<?php
$json_codes = array();
$sql = "SELECT id, instructions, quiz_question, correct, wrong, wrong1, wrong2 FROM student_quiz WHERE subject ='SOCIAL STUDIES' AND type = 'challenge' ";
$results = $pdo->query($sql);
$results->setFetchMode(PDO::FETCH_ASSOC);
while($row = $results->fetch()) {
$json_array['instructions'] = $row['instructions'];
$json_array['quiz_question'] = $row['quiz_question'];
$json_array['correct'] = $row['correct'];
$json_array['wrong'] = $row['wrong'];
$json_array['wrong1'] = $row['wrong1'];
$json_array['wrong2'] = $row['wrong2'];
array_push($json_codes,$json_array);
}
echo json_encode($json_codes);
?>
<script>
var obj = <?php echo json_encode($json_codes);?>;
(function() {
var questions = [
for(a=0; a<obj.length; a++){
document.write(obj[a]);
}
];
【问题讨论】:
-
你需要说出你期望它做什么以及它正在做什么。
标签: javascript php mysql json