[英]Dynamic drop down list!
I have edited the question as what I said before was two pieces of code together. 我已经编辑了这个问题,因为我之前所说的是两段代码在一起。
I have the following code: 我有以下代码:
$f = "SELECT * FROM ".TBL_FIXTURES." WHERE compname = '$_POST[league]' AND home_user = '$_SESSION[username]' ORDER BY away_user";
$fixtures = mysql_query($f);
?>
<?
while( $row = mysql_fetch_assoc($fixtures))
{
extract($row);
$info = explode("_",$row[compname]);
?>
<select name="hu" class="combo">
<option value="<? echo $home_user ?>"><? echo $home_user?></option>
</select>
<select name="au" class="combo">
<?php
while( $row = mysql_fetch_assoc($fixtures))
{
$u=$row['away_user'];
echo "<option value=\"$u\">$u</option>";
}
?>
</select>
<?
}
?>
As you can see, there are two while loops. 如您所见,有两个while循环。 But the first value from the dropdown list for away_user is missing. 但是,缺少下拉列表中away_user的第一个值。 If i remove the first loop, it appears but the drop down for home_user disappears. 如果我删除了第一个循环,它将出现,但home_user的下拉列表将消失。 How can i get around this? 我该如何解决?
THanks 谢谢
does it give an error? 它会给出错误吗? it seems like you are missing a bracket (") in th end of this line : 似乎您在此行的末尾缺少括号(“):
echo"<div align=\"center\"> <table cellspacing=\"10\" style='border: 1px dotted' width=\"450\" bgcolor=\"#eeeeee\">
I'm not sure why you are also printing a table... but you output </select>
in the loop, that's the problem. 我不确定为什么还要打印表格...但是您在循环中输出</select>
,这就是问题所在。
You should do 你应该做
echo '<select>';
while (....)
{
echo '<option>'.$u.'</option>';
}
echo '</select>';
<table code here>
The problem is that in your second loop you call mysql_fetch_assoc($fixtures) so you move on to the second record before using the away_user field from the first record. 问题在于,在第二个循环中,您将调用mysql_fetch_assoc($ fixtures),以便在使用第一条记录中的away_user字段之前移至第二条记录。 Two options I can think of are: 我可以想到的两个选择是:
Create the first option before the loop 在循环之前创建第一个选项
<select name="au" class="combo">
<?php
$u=$row['away_user'];
echo "<option value=\"$u\">$u</option>";
while( $row = mysql_fetch_assoc($fixtures))
{
$u=$row['away_user'];
echo "<option value=\"$u\">$u</option>";
}
?>
</select>
Change the loop to a do while loop 将循环更改为do while循环
<select name="au" class="combo">
<?php
do
{
$u=$row['away_user'];
echo "<option value=\"$u\">$u</option>";
} while( $row = mysql_fetch_assoc($fixtures))
?>
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.