-3

Table articles

id - content - ins_dt
1 - lorem ipsum1 - 2013-01-09
2 - lorem ipsum2 - 2013-02-08
3 - lorem ipsum3 - 2012-11-03
4 - lorem ipsum4 - 2011-01-20
5 - lorem ipsum5 - 2011-12-13
6 - lorem ipsum6 - 2010-05-12

With query

SELECT ins_dt FROM articles ORDER BY ins_dt DESC;

I get this: (http://sqlfiddle.com/#!2/fc9ea6/5)

"2013 2013 2012 2011 2011 2010" 

But I need this:

"2013 2012 2011 2010"
David Veselý
  • 41
  • 1
  • 6

4 Answers4

2

Whatever the values you are getting store them in array

then $finaldata = array_unique($yourarray);

Ravi Kant Mishra
  • 778
  • 1
  • 7
  • 13
  • While I also thought of this at first, it will only work if the year is the only element of his array. If he is pulling additional data, the elements in the array may still be unique even if they have the same year in them. +1 anyhow for beating me to the buzzer :) – Fluffeh Sep 01 '13 at 10:37
  • Thanks buddy, I just answered to his actual problem. – Ravi Kant Mishra Sep 01 '13 at 10:42
1

You can use simple SQL query

SELECT GROUP_CONCAT(DISTINCT YEAR(ins_dt)) AS `year` 
FROM articles 
ORDER BY ins_dt DESC;

Here is the sample and demo: http://sqlfiddle.com/#!2/fc9ea6/3

invisal
  • 11,075
  • 4
  • 33
  • 54
0

try sort http://php.net/manual/en/function.sort.php

sort($myarr['ins_dt'], SORT_NUMERIC); 
 print_r($myarr);

a little searching effort could land you on great links like this one http://php.net/manual/en/array.sorting.php

WeTheBrains
  • 164
  • 7
0

As you have the data in an array, you can easily use Array unique in your code, which will give you an array with only unique values in it.

Alternately, you could change your SQL to only pull the data you need from the table itself like this:

select distinct year from (select date_format(ins_dt, '%Y') from tableArticles) myData)
Fluffeh
  • 33,228
  • 16
  • 67
  • 80