简体   繁体   中英

PHP Alphabet table - A to Z, Z to A

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 then from Z back to A, when it comes to A it will do again from A to Z and from Z to A.

My code until now is only from A to Z and then again from A to Z.

<!DOCTYPE html>
<html>

<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <title>Vertikální abeceda</title>
</head>

<body>
    <form method="post">
        <input type="number" placeholder="SLOUPCE" name="sloupce" />
        <input type="number" placeholder="ŘÁDKY" name="radky" />
        <input type="submit" value="Vytvořit tabulku" /><br><br>
    </form>

        <?php
            if(isset($_POST['radky']) && isset($_POST['sloupce'])) {
                $col = $_POST['sloupce'];
                $row = $_POST['radky'];
                $cislo = 0;

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

                for($i = 1; $i<=$row; $i++) {
                    echo ("<tr>");
                    for($c = 0; $c<$col; $c++) {
                        $pismeno_id = 64;
                        $cislo = $i + ($c*$row);
                        $pismeno = $cislo + $pismeno_id;
                        while($pismeno > 90) {
                            $pismeno = $pismeno - 26;
                        }
                        echo ("<td>". "&#" . $pismeno . "</td>");
                    }
                    echo ("</tr>");
                }
                echo ("</table>");
            }
        ?>
</body>
</html>

Here we go: To add the backwards alphabet to the cicle I changed the maximum number of $letter to 115. Thats 65(value of A) + 26(full alphabet) + 24 (X - B).

To accomplish the backwards Alphabet, I added variable $realletter, which remains the same as $letter up to 90. From 91 to 115, it counts backwards, by substracting the amount higher than 90 from 90. That would be 90 - ($letter - 90), ie. for 94 -> 90 - (94 - 90) = 90 - 4 = 86.

<?php
$row = 26;
$col = 26;
echo ("<table rules='all'>");

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

echo ("</table>");
?>

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM