0

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>
  • Please make an example of the expect output. Also show what you already have tried – Rizier123 Mar 30 '15 at 18:16
  • So writing some code is always a good starting point.... a couple of hints: `$alphabet = range('a', 'z');` `foreach($alphabet as $letter) {..}` and `$alphabet = array_reverse($alphabet);` – Mark Baker Mar 30 '15 at 18:17
  • Well doesn't this seems pretty similar to this: http://stackoverflow.com/q/29333975/3933332 don't you think too? – Rizier123 Mar 30 '15 at 18:22

1 Answers1

1

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!

Edwin Krause
  • 1,766
  • 1
  • 16
  • 33
  • And **what** did you changed **where** and **why** did you changed it? (Just maaaybe helpful for OP and further readers) – Rizier123 Mar 30 '15 at 18:23
  • WOW! For real: http://stackoverflow.com/questions/29333975/php-vertical-alphabet-using-for-cycle#comment46891492_29334472 You just promoted OP to ask a new question so you can answer it and get more reputation ?! – Rizier123 Mar 30 '15 at 18:29
  • Hey Rizier, sorry if I've done something wrong, I just asked for a solution connected with my previous question, I suggested to send it by email but Edwin told me to create new question to get more reputation. My apologize again :/ – David Pilař Mar 30 '15 at 18:46
  • @DavidPilař I think it's more from Edwin Krause that he promoted you to ask a new question for his own advantage. (BTW: But at the end you decide if you want to ask a new question and maybe to help you to decide easier for next time, see: http://meta.stackexchange.com/a/10245) – Rizier123 Mar 30 '15 at 18:52
  • To be fair here... it is a new and different question. Answering it is not a crime and there is no harm done in giving me a few more points in reputation. Gimme a break. - for my short answer... I was in a rush, just leaving the house, I will add an explanation tomorrow. I'm going to sleep now :-) Buenas noches, amigos! – Edwin Krause Mar 30 '15 at 23:05