_tina_ Posted November 10, 2009 Share Posted November 10, 2009 Hi, I have a series of rows returned from a query. I need to organize the results based on the year in the date column. For example: 2009 ------ row1 row2 ... 2008 ------ rowA rowB Do you know how I can achieve this? Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/181003-php-organize-query-result-output/ Share on other sites More sharing options...
siric Posted November 10, 2009 Share Posted November 10, 2009 You need to run a select for each year and order the results Say you had a table named TABLE with columns DATE and VALUE. $sqlYear = "SELECT DISTINCT YEAR(datepro) AS YEAR FROM factory_output ORDER BY YEAR DESC;"; //This will select all unique instances of the year from the date field and sort them in descending order $resYear = mysql_query($sqlYear); $numYear = mysql_num_rows($resYear); for ($i = 0; $i < $numYear; $i++) { $year = mysql_result($resYear,$i,'year'); $sqlValue = "select VALUE from TABLE where year = '$year' order by VALUE"; $resValue = mysql_query($sqlValue); $numValue = mysql_num_rows($resValue); echo "$year <br />; echo "--------- <br />; for ($j=0; $j < $numValue ;j++) { //Rotates through each instance of the value for that year $value = mysql_result($res,$j,'value'); echo "$value <br />; } echo "<br />"; } Wrote this on the fly, so give this a wing and let us know. Link to comment https://forums.phpfreaks.com/topic/181003-php-organize-query-result-output/#findComment-955138 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.