Home php Draw a simple PHP Chess Board

Draw a simple PHP Chess Board

Author

Date

Category

So I implement my board (the size of the board is taken from the incommation), but I do not know how to make the output of this board on the screen, please tell me.

& lt;! doctype HTML & GT;
& lt; html & gt;
& lt; Head & gt;
  & lt; Meta Charset = "UTF-8" & GT;
& lt; / Head & gt;
& lt; Body & gt;
& lt; Form Method = "Get" & gt;
  & lt; p & gt;
    & lt; Input Type = "text" name = "height" & gt;
  & lt; / p & gt;
  & lt; p & gt;
    & lt; Input Type = "Text" name = "width" & gt;
  & lt; / p & gt;
  & lt; Input Type = "Submit" & gt;
& lt; / form & gt;
& lt; Table Border = '1' id = "Chessboard" & gt; & lt; / table & gt;
& lt;? php
$ table = '';
$ width = $ _get ['width'];
$ Height = $ _Get ['Height'];
For ($ i = 1; $ i & lt; = $ width; $ i ++) {
  $ table + = '& lt; TR & GT;';
  For ($ j = 1; $ j & lt; = $ Height; $ j ++) {
    $ color = ($ i + $ j)% 2? 'White': 'Black';
    $ table + = '& lt; TD BGColor = "'. $ Color. '" width = "20"
    Height = "20" & gt; & lt; / td & gt; ';
  }
  $ table + = '& lt; / tr & gt;';
}
? & gt;
& lt; / body & gt;
& lt; / html & gt;

Answer 1, Authority 100%

That’s for example.

& lt; table border = '1' id = "chessboard" & gt;
& lt;? php
$ table = '';
$ width = intval ($ _ get ['width']);
$ height = intval ($ _ get ['height']);
For ($ i = 1; $ i & lt; = $ width; $ i ++) {
  $ table. = '& lt; TR & GT;';
  For ($ j = 1; $ j & lt; = $ Height; $ j ++) {
    $ color = ($ i + $ j)% 2? 'White': 'Black';
    $ table. = "& lt; TD BGColor = '{$ Color}' width = '20 'height =' 20 '& gt; & lt; / td & gt;";
  }
  $ table. = '& lt; / tr & gt;';
}
Echo $ table;
? & gt;
& lt; / Table & gt;

Answer 2, Authority 100%

I know that the code is not optimal, I apologize in advance. The code was written to implement the following example:

 Enter a description of the image

& lt;! doctype HTML & GT;
& lt; html & gt;
& lt; Head & gt;
  & lt; Title & gt; & lt; / title & gt;
& lt; / Head & gt;
& lt; Body & gt;
  & lt; table width = "400px" CellSpacing = "1px" CellPadding = "0px" border = "0px" & gt;
    & lt;? php
      $ airlhabet = array ("a", "b", "c", "d", "e", "f", "g", "h", "h", "g", "f", " E "," d "," c "," b "," a ");
      $ numbers = Array ("1", "8", "2", "7", "3", "6", "4", "5", "5", "4", "6", " 3 "," 7 "," 2 "," 8 "," 1 ");
      $ alpha = -1;
      $ nums = -1;
      for ($ row = 0; $ row & lt; 10; $ row ++)
      {
        Echo "& lt; TR & GT;";
        for ($ col = 0; $ col & lt; 10; $ col ++)
        {
          if ($ row == 0 or $ row == 9)
          {
            if ($ col == 0 or $ col == 9) {
              echo "& lt; td height = 25px width = 25px bgcolor = # 686868 & gt; & lt; / td & gt;";
            }
            ELSE {
              $ alpha ++;
              echo "& lt; TD Height = 25px width = 25px bgcolor = # 686868 Align = 'Center' & gt; $ APLHABET [$ alpha] & lt; / td & gt;";
            }
          }
          ELSE {
            if ((($ row & gt; = 1 and $ row & lt; 9) and ($ col == 0 or $ col == 9)) {
              $ nums ++;
              Echo "& lt; TD Height = 25px width = 25px BGColor = # 686868 Align = 'CENTER' & GT; $ numbers [$ nums] & lt; / td & gt;";
            }
            Elseif ($ ROW% 2! = 0) {
              if ($ col% 2! = 0) {
                Echo "& lt; TD Height = 40px width = 40px bgcolor = # fffff & gt; & lt; / td & gt;";
              }
              ELSE {
                Echo "& lt; TD Height = 40px width = 40px bgcolor = # 000000 & gt; & lt; / td & gt;";
              }
            }
            ELSEIF ($ ROW% 2 == 0) {
              if ($ col% 2 == 0) {
                Echo "& lt; TD Height = 40px width = 40px bgcolor = # fffff & gt; & lt; / td & gt;";
              }
              ELSE { 
Echo "& lt; TD Height = 40px width = 40px bgcolor = # 000000 & gt; & lt; / td & gt;";
               }
             }
           }
         }
         Echo "& lt; / tr & gt;";
       }
     ? & gt;
   & lt; / Table & gt;
& lt; / body & gt;
& lt; / html & gt;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions