开发者

"for" loop inside another "for" loop

I have quick question, and I'm sure some of you guys will be able to point out what I'm doing wrong.

Basically, I want to create a table with (calendar), with the months and days of an entire year, and Im using php to do it, my code is doing something weird. What happens is that January is empty and the days for January are being put in february.

My code so far is as follows:

$months = 12;
$monthsOfTheyear = array("Januany","February","March","April","May","June","July","August","September","October","November","December");
$currentMonth = date("n");
$currentYear = date("Y");
$daysOfTheMonth = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear);
for($i = 0; $i < $months; $i++){
echo    "   <tbody class='month'>";
echo    "       <tr>
                    <td colspan='".$daysOfTheMonth."'>
                        ".$monthsOfTheyear[$i]."
                    </td>
                </tr>
                <tr>
        ";
$daysOfEac开发者_开发知识库hMonth = cal_days_in_month(CAL_GREGORIAN, $i, $currentYear);
    for($d = 1; $d <= $daysOfEachMonth; $d++){

        echo    "   <td>
                        ".$d."
                    </td>
                ";
    }
    echo    "       </tr>
            </tbody";
}

I'm obviously doing something wrong, but I've been staring at the monitor for about an hour trying to figure it out.

I'd appreciate any advice.

Thanks


In

$daysOfEachMonth = cal_days_in_month(CAL_GREGORIAN, $i, $currentYear);

You are using $i. The first value is 0. That's why you don't get any value.

if u use

$daysOfEachMonth = cal_days_in_month(CAL_GREGORIAN, $i+1, $currentYear);

you solve your problem


You are passing 0 as month number of January to function cal_days_in_month which is incorrect. The function expects 1 for January.

So change $i to $i+1:

$daysOfEachMonth = cal_days_in_month(CAL_GREGORIAN, $i+1, $currentYear);
                                                      ^^

Looks like you've warnings disabled. If you enable them you'll see the following warning for your existing code:

PHP Warning:  cal_days_in_month(): invalid date.


If this line:

$daysOfEachMonth = cal_days_in_month(CAL_GREGORIAN, $i, $currentYear);

Becomes this line:

$daysOfEachMonth = cal_days_in_month(CAL_GREGORIAN, $i + 1, $currentYear);

Does it solve anything?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜