HTML table alternate row color using PHP
In this post, you will learn how to display HTML table alternate row color using the PHP programming language. The alternate row colour helps to improve the visual representation of the web page. This appearance is mostly preferred because it helps to understand the data.
In the given example, we are using the PHP programming language to select data from MySQL and display it in a tabular form. Suppose we have the following 'employee' table.
CREATE TABLE IF NOT EXISTS `employee` (
`emp_id` int(11) NOT NULL AUTO_INCREMENT,
`emp_name` varchar(150) NOT NULL,
`email` varchar(150) NOT NULL,
`phone` varchar(100) NOT NULL,
`created_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`is_enabled` int(11) NOT NULL,
PRIMARY KEY (`emp_id`)
)
INSERT INTO `employee` (`emp_id`, `emp_name`, `email`, `phone`, `created_date`, `is_enabled`) VALUES
(1, 'John', This email address is being protected from spambots. You need JavaScript enabled to view it. ', '2323234543', '2019-06-05 15:36:07', 1),
(2, 'Smith', This email address is being protected from spambots. You need JavaScript enabled to view it. ', '9898577442', '2019-05-14 15:36:07', 1),
(3, 'Priska', This email address is being protected from spambots. You need JavaScript enabled to view it. ', '9393452387', '2019-05-28 15:36:07', 1),
(4, 'Gaga', This email address is being protected from spambots. You need JavaScript enabled to view it. ', '8482764537', '2019-06-29 15:36:07', 1);
config.php
Now, we have written database connection code to fetch data from MySQL. Make sure to replace 'hostname', 'username', 'password' and 'databasename' with your database credentials and name.
$conn = new mysqli('hostname', 'username', 'password', 'databasename');
//Check for database connection error
if($conn->connect_error){
die("Error in DB connection: ".$conn->connect_errno." : ".$conn->connect_error);
}
index.php
Here is the main PHP file, 'index.php', that we will call in the browser. At the top of this page, the 'config.php' file has been added.
<?php
require_once 'config.php';
?>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<style type="text/css">
.even {
background-color: #66CCFF !important;
}
.odd {
background-color: #FFB85F !important;
}
</style>
</head>
<body>
<table>
<?php
$styles = array('even','odd');
$select = "SELECT * FROM employee ";
$result = $conn->query($select);
$i = 1;
if($result->num_rows > 0){
echo '<table class="table table-striped">';
echo '<tr><th>Employee Name</th>';
echo '<th>Email</th>';
echo '<th>Phone</th>';
echo '<th>Created Date</th>';
echo '</tr>';
while($row = $result->fetch_object()){
?>
<tr class="<?php echo $styles[$i % 2]; ?>">
<td><?php echo htmlentities($row->emp_name); ?></td>
<td><?php echo htmlentities($row->email); ?></td>
<td><?php echo htmlentities($row->phone); ?></td>
<td><?php echo htmlentities($row->created_date); ?></td>
</tr>
<?php
$i++;
}
} ?>
</table>
</body>
</html>
In the above file, we have taken two CSS classes, 'even' and 'odd' and defined the background colour. By implementing the logic through PHP code, we have added an alternate background colour on HTML Table rows.
Related Articles
Remove duplicates from array PHPPHP remove last character from string
PHP sanitize input for MySQL
PHP random quote generator
PHP String Contains
PHP calculate percentage of total
PHP Fix: invalid argument supplied for foreach
Locking files with flock()
How to get current directory, filename and code line number in PHP
PHP SplFileObject Standard Library
How to lock a file using PHP
How to upload multiple files and store in MySQL database using PHP
Create And Download Word Document in PHP
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP