好吧,我正在开发一个学习网站,在过去的几天里遇到了一个我无法克服的大障碍,尽管尝试解决这个问题让我克服了其他一些障碍,我'我现在卡住了...
如果有人知道任何教程或任何非常有帮助的东西,我发现很难找到教程来帮助我,我可以找到零碎的东西,但没有任何东西能够帮助我找到解决方案。
我想要发生的事情: 当用户进入类(class)页面时,它会从问题列表中提取 5 个项目,并一次将它们呈现给用户 1 个,当他们按 Enter 键时,当他们完成 5 个项目后,系统会将他们带到下一个项目它将离开页面。
有什么作用: 使用 MySQL 提取 5 个问题 ID
我需要什么: 将 5 个问题 ID 放入 JavaScript 箭头中 根据第一个数组编号将数据加载到页面中 迭代数组将数据加载到页面上,无需完全刷新
这是我迄今为止拥有的 Lesson.php 页面,减去了我尝试过的一些内容,这些内容只是代码片段,我从 stackoverflow 中找到了这些代码片段,我尝试使用我的设置:
<!doctype html>
<?php
include ("Connections/localhost.php");
//Get up to five questions that user hasn't seen
$sql = "SELECT quest_id FROM questions LEFT OUTER JOIN answers ON questions.quest_id = answers.ans_question WHERE answers.ans_question IS null and questions.quest_level <= (SELECT user_level FROM users WHERE username = '".$_SESSION['username']."' LIMIT 5);";
$qresult = mysql_query($sql);
//Get number of lessons available
$result = mysql_query("SELECT COUNT(*) AS total FROM questions LEFT OUTER JOIN answers ON questions.quest_id = answers.ans_question WHERE answers.ans_question IS null and questions.quest_level <= (SELECT user_level FROM users WHERE username = '".$_SESSION['username']."');");
$les = mysql_fetch_assoc($result);
?>
<html>
<head>
<meta charset="utf-8">
<title>Lesson</title>
<script>
function play_audio() {
document.getElementById('id1').play();
}
var lessons = <?php echo $les['total']; ?>;
var i = 1;
if(lessons > 5) {
lessons = 5
}
function next_question() {
if(i<lessons) { //if there are still reviews to do
//call webpage to get new information
i++;
}
else {
//Load finished page
}
}
</script>
<link href="review.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="text_display">
<p><?php echo "Question Appears Here" ?></p>
</div>
<div id="looking">
What is the <?php echo "Meaning?" ?>
</div>
<div id="input_bar">
<form>
<input type="text" name="textfield" id="txt_input">
</form>
</div>
<div id="audio">
<form>
<input name="audio" type="button" id="btn_audio" value="Audio" onClick="play_audio()">
</form>
</div>
<div id="info">
<audio id="id1" src="audio/" style="display:none"></audio>
<h2>Information</h2>
<p></p>
</div>
</body>
</html>
我有一个 getitem.php 页面,它将从数据库中提取选定的问题信息,以便我可以从中提取信息并更新类(class)页面,而无需重新加载它。
<?php
include ("Connections/localhost.php");
//Get item from url
$question = $_GET["ans_question"];
//Make sure to get UTF-8
$change = "SET NAMES 'utf8'";
mysql_query($change) or die("The change did not work: $change");
//Pull that question from the database
$sql = mysql_query("SELECT * FROM questions WHERE quest_id = '".$question."'");
$item= mysql_fetch_assoc($sql);
?>
<html>
<meta charset="utf-8">
<body>
<div id="text"><?php echo $item['quest_text']; ?></div>
<div id="type"><?php $qtype = $item['quest_type']; echo $qtype ?></div>
<div id="question"><?php echo $item['quest_question']; ?></div>
<div id="answer"><?php $ans = $item['quest_ans']; echo $ans?></div>
<div id="qaudio"><?php $qaudio = $item['quest_audio']; echo $qaudio?></div>
<div id="qinfomation"><?php echo $item['quest_info'];?></div>
</body>
</html>
最佳答案
使用像 laravel 这样的框架会给你带来很多好处。为了帮助你简化事情,如果你是 php 和 web 开发的新手,那么使用核心 php 编程和 mysql 查询是有意义的。另一方面,Laravel 非常容易学习并且安装简单。话虽如此,您还可以决定将结果集从 mysql 返回为 json 数据到您的网页,并使用 jquery 捕获它。
您应该关注的是如何构建 Ajax 应用程序,为此我强烈建议使用 Jquery library .
关于MySQL 中的 Javascript 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21345181/