How can I populate a menu of options for selecting from a database with a large number of fields?

I have a selection menu. I populate it from the 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++;
}

And I write them:

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

It works great, but I would like to write the year of release next to the name in the menu. The year of release is in the same table of films in the field of release. How to add values ​​of two fields?

Thank!

+4
source share
3 answers

Change your code to

$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++;
}
+1
source

Select both columns from the database and put the entire row object in an array:

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

Then write them both:

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

: 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> 
+1

All Articles