How to make chessboard layers using PHP

2

ChessBoard Layers using PHP

In this tutorial we are going to learn how to write PHP codes for making ChessBoard like layers. We can create Chess Board as well as any number of Chess Board layers as per user input. So lets go and start our tutorial.

We are going to use ” For loops ” and ” If else ” command for writing this code.

First Part of the code is CSSĀ 

input[type=text]{width:400px;padding:5px;}
table{margin:auto;}
td{border:1px solid #000;width:40px;height:40px;}
td.black{background-color:#000;}
.box{text-align:center;border:1px solid #000;border-radius:4px;padding:30px;background-color:#e5dcdc;}

 

Second Part of the code is PHP

<?php

if(!empty($_POST) and !empty($_POST['howmany']))
{
    $x = trim($_POST['howmany']);

$tdcount = 0;
$rowcount = 0;
echo '<table>';	
    for($k=1; $k<=$x; $k++)
    {
        $rowcount++;
        echo '<tr>';
        for($i=1; $i<=8; $i++)
        {
            if($rowcount % 2 == 0)
            {
                $tdcount++;
                if($tdcount % 2 == 0)
                {
                    echo '<td class="black"></td>';
                }
                else
                {
                    echo '<td></td>';
                }
            }
            else
            {
                $tdcount++;
                if($tdcount % 2 == 0)
                {
                    echo '<td></td>';
                }
                else
                {
                    echo '<td class="black"></td>';
                    
                }				
            }
        }
        echo '</tr>';
    }
echo '</table>';
}
?>

 

Third Part of the code is HTML

 

<div class="box">

<h2>Make Chess Board Type Table</h2>
<form action="" method="post">
    <input type="text" placeholder="How Many Chess Board layers you want ?" name="howmany" /><br />
    <input type="submit" value="Make Table" />
</form>

</div>

 

Little Explanation how this code works :

When you see HTML code it is making a form where user can input how many chess board layers he want to see. Suppose he entered 3 then php code will give out of 3 rows x 8 columns but chess like structure. As show in picture below :

 

chessboard layer

So in this tutorial you will get whole concept of making layers like Chess Board.

Live Demo for the above codes : https://websitecoder.in/guideblog/chessboard/

 

Do you love PHP Tutorials ?

 

2 thoughts on “How to make chessboard layers using PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

  +  78  =  87