[英]PHP Page populated by MySQL dropdowns
我试图编写一个脚本来显示来自SQL数据库的记录,但是基于三个变量,这些变量由页面上的三个下拉框设置,这些变量是从数据库自动填充的。
这是我正在研究的学习管理系统,用于提供在线学习测试的有效反馈。
我当前正在使用的代码以及伪代码(希望通过它解释我的要求)在下面。 我遇到的问题是我无法根据在伪代码中设置的条件来填充下拉框。
任何帮助都非常感谢,谢谢
约翰
// I used this article for the structure of the following script:
http://forums.devarticles.com/mysql-development-50/drop-down-menu-populated-from-a-mysql-database-1811.html
// Dropdown Box 1 - Choose the course - Show entries from the column "Name" from table "mdl_scorm". Once an option has been selected set variable $coursechoice to the value in the "id" column of the "mdl_scorm" table
// Dropdown Box 2 - Choose the user - Show entries from the columns "firstname" + "lastname" from table "mdl_user" IF the number shown in the "id" column of table "mdl_user" is present in the "userid" column of table "mdl_scorm_scoes_track" AND IF $coursechoice is present in the "scormid" column of table "mdl_scorm_scoes_track". Once an option has been selected set variable $userchoice to the value in the "id" column of table "mdl_user"
// Dropdown Box 3 - Choose the attempt - Show entries from the column "attempt" from table "mdl_scorm_scoes_track" IF $coursechoice is present in the "scormid" column of the table "mdl_scorm_scoes_track" AND IF $userchoice is present in the "userid" column of table "mdl_scorm_scoes_track". Once an option has been selected set variable $attemptchoice to the value in the "attempt" column from table "mdl_scorm_scoes_track"
// Submit button displays the records from table "mdl_scorm_scoes_track" which have a value in the column "scormid" which matches $coursechoice AND have a value in the column "userid" which matches $userchoice AND have a value in the column "attempt" which matches $attemptchoice
$sql="SELECT name FROM mdl_scorm";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$thing=$row["name"];
$options.="<OPTION VALUE=\"$id\">".$thing;
}
?>
<SELECT NAME=course>
<OPTION VALUE=0>Choose the course
<?=$options?>
</SELECT>
<?php
$sql="SELECT username FROM mdl_user";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$thing=$row["name"];
$options.="<OPTION VALUE=\"$id\">".$thing;
}
?>
<SELECT NAME=user>
<OPTION VALUE=0>Choose the user
<?=$options?>
</SELECT>
<?php
$sql="SELECT attempt FROM mdl_scorm_scoes_track";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$thing=$row["name"];
$options.="<OPTION VALUE=\"$id\">".$thing;
}
?>
<SELECT NAME=attempt>
<OPTION VALUE=0>Choose the attempt
<?=$options?>
</SELECT>
<?php
$finalresult = SELECT element, value FROM mdl_scorm_scoes_track WHERE scormid=$coursechoice AND userid=$userchoice AND attempt=$attemptchoice
while ($testrows = mysqli_fetch_array($finalresult)){
echo $testrows['value'];
我认为下面的建议将使您的下拉菜单起作用...要实际基于下拉列表动态提取信息,将需要一个表单,一个提交按钮和$ _POST变量的一些解析。
一次仅一步,让您的下拉列表正常工作。
首先,不要忘记在$ .thing之后用</option>
关闭选项:
$options.="<OPTION VALUE=\"$id\">".$thing."</OPTION>";
和
<OPTION VALUE=0>Choose the course</OPTION>
此外,您仅选择名称,如果您想要ID,也需要选择该名称。
$sql="SELECT name, id FROM mdl_scorm";
最后,要将查询结果用作关联数组,您需要mysql_fetch_assoc
while ($row=mysql_fetch_assoc($result)) {
最后可能是最让您绊倒的。
希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.