【问题标题】:Leap year and non leap year into separate tables闰年和非闰年放在单独的表中
【发布时间】:2017-10-04 20:28:32
【问题描述】:

此代码将显示从 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>

【问题讨论】:

  • 告诉我你尝试了什么?
  • 那么,您需要两张表:一张只包含闰年列表,另一张没有闰年列表?在 for 循环中,您可以将它们存储在两个数组中,然后用它们构建您的表。尝试一下,然后更新你的答案。
  • 我不知道如何将其转换为数组,这就是问题所在并打印出来@Federkun
  • 我应该按照我兄弟告诉我的年份逐年闰年,我做到了。但现在他告诉我分开显示,比如一张表中的闰年和一张表中的非闰年或使用网格系统

标签: php html css bootstrap-modal leap-year


【解决方案1】:

(代表 OP 发布)

谢谢你的帮助,我知道了。这就是答案:

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

【讨论】:

    【解决方案2】:

    你的 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>';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多