2

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.

Here is the code I have until now, thanks for help.

<!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>
  • havent really looks at your code but my arrprach would be something like `foreach (range('a', 'z') as $letter) {` –  Mar 29 '15 at 20:02

2 Answers2

1

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>");

UPDATED:

Now vertical, try this...

Edwin Krause
  • 1,766
  • 1
  • 16
  • 33
  • The $number variable did exactly what I wanted, it was the vertical numbering, which you deleted so now it's not what I wanted. Yeah it's alphabet but now VERTICAL :/ However, thank you for help – David Pilař Mar 29 '15 at 20:42
  • Can you help me with another thing? It's quite useless but what if I want to edit the script to echo the alphabet from A to Z, then from Z to A, than again from A to Z... You know what I mean. – David Pilař Mar 30 '15 at 17:23
  • Yeah it seems quite useless, but then again, I also didn't know the use of your other function... – Edwin Krause Mar 30 '15 at 17:47
  • I got the function, where is your related question so I can post the answer...? – Edwin Krause Mar 30 '15 at 17:56
  • You can send it here → pilka91@outlook.cz To describe what's that for, I study IT and creating websites, PHP and so on. Teacher give me these "useless" things to practise this, so it seems useless but I really appreciate the code of othere people so I can learn from it, learn from my mistakes and be even better :) – David Pilař Mar 30 '15 at 18:04
  • Create a new question, and I post the answer, maybe other people like to know as well. I need more reputation here :-) – Edwin Krause Mar 30 '15 at 18:07
  • Here you go ;) → http://stackoverflow.com/questions/29353301/php-alphabet-table-a-to-z-z-to-a – David Pilař Mar 30 '15 at 18:15
0

Because $number always grown up.
The first A-Z, $number is between 0 and 25, you go in the else case and it is ok.
The second A-Z, $number is between 26 and 51, you go in the if case, you remove 26 and your print is ok.

Next $number is at 52, as previously, you go in the if case and try to print the 27th letter of alphabet ^^

Ludeau
  • 1