我试图找出如何通过检查其值来选择下拉选项,但该值来自另一个查询。
我从名为$FK_TopicID
的查询中获取$quickedit
。下拉列表由名为$topresult
的其他查询生成。我有一个IF / ELSE语句,当SELECTED
等于<option value="the Topic ID" SELECTED>
时,该语句应该在$FK_TopicID
之类的选项内打印$row['TopicID']
。
我只是不确定如何检查$FK_TopicID
while
循环中的$topresult
。有什么想法吗?
<?php
$NewsID = $_GET["n"];
$quickedit = mysql_query("SELECT * FROM News LEFT JOIN Topics on Topics.TopicID = News.FK_TopicID WHERE NewsID = $NewsID ORDER BY TopicName ASC, NewsTitle");
$row = mysql_fetch_array($quickedit);
echo "<p>" . $FK_TopicID . "</p>";
/* additional php... */
$topresult = mysql_query("SELECT * FROM Topics WHERE FK_UserID=$_SESSION[user_id] ORDER BY TopicSort, TopicName");
while($row = mysql_fetch_array($topresult)) {
if ( $row['TopicID'] == $FK_TopicID){ /* $FK_TopicID not printing value here */
$selected = " SELECTED";
} else {
$selected = "";
}
echo '<option value=\"' . $row['TopicID'] . '" ' . $selected . '>' . $row['TopicName'] . '</option>';
}
?>
答案 0 :(得分:0)
我对数据库的结构一无所知,但我会试一试
这会输出你想要的东西吗?
<?php
$NewsID = $_GET['n'];
$quickedit = mysql_query("SELECT * FROM News LEFT JOIN Topics on Topics.TopicID = News.FK_TopicID WHERE NewsID = $NewsID ORDER BY TopicName ASC, NewsTitle");
$topresult = mysql_query("SELECT * FROM Topics WHERE FK_UserID=$_SESSION[user_id] ORDER BY TopicSort, TopicName");
echo "<p>" . $FK_TopicID . "</p>";
while($row = mysql_fetch_array($quickedit)) {
while($row2 = mysql_fetch_array($topresult)) {
if ( $row2['TopicID'] == $row['FK_TopicID']){
$selected = " SELECTED";
} else {
$selected = "";
}
echo '<option value=\"' . $row2['TopicID'] . '" ' . $selected . '>' . $row['TopicName'] . '</option>';
}
}
?>
此代码循环遍历两个查询,如果TopicID
中的$topresult
等于FK_TopicID
中的$quickedit
,则会选中该代码。