1

Hi I know this might sound familiar to other questions here in stackoverflow but what I'm trying to derived on this question, are there any possible way on how you can GROUP a list of MONTH and YEARS with its Days are listed too but are grouped in different months. Like what is shown below:

  1. January, 2016
    • January 1, 2016
    • January 2, 2016
  2. February, 2016
    • February 1, 2016
    • February 2, 2016

I actually have a form and what it does is you can search with a filter of DATE FROM and DATE TO and another filter of MONTHLY viewing. So let you selected the MONTHLY filter and a date range of January 01, 2016 to February 29, 2016 it should display like this.

FIRST GROUP:

  1. January, 2016
    • January 1, 2016
    • January 2, 2016
    • etc.. to January, 31, 2016

SECOND GROUP:

  1. February, 2016
    • February 1, 2016
    • February 2, 2016
    • etc.. to February, 29, 2016

My query is just simple:

$query = msyqli_query($con, "select YEAR(maxi_date),MONTH(maxi_date) from maxi_summaryreport where maxi_date between '2016-01-01' and '2016-02-29' group by YEAR(maxi_date), MONTH(maxi_date)");

$ahtquery = "select maxi_aht,maxi_callsanswered from maxi_summaryreport where maxi_date Between '2016-01-01' and '2016-02-29' and maxi_location='Zapote'
";

$ahtq = mysqli_query($con, $ahtquery);
while($ahtf1 = mysqli_fetch_assoc($ahtq))
{
    $aht1 = $ahtf1['maxi_aht'];
    $ahtout1[] = $aht1;
    //echo $aht1."-summary report<br>";
    $cal1 = $ahtf1['maxi_callsanswered'];
    $calout1[] = $cal1;
    //echo "<br>".$cal1." - ".$aht1."<br>";             
}
while($row = mysqli_fetch_array($query))
{
  echo "<br>".$calout1[$a]." - ".$ahtout1[$a]."<br>";   
  $a++;
}

But what it only display is:

FIRST GROUP

  1. 2016-01-01

SECOND GROUP

  1. 2016-02-01

not

FIRST GROUP

  1. 2016-01-01
  2. 2016-01-02
  3. 2016-01-03
  4. ...etc. to 2016-01-31

SECOND GROUP

  1. 2016-02-01
  2. 2016-02-02
  3. 2016-02-03
  4. ...etc to 2016-02-29

Can somebody help me I've really been looking over the net for some ideas to workaround but I'm not really good at sql. Thank you so much for any help.


Hi Guys thank you so much for all the help and ideas you've given me I just found the solution to my problem. Here's the code below.

 $q=mysqli_query($con,"SELECT DISTINCT MONTH(maxi_date),YEAR(maxi_date), maxi_srid,maxi_date FROM maxi_summaryreport where maxi_location='Zapote' and 
maxi_date between '$datefrom' and '$dateto' group BY MONTH(maxi_date), YEAR(maxi_date)");
 foreach($q as $cat){
    echo '<li id="'.$cat['MONTH(maxi_date)'].'" class="files">';  
    echo '<a href="'.$cat['maxi_srid'].'">'.$cat['MONTH(maxi_date)'].", ".$cat['YEAR(maxi_date)'].'</a>'; 
    echo '<ul class="sub-menu">';
    echo  $cat['maxi_srid'];
    $linkq=mysqli_query($con,"SELECT * FROM maxi_summaryreport WHERE maxi_location = 'Zapote' and MONTH(maxi_date)='" . $cat['MONTH(maxi_date)'] . "'"); 
    foreach($linkq as $link){
       echo '<li><a href="#">'.$link['maxi_date'].'</a></li>';
       echo mysqli_num_rows($linkq);
    }
    echo '</ul></li>';
 }
  • 1
    Don't use `GROUP BY` for this. Use order by and use PHP to style the results. `GROUP BY` is meant to achieve other goals. – apokryfos Jul 07 '16 at 12:31
  • [How to group by month from Date field using sql](http://stackoverflow.com/questions/14565788/how-to-group-by-month-from-date-field-using-sql) – bipin patel Jul 07 '16 at 12:40
  • @apokryfos, Thank you for answering my question but when I use ORDER BY it's only gonna list down everything not group the MONTHS separately but anyways I'll try to find a way and thanks for the idea. :) – vanessapusakal Jul 07 '16 at 12:53
  • @vanessapusakal my point is MySQL doesn't have a built-in way to achieve this since this is styling and it should be handled by the front-end of the application. – apokryfos Jul 07 '16 at 12:53
  • Hi @bipinpatel, thank you for giving me a link. I haven't checked this yet but I'll try also to check if it has the same scenario as mine. :) – vanessapusakal Jul 07 '16 at 12:54
  • @vanessapusakal let me know whether you fix it or not – Swaminathan V Jul 07 '16 at 12:56
  • @apokryfos you're right I'll check again my code and thank you for pointing that out. I'll post if there are any updates. Thanks so much for the effort on answering my query. – vanessapusakal Jul 07 '16 at 12:57
  • @RaguSwaminathan yes i will. I'll try checking on the suggestion given above. – vanessapusakal Jul 07 '16 at 13:03
  • Hi @RaguSwaminathan, I already found solution on my problem I don't if it's this is what you also want to achieve but is now working thank you for all the help guys. I added the solution to my problem above. Thanks a lot :) – vanessapusakal Jul 09 '16 at 13:02

0 Answers0