簡體   English   中英

MySQL下拉列表填充的PHP頁面

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM