闰年和非闰年分开放在两个表格中(关于)IT技术。

4
这段代码将显示1991年至2100年的闰年和非闰年,我试图制作一个表格来区分它们,但我失败了。 请问如何将其呈现为表格格式或网格系统?这是为了学术研究。
 <!DOCTYPE html>
    <html>
    <head>
        <title>Leap Year</title>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    </head>
    <body>
        <?php  
        function isLeap($year) {
        return ((($year % 4) == 0) && ((($year % 100) != 0)));
        }

            for($year=1991; $year<=2100; $year++)  
            {  
                If (isLeap($year))  
                {   
                    $leap="$year : LEAP YEAR <br/>";
                    //echo "<div class='col-sm-12'>" . $leap . "</div>";
                    echo $leap;
                }  
                else  
                {  
                    $nonLeap="$year : Not leap year <br/>";  
                    //echo "<div class='col-sm-6'>" . $nonLeap ."</div>";       
                    echo $nonLeap;           
                }  
            }
        ?>
    </body>
    </html>

你能告诉我你已经尝试过什么了吗? - Ahmed Ginani
所以,你想要两个表格:一个只列出闰年列表,另一个则不包括?在那个 for 循环内,你可以将它们存储在两个数组中,然后使用它们构建你的表格。试一下,然后更新你的答案。 - Federkun
我不知道如何将它转换为数组,这就是问题所在,并打印出来 @Federkun - mishal
我应该从我哥哥告诉我的年份中找出闰年,我已经做到了。但现在他告诉我要分别显示闰年和非闰年,可以使用表格或网格系统。 - mishal
2个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
0

(代表发帖人发布)

感谢您的帮助,我已经明白了。这就是答案:

<!DOCTYPE html>
<html>
<head>
    <title>Leap Year</title>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.css"/>
    <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.js"></script>
    <style>
        .table-border{
            border:1px solid black;
            margin-top: 20px !important;
            margin-bottom: 20px !important; 
        }
        header{
            text-align: center;
            font-size: 50px;
            border: 1px solid;
            margin-left: 28px;
            margin-right: 28px;
            margin-top: 20px;
        }
    </style>
</head>
<body>
<header>Leap Year and Non-Leap year...</header>
    <script>
        $(document).ready(function() {
            $('#table.display').DataTable();
             "pagingType": "full_numbers"
        } );    
    </script>

    <?php  
        function isLeap($year) {
        return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)));
        }

        for($year=1900; $year<=2100; $year++)  
        {  
            If (isLeap($year))  
            {   
                $leaps[] = $year;
            }  
            else  
            {  
                $nonLeaps[] = $year;
            }
        }
        echo'<div class="col-sm-12">';
        echo'<div class="col-sm-6">';
        echo'<table style="width: 100%" id="DataTables_Table_0" class=" table-border display dataTable"><tr><th>Leap Year</th></tr>';

                foreach($leaps as $y){

                        echo '<tr>';
                        echo '<td>' . $y . '</td>';
                        echo '</tr>';

                    }

        echo '</table></div>';

        echo'<div class="col-sm-6">';
        echo'<table style="width: 100%" id="DataTables_Table_0" class="table-border display dataTable"><tr><th>Non-Leap Year</th></tr>';

                    foreach ($nonLeaps as $ny) {

                        echo '<tr>';
                        echo '<td>' . $ny . '</td>';
                        echo '</tr>';

                        }
        echo '</table></div>';
        echo '</div>';
    ?>
</body>
</html> 

0
你的 isLeap 函数有误。你可以参考这个 post
function isLeap($year) {
    return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)));
}

for($year=1991; $year<=2100; $year++)  
{  
    if (isLeap($year))  
    {   
        $leaps[] = $year;
    }  
    else  
    {  
        $nonLeaps[] = $year;
    }
}

echo '<table><tr>';
foreach($leaps as $y)
{
  echo '<td>' . $y . '</td>';
}
echo '</tr></table>';

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,