dlebowski Posted May 25, 2008 Share Posted May 25, 2008 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/107215-solved-drop-down-population-query-problems/ Share on other sites More sharing options...
jonsjava Posted May 25, 2008 Share Posted May 25, 2008 <?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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/107215-solved-drop-down-population-query-problems/#findComment-549667 Share on other sites More sharing options...
dlebowski Posted May 25, 2008 Author Share Posted May 25, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/107215-solved-drop-down-population-query-problems/#findComment-549723 Share on other sites More sharing options...
dlebowski Posted May 25, 2008 Author Share Posted May 25, 2008 Why won't this work? I get this error "Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';'" echo "<option value="$nt['YEAR(Date)']">$nt['YEAR(Date)']</option>"; Quote Link to comment https://forums.phpfreaks.com/topic/107215-solved-drop-down-population-query-problems/#findComment-549740 Share on other sites More sharing options...
BlueSkyIS Posted May 25, 2008 Share Posted May 25, 2008 you can't use MySQL YEAR() function in PHP. this doesn't do anything in PHP (except generate an error): YEAR(Date); Quote Link to comment https://forums.phpfreaks.com/topic/107215-solved-drop-down-population-query-problems/#findComment-549754 Share on other sites More sharing options...
BlueSkyIS Posted May 25, 2008 Share Posted May 25, 2008 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']; Quote Link to comment https://forums.phpfreaks.com/topic/107215-solved-drop-down-population-query-problems/#findComment-549758 Share on other sites More sharing options...
dlebowski Posted May 25, 2008 Author Share Posted May 25, 2008 That took care of it. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/107215-solved-drop-down-population-query-problems/#findComment-549774 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.