0

I have query, that first fetches years. And then all months that is represented for that year, from the same table and same column (date).

It look like this:

public static function arrange(){

        $database = DatabaseFactory::getFactory()->getConnection();

        //Select all years
        $sql = "SELECT DISTINCT YEAR(date) as year FROM MOCK_DATA ORDER by YEAR(date) DESC";
        $query = $database->prepare($sql);
        $query->execute();
        $years = $query->fetchAll();

        //Select all months (in numbers)
        //Should this be in a foreach? Loop through the results from $years to get the months?
        $sql = "SELECT DISTINCT MONTH(DATE) AS month_num FROM MOCK_DATA WHERE YEAR(DATE) = :year ORDER BY DATE DESC";
        $query = $database->prepare($sql);

        $months=array();
        foreach($years as $year){
        $query->execute(array(':year' => $year->year));
        $data = $query->fetchAll();
        $months[] = $data;
        }

         // Result from above query is given in numbers, 1 for jan, 2 for feb etc.
         // Those results needs to be converted.

        $data = new stdClass;
        $data->years = $years;
        $data->months = $months;

        return $data;

    }

This functions gives me this stdClass object:

object(stdClass)#57 (2) { 
["years"]=> array(3) {
    [0]=> object(stdClass)#19 (1) { ["year"]=> string(4) "2015" } 
    [1]=> object(stdClass)#20 (1) { ["year"]=> string(4) "2014" } 
    [2]=> object(stdClass)#21 (1) { ["year"]=> string(4) "2013" } 
}

["months"]=> array(3) { 
    [0]=> array(11) { 
    [0]=> object(stdClass)#8 (1) { 
    ["month_num"]=> string(2) "11" } 
    [1]=> object(stdClass)#23 (1) { 
    ["month_num"]=> string(2) "10" }
    [2]=> object(stdClass)#24 (1) { 
    ["month_num"]=> string(1) "9" } 
    [3]=> object(stdClass)#25 (1) { 
    ["month_num"]=> string(1) "8" } 
    [4]=> object(stdClass)#26 (1) { 
    ["month_num"]=> string(1) "7" } 
    [5]=> object(stdClass)#27 (1) { 
    ["month_num"]=> string(1) "6" } 
    [6]=> object(stdClass)#28 (1) { 
    ["month_num"]=> string(1) "5" } 
    [7]=> object(stdClass)#29 (1) { 
    ["month_num"]=> string(1) "4" } 
    [8]=> object(stdClass)#30 (1) { 
    ["month_num"]=> string(1) "3" } 
    [9]=> object(stdClass)#31 (1) { 
    ["month_num"]=> string(1) "2" } 
    [10]=> object(stdClass)#32 (1) { 
    ["month_num"]=> string(1) "1" } } 
    [1]=> array(12) { 
    [0]=> object(stdClass)#33 (1) { 
    ["month_num"]=> string(2) "12" } 
    [1]=> object(stdClass)#34 (1) { 
    ["month_num"]=> string(2) "11" }
    [2]=> object(stdClass)#35 (1) { 
    ["month_num"]=> string(2) "10" } 
    [3]=> object(stdClass)#36 (1) { 
    ["month_num"]=> string(1) "9" } 
    [4]=> object(stdClass)#37 (1) { 
    ["month_num"]=> string(1) "8" } 
    [5]=> object(stdClass)#38 (1) { 
    ["month_num"]=> string(1) "7" } 
    [6]=> object(stdClass)#39 (1) { 
    ["month_num"]=> string(1) "6" } 
    [7]=> object(stdClass)#40 (1) { 
    ["month_num"]=> string(1) "5" } 
    [8]=> object(stdClass)#41 (1) { 
    ["month_num"]=> string(1) "4" } 
    [9]=> object(stdClass)#42 (1) { 
    ["month_num"]=> string(1) "3" } 
    [10]=> object(stdClass)#43 (1) { 
    ["month_num"]=> string(1) "2" } 
    [11]=> object(stdClass)#44 (1) { 
    ["month_num"]=> string(1) "1" } } 
    [2]=> array(12) { 
    [0]=> object(stdClass)#45 (1) { 
    ["month_num"]=> string(2) "12" } 
    [1]=> object(stdClass)#46 (1) { 
    ["month_num"]=> string(2) "11" }
    [2]=> object(stdClass)#47 (1) { 
    ["month_num"]=> string(2) "10" } 
    [3]=> object(stdClass)#48 (1) { 
    ["month_num"]=> string(1) "9" } 
    [4]=> object(stdClass)#49 (1) { 
    ["month_num"]=> string(1) "8" } 
    [5]=> object(stdClass)#50 (1) { 
    ["month_num"]=> string(1) "7" } 
    [6]=> object(stdClass)#51 (1) { 
    ["month_num"]=> string(1) "6" } 
    [7]=> object(stdClass)#52 (1) { 
    ["month_num"]=> string(1) "5" } 
    [8]=> object(stdClass)#53 (1) { 
    ["month_num"]=> string(1) "4" } 
    [9]=> object(stdClass)#54 (1) { 
    ["month_num"]=> string(1) "3" } 
    [10]=> object(stdClass)#55 (1) { 
    ["month_num"]=> string(1) "2" } 
    [11]=> object(stdClass)#56 (1) { 
    ["month_num"]=> string(1) "1" } 
} 
} 
} 

Im having hard time to echo out the results of this, for example:

foreach($this->arrange->months as $month){
    echo $month->month_num;
}

Gives me this error Notice: Trying to get property of non-object in..

And wouldent it be better if the returned array looked something like:

data => array(
year = 2013,
months = array(1,2,3,4,5,6,7,8,9,10,11,12),

year = 2014,
months = array(1,2,3,4,5,6,7,8,9,10,11,12),

year = 2015,
months = array(1,2,3,4,5,6,7,8,9,10)
)

My question:

  1. How to i output the results that I'm getting from this function? without the error
  2. Can i change the function to make the returned array more useable?
Adam
  • 1,231
  • 1
  • 13
  • 37
  • I don't understand why you have two queries. – Strawberry Oct 13 '15 at 13:28
  • month is not an object so you can't access the month_num with `->` operator, try `echo $month['month_num']` – Fanax Oct 13 '15 at 13:33
  • I don't understand why you need mysql for this. – e4c5 Oct 13 '15 at 13:33
  • @Strawberry If you know how to do this in one query i would be very thankful! @e4c5 because all data is in the mysql database, including that `date` column i was talking about in the question? – Adam Oct 13 '15 at 18:21

0 Answers0