根据第一个

时间:2015-10-13 13:26:20

标签: php mysql

我有查询,首先是多年。然后是同一个表和同一列(date)表示该年份的所有月份。

看起来像这样:

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;

    }

这个函数给了我这个stdClass对象:

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" } 
} 
} 
} 

我很难回应结果,例如:

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

给我这个错误Notice: Trying to get property of non-object in..

如果返回的数组看起来像是这样的话,那就更好了。

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)
)

我的问题:

  1. 如何输出我从此功能获得的结果? 没有错误
  2. 我可以更改函数以使返回的数组更有用吗?

0 个答案:

没有答案