完成日历表,使用php代码

时间:2015-11-03 02:01:23

标签: php html

当在Notepad ++中,php中的""内的任何内容都不会显示并完成表格。所以echo "<tr>";变成灰色并且没有完成表格。有什么建议吗?

<table border='1'>
     <tr>
            <td><input type='button' value='<' name='previousebutton' onclick="goLastMonth(<?php echo $month.",".$year?>)"></td>
            <td colspan='5'> <?php echo $monthName.", ".$year ?> </td>
            <td><input type='button' value='>' name='nextbutton'onclick="goNextMonth(<?php echo $month.",".$year?>)" ></td>
        </tr>
        <tr>
            <td width='50px'>Sun</td>
            <td width='50px'>Mon</td>
            <td width='50px'>Tue</td>
            <td width='50px'>Wed</td>
            <td width='50px'>Thu</td>
            <td width='50px'>Fri</td>
            <td width='50px'>Sat</td>
        </tr>
        <?php
            echo "<tr>";

          for($i = 1; $i < $numDays+1; $i++ , $counter++){
                $timeStamp = strtotime("$year-$month-$i");
                if($i == 1) {
                    $firstDay = date("w", $timeStamp);
                    for($j = 0; $j < $firstDay; $j++, $counter++) {

                        echo "<td>$nbsp;</td>";             
                    }
                }
                if($counter % 7 == 0) {
                    echo "</tr><tr>";
                }
          }

          echo "</tr>";
          ?>
    </table>

2 个答案:

答案 0 :(得分:0)

你可以尝试使用备用控制结构语法,虽然(在我看来)它可能使一些代码更难阅读:

<?php for($i = 1; $i < $numDays+1; $i++ , $counter++){
            $timeStamp = strtotime("$year-$month-$i");
            if($i == 1) {
                $firstDay = date("w", $timeStamp);
                for($j = 0; $j < $firstDay; $j++, $counter++): ?>
                    <td>$nbsp;</td>
                <?php endfor; ?>         
            }
            if($counter % 7 == 0): ?>
               </tr><tr>
            <?php endif; ?>
      }
?>

答案 1 :(得分:0)

修改了一些代码:

<?php 
    if(isset($_GET['day'])){ $day = $_GET['day']; }else{ $day = date("j"); } 
    if(isset($_GET['month'])){ $month = $_GET['month']; }else{ $month = date("n"); } 
    if(isset($_GET['year'])){ $year = $_GET['year']; }else{ $year = date("Y"); } 
    //calender variable 
    $currentTimeStamp = strtotime("$year-$month-$day"); 
    $monthName = date("F", $currentTimeStamp); 
    $numDays = date("t" , $currentTimeStamp); 
    $counter = 1; 
    $day = date('d', $currentTimeStamp);
    $firstDay = date('w', strtotime("{$year}-{$month}-01"));
?>
<table border='1'>
     <tr>
            <td><input type='button' value='<' name='previousebutton' onclick="goLastMonth(<?php echo $month.",".$year?>)"></td>
            <td colspan='5'> <?php echo $monthName.", ".$year ?> </td>
            <td><input type='button' value='>' name='nextbutton'onclick="goNextMonth(<?php echo $month.",".$year?>)" ></td>
        </tr>
        <tr>
            <td width='50px'>Sun</td>
            <td width='50px'>Mon</td>
            <td width='50px'>Tue</td>
            <td width='50px'>Wed</td>
            <td width='50px'>Thu</td>
            <td width='50px'>Fri</td>
            <td width='50px'>Sat</td>
        </tr>
        <tr>
            <?php for($i = 0; $i < $firstDay; $i++): ?>
                <td></td>
            <?php endfor; ?>
            <?php for($i = 1; $i <= $numDays; $i++): ?>
                <?php if($day == $i): ?>
                    <td><strong><?php echo $i ?></strong></td>
                <?php else: ?>
                    <td><?php echo $i ?></td>
                <?php endif; ?>
                <?php if(($i + $firstDay) % 7 == 0): ?>
                    </tr><tr>
                <?php endif; ?>
            <?php endfor; ?>
            <?php for($i = 0; ($i + $firstDay + $numDays) % 7 != 0; $i++): ?>
                <td></td>
            <?php endfor; ?>
        </tr>
    </table>