PHP页面由MySQL下拉列表填充

时间:2014-01-01 22:44:58

标签: php sql

我正在尝试编写一个脚本来显示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'];

1 个答案:

答案 0 :(得分:0)

我认为下面的建议会让你的下拉列表工作......要根据下拉列表动态地提取信息,需要一个表单,一个提交按钮和一些$ _POST变量的解析。

一次一步,让你的下拉菜单工作。

首先,不要忘记在{$ 1}之后关闭您的选项。$ thing:

</option>

$options.="<OPTION VALUE=\"$id\">".$thing."</OPTION>";

此外,您只需选择名称,如果您还需要选择ID。

<OPTION VALUE=0>Choose the course</OPTION>

最后,要将查询结果用作关联数组,需要mysql_fetch_assoc

$sql="SELECT name, id FROM mdl_scorm"; 

最后一次可能是最让你绊倒的。

希望有所帮助。