Creating Colorful Table using HTML and PHP – Programs for Beginners

1
444
table using html and php

Creating Table using HTML and PHP

Here’s another PHP program for Beginners on Programmingy. Are you ready to learn how to create a colorful table using HTML and PHP. Here you have to create a program – to take 3 user inputs. One: Number of rows, Another: Number of Columns and last you have to accept background colour for the table. Then Create a table according to given data by user and print each cell number of table in each cell.

First File – a.php

table using html and php

Here is just some basic HTML coding. Make a form, with 3 textboxes and 1 submit button. We will take Number of rows, number of columns and color of the table from user. We will use ‘GET’ Method to send value to our PHP file.

<html>
<head>PHP Programs</head>
<body>
<form name=”frm1″ method=”get” action=”rowcol.php”>
<input type=”text” placeholder=”Enter Number of rows” name=”row”>
<br>
<input type=”text” placeholder=”Enter Number of cols” name=”col”>
<br>

<input type=”text” placeholder=”background color ” name=”clr”>
<br>
<input type=”submit”>
</form>
</body>
</html>

 

Table Using HTML and PHP: Second File

table using html and php

In our first file, we simply made a form to take user inputs. Now we have to create a table using HTML and PHP here. Recieve all 3 inputs and store it in $row, $col, and $clr variables. Make another variable $cell for keeping the count of cell numbers (Make sure you initialize it with 1).

After all that, just start looping for making table. One thing you should know here is: How to Use HTML in PHP. To use HTML tags you simply use echo ” <TAG_NAME> “. For example to use new-line-tag (that is <BR>), all you have to do is write: echo “<BR>”;

<?php

$row=$_GET[“row”];
$col=$_GET[“col”];
$clr=$_GET[“clr”];
$cell=1;
echo “<table border=1 style=background-color:$clr>”;
for($i=1;$i<=$row;$i++){
echo “<tr>”;
for($j=1;$j<=$col;$j++){
echo “<td>$cell</td>”;
$cell++;
}
echo “</tr>”;
}

echo “</table>”;
?>

LEARN: Factorial Program by Taking Input in PHP 

Hope you learnt to create Table using HTML and PHP from the article. If you want some specific program in PHP Programming do comment below and share your views about our article below. Keep Coding!

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here