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>";

Link to comment
Share on other sites

<?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
                    ?>

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.