php Nest while循环

时间:2014-08-09 00:40:39

标签: php while-loop nested

我有这个嵌套的while循环似乎应该可以工作,但内循环只运行一次。我必须继续运行查询的原因是每年都会有与该年相关的不同月份。

预计输出为
2013>四月
       可以
2014>七月

实际输出为
2013>四月 2014>七月

while ($row_year = mysqli_fetch_array($result_year, MYSQLI_ASSOC))
{
    $nest_usage_data_year[] = $row_year;
    echo ("<li class=\"dropdown\"><a href=\"#\">"
    . $nest_usage_data_year[$y]['days']
    . "<i class=\"icon-angle-right\"></i></a>");
    $result_month = mysqli_query($dbc,
    "SELECT DISTINCT DATE_FORMAT(days, '%M') "
    . "As days FROM energy_history "
    . "WHERE serial_number = '".$_SESSION['serial_number']."' "
    . "AND DATE_FORMAT(days, '%Y') = '"
    .  $nest_usage_data_year[$y]['days']."' "); //add AND month to filter

    $y++;

    // This while is only iterating once...
    while ($row_month = mysqli_fetch_array($result_month))
    {
        $nest_usage_data_month[] = $row_month;
        echo (" <ul style=\"display: none;\" class=\"dropdown-menu sub-menu-level1 bold\">");
        echo ("<li><a href=\"#\">".$nest_usage_data_month[$m]['days']."</a></li>");
        echo ("</ul>");
        echo ("</li>");
        $m++;
    }
}   

0 个答案:

没有答案