Query database, explode date to get year, and populate dropdown with unique year?

$result = mysql_query("SELECT distinct year(date) as years FROM user_history ORDER BY 1 ASC").

There's no "years" row. How does this work? – nitsuj Aug 9 at 16:29 @nitsuj the keyword 'as' renames the selected column/value to 'years'.

You can also do that with tables. – Quasdunk Aug 9 at 16:30 I think that I should have worded that differently. I see that it is set AS years, but how does year(date) pull the year out of a value like 08-09-2011?

– nitsuj Aug 9 at 16:34 @nitsuj It's an sql-function, you can do the same with month() and day(). But it only works with fields of some kind of date-type. – Quasdunk Aug 9 at 16:39 That was the problem...fields were not set as date-type.

This solution worked! Thanks! – nitsuj Aug 9 at 16:49.

SELECT DISTINCT YEAR(date) FROM user_history, maybe with ORDER BY date, would make the PHP part much simpler...

Try: SELECT DISTINCT YEAR(date) FROM user_history.

First thing, may I suggest that you use date_parse on the $row'date', something like this. $date = date_parse($row'date'); $year = $date'year'; The reason years are showing up twice is that you have no check for redundancy. I would recommend adding each year to an array and using in_array($year, $array) to check if you already have it added.

Try: SELECT DISTINCT YEAR(date) as date FROM user_history ORDER BY date asc You don't need the code that you had to extract the year component this way... Your full code would be: $result = mysql_query("SELECT DISTINCT YEAR(date) as date FROM user_history ORDER BY date asc"); $yearoptions = ""; while($row = mysql_fetch_array($result)) { $date = $row'date'; $yearoptions . = '' . $date .''; Edite You may also find the following useful when working with dates / times in the future dev.mysql.com/doc/refman/5.5/en/date-and....

You aren't checking for duplicates in the year before outputting it. That's your base problem. You should store the possible years as an array first, then populate the options based on that array.

That way, you won't have to worry about duplicating. If you have 11.26.2009, 09.01.2010, 12.15.2010, 06.05.2011, iterate through all the results for an array similar to $yearOptions = array('2009', '2010', '2011'); The other benefit here is if there's an error with the way the dates are displaying, you can simply var_dump($yearOptions); to see what's showing up in there. Finally, I'd suggest using strtotime() instead of explode(), simply because it can accept a potentially non-standard date format and still return a proper UNIX timestamp.

Then you can manipulate it using date(). Extra mini-bonus: if you ever want to switch to '09 rather than 2009, it's a simple switch. $result = mysql_query("SELECT date FROM user_history"); $yearOptions = array(); while($row = mysql_fetch_array($result)) { $date = $row'date'; // convert the date to a UNIX timestamp $date = strtotime($date); // retrieve just the Y (xxxx) from the timestamp $year = date('Y', $date) // if the year doesn't already exist in the array, add it.

If(!in_array($year, $yearOptions)) { $yearOptions = $year; } } foreach($yearsOptions as $year) { echo('' . $year . ''); } Hope it works.

As mentioned above, the easiest way would be SELECT DISTINCT YEAR(date) FROM user_history ORDER BY date but if the date is not saved in database a regular DATE db type, you cannot apply the year function and then you need to process manually. $result = mysql_query("SELECT date FROM user_history"); $yearoptions = ""; $yearArray = array(); while($row = mysql_fetch_array($result)) { $date = $row'date'; $yeararray = explode("-", $date); array_push($yearArray, $year2); } $yearArray = array_unique($yearArray); // Keep only distinct elements $yearArray = array_sort($yearArray); // To have years in asc order for(int i=0; i'. $yearArrayi .''; }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions