Jump to content

[SOLVED] Drop Down Population Query Problems


dlebowski

Recommended Posts

I'm sure this is an easy one.  Just can't seem to figure it out.

 

I have this code that basically populates a dropdown box from the database. 

 

<?php
                    include("dbinfo.inc.php");
                    mysql_connect(localhost,$username,$password);
                    @mysql_select_db($database) or die( "Unable to select database");
                    $query = "SELECT YEAR(Date) FROM auctions GROUP BY YEAR(Date)";
                    $result = mysql_query ($query);
                    echo "<select name=Date>";
                    // printing the list box select command

                    while($nt=mysql_fetch_array($result))
                    {

                    //Array or records stored in $nt
                    echo "<option value=$nt[YEAR(Date)]>$nt[YEAR(Date)]</option>";
                    /* Option values are added by looping through the array */
                    }
                    echo "</select>";// Closing of list box
                    ?>

 

This portion of the code does not work.  It won't populate the values.  I know it's syntax.  Any help would be appreciated!  Thanks.

 

echo "<option value=$nt[YEAR(Date)]>$nt[YEAR(Date)]</option>";

<?php
                    include("dbinfo.inc.php");
                    mysql_connect(localhost,$username,$password);
                    @mysql_select_db($database) or die( "Unable to select database");
                    $query = "SELECT YEAR(Date) FROM auctions GROUP BY YEAR(Date)";
                    $result = mysql_query ($query);
                    echo "<select name=Date>";
                    // printing the list box select command

                    while($nt=mysql_fetch_array($result))
                    {

                    //Array or records stored in $nt
                    $date = $nt['date'];
                    echo "<option value='$date'>$date</option>";
                    /* Option values are added by looping through the array */
                    }
                    echo "</select>";// Closing of list box
                    ?>

Thank you for the quick response.  $date = $nt['date']; won't work because I am using YEAR(date) in my query.  That is where I am running into the problem.  Any other suggestions?

 

you probably want to alias YEAR() to give it a name to use later:

 

$query = "SELECT YEAR(Date) AS date_year FROM auctions GROUP BY date_year";

 

then look at $nt['date_year'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.