PHP垂直字母使用FOR循环 [英] PHP Vertical Alphabet Using FOR Cycle

查看:85
本文介绍了PHP垂直字母使用FOR循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP编写脚本,该脚本将生成一个带有垂直字母的表格。它将简单地将字母从A回显到Z,当涉及到Z时,它将重置并再次从A开始。我有一个问题,因为我只能重复两次,然后所有单元格中都有一些不需要的信号。我正在使用其ASCII html代码回显该字母,其中A符号为&#65,Z符号为&#90。

I'm trying to do a script in PHP which will generate a table with vertical alphabet in it. It will simply echo letters from A to Z and when it comes to Z, it will reset and begin from A again. I have a problem with this because I only can repeat this twice, then all cells have some unwanted signs in them. I'm echo-ing the letter using their ASCII html codes, where the A sign is &#65 and the Z sign is &#90.

<!DOCTYPE html>
<html>

<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <title>Vertical alphabet</title>
</head>
<body>
    <form method="post">
        <input type="number" placeholder="COLUMNS" name="cols" />
        <input type="number" placeholder="ROWS" name="rows" />
        <input type="submit" value="Create table" /><br><br>
    </form>

        <?php
            if(isset($_POST['rows']) && isset($_POST['cols'])) {
                $col = $_POST['cols'];
                $row = $_POST['rows'];

                echo ("<table rules='all'>");

                for($i = 1; $i<$row+1; $i++) {
                    echo ("<tr>");
                    for($c = 0; $c<$col; $c++) {
                        $letter_id = 65;
                        $number = ($i + ($c*$row)-1);
                        $letter = $number + $letter_id;
                        if($letter > 90) {
                            $number = $number - 26;
                            $letter = $letter - 26;
                            echo ("<td>". "&#" . $letter. "</td>");
                        } else {
                            echo ("<td>". "&#" . $letter. "</td>");
                        }
                    }
                    echo ("</tr>");
                }
                echo ("</table>");
            }
        ?>
</body>
</html>


推荐答案

不确定您要使用 $ number 变量,但这就是这里的问题

Not sure what you're trying to with the $number variable, but that's the issue here

$number = 0;

echo ("<table rules='all'>");

for($i = 1; $i<=$row; $i++) {
     echo ("<tr>");
     for($c = 0; $c<$col; $c++) {
          $letter_id = 65;
          $number = $i + ($c*$row);
          $letter = $number + $letter_id;
          while($letter > 90) {
                $letter = $letter - 26;
          }
          echo ("<td>". "&#" . $letter. "</td>");
     }
     echo ("</tr>");
}

echo ("</table>");

已更新:

现在垂直,尝试这个...

Now vertical, try this...

这篇关于PHP垂直字母使用FOR循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆