如何从更多字段的数据库中填写选择选项菜单?

时间:2014-10-23 09:36:16

标签: php mysql database

我有一个选择选项菜单。 我从数据库中填写它:

$value = mysql_query("SELECT title FROM movies ORDER BY id DESC");
$i = 0;
while ($row = mysql_fetch_object($value))
{
    $release[$i] = $row->title;
    $i++;
}

我写出来了:

<select name="release"><?php 
foreach($release as $i){
    echo"<option value = '$i' > $i </option>";
}
?></select>

它工作正常,但我想在菜单中写下标题旁边的发行年份。 发行年份在发行版中的同一电影表中。 我如何追加这两个字段&#39;值?

谢谢!

3 个答案:

答案 0 :(得分:2)

从数据库中选择两个列并将整个行对象放在数组中:

$value = mysql_query("SELECT title, release_year FROM movies ORDER BY id DESC");
while ($row = mysql_fetch_object($value))
{
    $release[] = $row;
}

然后把它们写出来:

<select name="release"><?php 
foreach($release as $i => $r){
    echo"<option value = '$r->title' > $r->title ($r->release_year) </option>";
}
?></select>

答案 1 :(得分:1)

将您的代码更改为此

$value = mysql_query("SELECT title, release FROM movies ORDER BY id DESC");
$i = 0;
while ($row = mysql_fetch_object($value))
{
    $release[$i] = $row->title . ' (' . $row->release . ')';
    $i++;
}

答案 2 :(得分:1)

试试这个: TESTED

    <select name="release">
        <?php
        $value = "SELECT title, release_year FROM movies ORDER BY id DESC";
        $select=mysql_query($value);
while($row=mysql_fetch_array($select))
    {
        $title=$row['title'];
      $release_year=$row['release_year'];

     echo $title_rel_yr=$title.'('.$release_year.')';
            echo"<option value = '$title' >$title_rel_yr</option>";
                }       
        ?>
      </select>