【问题标题】:How to use PHP to expand columns and rows of a table?如何使用 PHP 扩展表格的列和行?
【发布时间】:2021-07-27 07:58:42
【问题描述】:

我正在尝试编写代码来扩展和收缩 PHP 中乘法表中的行和列。这是我目前拥有的代码,我认为可以扩展和收缩行和列:

<?php
$row=4;
$col=4;
if(isset($_GET["row"]))
{
    $row=$_GET["row"];
}
if(isset($_GET["col"]))
{
    $col=$_GET["col"];
}
?>
<html style="background-color: #90A7E8;">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <html lang="en">
<head>
<body>
<ul>
<?php
echo "<table>";
for($r=1;$r<$row+1;$r++){
    echo "<tr>";
    for($c=1;$c<$col+1;$c++){
        $p=$c*$r;
        //~ echo "<td>";
        echo "<td>$p</td>";
        //~ echo "</td>";
        }
    echo "</tr>";
}
echo "</table><br>";
$row++;
echo "</ul><a href='math.php?row=$row?col=$col'>more rows</a><br>";
$row-=1;
echo "<a href='math.php?row=$row?col=$col'>less rows</a><br>";
$col++;
echo "<a href='math.php?row=$row?col=$col'>more columns</a><br>";
$col-=1;
echo "<a href='math.php?row=$row?col=$col'>less columns</a><br>";
?>
</body>
</html>

当我运行代码时,我不断收到此错误以及无法扩展和收缩列。我在这行代码中得到一个格式不正确的数值错误:for($r=1;$r&lt;$row+1;$r++)

当我尝试扩展和收缩列时,它只会收缩行。

总而言之,我有一个乘法表,我想仅使用 PHP 来扩展和收缩行和列。当我运行上面指定的代码时,我在上面指定的行上收到一条错误消息“遇到格式不正确的数值”,并且无法扩展或收缩列。有谁能帮帮我吗?

【问题讨论】:

    标签: php html html-table


    【解决方案1】:

    从您的格式不正确的数值开始$_GET 中的值都是字符串。您需要将这些字符串转换为整数。

    $row=(int)$_GET["row"];
    

    在您的链接中,查询字符串的格式不正确。查询字符串以问号开头,键/值对由 & 号分隔 (&amp;)。所以你得到

    echo "</ul><a href='math.php?row=$row&col=$col'>more rows</a><br>";
    

    最后,计算新行数和新列数的代码仅适用于增加这些值,因为减少它们的计算只是反转了应用于前一行的增加。

    $row 开始设置为 4:

    $row++;    // $row = 5
    echo ...   // More rows
    $row -=1   // $row is now 4
    echo ...   // Fewer rows.
    

    有很多方法可以解决这个问题。一种方法是为新的行数和列数使用一个新变量。

    $row 开始设置为 4:

    $newRow = $row+1;    // $newRow = 5
    echo ...             // More rows
    $newRow = $row-1     // $newRow is now 3
    echo ...             // Fewer rows.
    

    把它们放在一起给出:

    <?php
    
    $row=4;
    $col=4;
    if(isset($_GET["row"]))
    {
        $row=(int)$_GET["row"];
    }
    if(isset($_GET["col"]))
    {
        $col=(int)$_GET["col"];
    }
    ?>
    <html style="background-color: #90A7E8;">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <html lang="en">
    <head>
    <body>
    <ul>
        <?php
        echo "<table>";
        for($r=1;$r<$row+1;$r++){
            echo "<tr>";
            for($c=1;$c<$col+1;$c++){
                $p=$c*$r;
                //~ echo "<td>";
                echo "<td>$p</td>";
                //~ echo "</td>";
            }
            echo "</tr>";
        }
        echo "</table><br>";
        $newRow = $row+1;
        echo "</ul><a href='math.php?row=$newRow&col=$col'>more rows</a><br>";
        $newRow = $row-1;
        echo "<a href='math.php?row=$newRow&col=$col'>less rows</a><br>";
        $newCol = $col+1;
        echo "<a href='math.php?row=$row&col=$newCol'>more columns</a><br>";
        $newCol = $col-1;
        echo "<a href='math.php?row=$row&col=$newCol'>less columns</a><br>";
        ?>
    </body>
    </html>
    
    

    【讨论】:

      猜你喜欢
      • 2014-11-29
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多