0

您好,我是 php 新手,我试图找到一段代码,我可以用它来完成我需要的任务,我目前有一个页面,其中包含一个用于查看课程标准的表单。我还有一个下拉菜单,其中当前包含我存储在数据库中的模块的所有课程代码。我的问题是,当我选择课程代码时,我希望填充表单中的字段以显示有关所选课程的所有信息。我试图开始工作的代码如下:

<?php 
session_start();
?>
<? include ("dbcon.php") ?>
<?php

if(!isset($_GET['coursecode'])){ 
$Var ='%'; 
} 
else 
{ 
if($_GET['coursecode'] == "ALL"){ 
$Var = '%'; 
} else { 
$Var = $_GET['coursecode']; 
} 
}


echo "<form action=\"newq4.php\" method=\"GET\"> 
<table border=0 cellpadding=5 align=left><tr><td><b>Coursecode</b><br>";

$res=mysql_query("SELECT * FROM module GROUP BY mId"); 
if(mysql_num_rows($res)==0){ 
echo "there is no data in table.."; 
} else 
{ 
echo "<select name=\"coursecode\" id=\"coursecode\"><option value=\"ALL\"> ALL </option>"; 
for($i=0;$i<mysql_num_rows($res);$i++) 
{ 
$row=mysql_fetch_assoc($res); 
echo"<option value=$row[coursecode]";

if($Var==$row[coursecode]) 
echo " selected"; 
echo ">$row[coursecode]</option>"; 
}
echo "</select>"; 
}
echo "</td><td align=\"left\"><input type=\"submit\" value=\"SELECT\" /> 

</td></tr></table></form><br>";

$query = "SELECT * FROM module WHERE coursecode LIKE '$Var' "; 
$result = mysql_query($query) or die("Error: " . mysql_error());

if(mysql_num_rows($result) == 0){ 
echo("No modules match your currently selected coursecode. Please try another coursecode!"); 
} ELSE { 
        Coursecode: echo $row['coursecode'];
        Module: echo $row['mName']; 
        echo $row['mCredits'];
        echo $row['TotalContactHours'];
        echo $row['mdescription'];
        echo $row['Syllabus'];
        }
?>

但是,我似乎只能从我的数据库中获取最后一个条目,以帮助解决此问题或更好的编码方式,因此它将不胜感激

谢谢

4

2 回答 2

0

The main error is in your final query, you're not actually fetching anything from the query, so you're just displaying the LAST row you fetched in the first query.

Some tips:

1) Don't use a for() loop to fetch results from a query result. While loops are far more concise:

$result = mysql_query(...) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
   ...
}

2) Add another one of these while loops to your final query, since it's just being executed, but not fetched.

于 2012-06-06T14:05:23.153 回答
0

For me i would use some javascript(NOTE: i prefer jQuery)
An easy technique would be to do this(going on the assumption that when creating the drop downs, your record also contains the description):

Apart from creating your dropdown options like this <option value="...">data</option>, you could add some additional attributes like so:

echo '<option value="'.$row['coursecode'].'" data-desc="'.$row['description'].'">.....</option>

Now you have all your drop down options, next is the javascript part

Let's assume you have included jQuery onto your page; and let's also assume that the description of any selected course is to be displayed in a <div> called description like so:

<div id="course-description">&nbsp;</div>
<!--style it how you wish -->

With your javascript you could then do this:

$(function(){
    $("#id-of-course-drop-down").change(function(){
        var desc = $(this).children("option").filter("selected").attr("data-des");
        //now you have your description text
        $("#course-description").html(desc);
        //display the description of the course
    }
});

Hope this helps you, even a little
Have fun!

NOTE: At least this is more optimal than having to use AJAX to fecch the description on selection of the option :)

于 2012-06-06T14:06:56.943 回答