如何在选择下拉列表中选择ID类别?

时间:2015-01-19 16:05:55

标签: php sql

我有一个数据库表,其中包含一个名为“projCat”的列的所有项目,该列是一个与另一个名为projectcats的表相关的ID字段。

项目表projCat有一个与projectscat ID相关的ID。

<li class="field"><h2>Select Project Sector?</h2>
<select name="projCat" id="projCat" class="input">
<?php 
    $cat = $_GET['cat'];
    $query1 = mysql_query("SELECT * FROM projectscat");

    $checked == '$cat' ? 'selected' : '';

    while($row1 = mysql_fetch_object($query1)){
        echo "<option value=\"" . $row1->projCatID . "\"" . $checked . ">" . $row1->projCatTitle . "</option>\n";
    }
?>
</select></li>

我希望得到的结果是所有项目类别的列表,但获得projCat,因此它首先突出显示此类别。

2 个答案:

答案 0 :(得分:0)

试试这个:

    <li class="field"><h2>Select Project Sector? <?php echo $row->projCat;?></h2>
    <select name="projCat" id="projCat" class="input">
    <?php 
        $cat = $_GET['cat'];
        $query1 = mysql_query("SELECT * FROM projectscat");
        while($row1 = mysql_fetch_object($query1)){
            echo "<option value='" . $row1->projCatID . "' selected='";

            if($cat == $row1->[your category in table]) {
                echo "selected";
            } else {
                echo '';
            }
            echo "'>" . $row1->projCatTitle . "</option>";
        }
    ?>
    </select></li>

答案 1 :(得分:0)

来自IamNewbie的代码

while($row1 = mysql_fetch_object($query1)){
     $checked = $cat ? ' selected="selected"' : '';
    echo "<option value='" . $row1->projCatID . "' " . $checked . ">" . $row1->projCatTitle . "</option>";
}