在表格中显示0到100之间的偶数,包括带有PHP的0

时间:2017-11-30 04:53:24

标签: php html for-loop html-table numbers

我想在列中显示0到100之间的数字(包括0)。

How the table should look like

这是我现在的代码......

<?php

echo "<table border=1><tr><td>";
for ($_getal = 0; $_getal < 100; $_getal++) {

    if ($_getal % 2 == 0) {
        echo "$_getal<br>";
    }
}

?>

6 个答案:

答案 0 :(得分:0)

试试这个:

<?php
echo '<table border="1">';
for ($_getal = 0; $_getal < 100; $_getal++) {
    echo "<tr><td>$_getal</td></tr>";
}
echo '</table>';
?>

答案 1 :(得分:0)

我在你的代码中做了一些修改。

<?php
echo "<table border=1>";
    $temp = 0;
    for ($_getal = 0; $_getal < 100; $_getal++) 
    {
        if($temp == 0 && $_getal % 2 == 0)
        {
            echo "<th>";
        }
        if ($_getal % 2 == 0) 
        {
            echo "$_getal<br>";
            $temp++;
        }
        if($temp == 5 && $_getal % 2 == 0)
        {
            $temp = 0;
             echo "</th>";
        }
    }

?>

<强>输出enter image description here

答案 2 :(得分:0)

<?php
echo '<table border="1">';
for ($_getal = 0; $_getal < 100; $_getal+=2) {
    echo "<tr><td>$_getal</td></tr>";
}
echo '</table>';
?>

答案 3 :(得分:0)

您应该决定所需的行数(在您的示例中,您有5行)。

第一列(0)的第一个数字和第二列(10)的第一个数字之间的差异是行数(5)乘以2

你的for循环遍历每个数字,但是:如果你只是做$_getal+=2

,它的内存效率更高
<?php
$_maxNumber = 100;
$_numberStep = 2;
$_numberOfRows = 5;

echo '<table border=1><tr>';

for ($_startGetal = 0; $_startGetal < $_maxNumber; $_startGetal += $_numberStep * $_numberOfRows) {
    echo '<td>';

    for ($_getal = $_startGetal; $_getal < $_startGetal + $_numberStep * $_numberOfRows; $_getal += $_numberStep) {
        echo "$_getal<br>";
    }

    echo '</td>';
}

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

我添加了2作为变量,因此您可以执行任何数字的步骤。

答案 4 :(得分:0)

<?php
echo "<table border=1 style='width:20%;border: 2px solid #101216;border-collapse: collapse;'>";
    $temp = 0;
    for ($_getal = 0; $_getal < 100; $_getal++) 
    {
        if($temp == 0 && $_getal % 2 == 0)
        {
            echo "<td style='text-align:center;'>";
        }
        if ($_getal % 2 == 0) 
        {
            echo "$_getal<br>";
            $temp++;
        }
        if($temp == 5 && $_getal % 2 == 0)
        {
            $temp = 0;
             echo "</td>";
        }
    }
echo "</table>";

?>

答案 5 :(得分:0)

这是一种做法。

我创建了一个0-8范围内的数组,这是表格中最左边的数字 然后我循环它们,我循环#34;数十&#34;在for循环中。
我将数字和单个数字放在彼此旁边并乘以1使其成为整数(删除前导零)。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="breadcrumb"></div>

如果您将for循环更改为$numb = range(0,8, 2); echo "<table border=1>"; foreach($numb as $val){ echo "<tr>"; for($tens=0;$tens<=9; $tens++){ echo "<td>" . ($tens.$val)*1 . "</td>"; } echo "</tr>\n"; } echo "</table>"; ,它将计为198而不是