Thursday, 14 July 2016

How to make Zebra Stripes Table with PHP Mysql

  

If you wanted to learn something like how to create html table zebra stripes from data which are fetch from mysql database by using PHP Language. Zebra stripes html table will increase the visibility of your data which you have display on web page. I have simple fetch data from mysql table by using PHP programming and display in html table but in zebra stripe format. From this format of html table user will not confused between two row of data this is becase both row color will be different. Here for making zebra stripes html table I have not used any Jquery or Javascript code or even I have not used any css odd or even propery but I have simple use php code logic for creating zebra stripes html table. It will be my pleasure you have something learn from this post regarding how to use php programming with mysql database for creating Zebra stripes html table.

Source Code

Employee Table


 --  
 -- Table structure for table `tbl_employee`  
 --  
 CREATE TABLE IF NOT EXISTS `tbl_employee` (  
  `id` int(11) NOT NULL AUTO_INCREMENT,  
  `name` varchar(50) NOT NULL,  
  `gender` varchar(10) NOT NULL,  
  `designation` varchar(100) NOT NULL,  
  PRIMARY KEY (`id`)  
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;  
 --  
 -- Dumping data for table `tbl_employee`  
 --  
 INSERT INTO `tbl_employee` (`id`, `name`, `gender`, `designation`) VALUES  
 (1, 'Micheal Bruce', 'Male', 'System Architect'),  
 (5, 'Clara Gilliam', 'Female', 'Programmer'),  
 (6, 'Barbra K. Hurley', 'Female', 'Service technician'),  
 (7, 'Antonio J. Forbes', 'Male', 'Faller'),  
 (8, 'Charles D. Horst', 'Male', 'Financial investigator'),  
 (9, 'Beau L. Clayton', 'Male', 'Extractive metallurgical engin'),  
 (10, 'Ramona W. Burns', 'Female', 'Electronic typesetting machine operator'),  
 (11, 'Jennifer A. Morrison', 'Female', 'Rigging chaser'),  
 (12, 'Susan M. Juarez', 'Female', 'Control and valve installer'),  
 (13, 'Ellan D. Downie', 'Female', 'Education and training manager'),  
 (14, 'Larry T. Williamson', 'Male', 'Teaching assistant'),  
 (15, 'Lauren M. Reynolds', 'Female', 'Internet developer'),  
 (16, 'Joseph L. Judge', 'Male', 'Refrigeration mechanic'),  
 (17, 'Eric C. Lavelle', 'Male', 'Model'),  
 (18, 'Cheryl T. Smithers', 'Female', 'Personal banker'),  
 (19, 'Tonia J. Diaz', 'Female', 'Facilitator'),  
 (20, 'Stephanie P. Lederman', 'Female', 'Mental health aide'),  
 (21, 'Edward F. Sanchez', 'Male', 'Marine oiler');  

zebra-stripe.php


 <?php  
 //How to make Zebra Stripe Table with PHP Mysql  
 function zebra_stripes_table()  
 {  
      $output = '';  
      $host = "localhost";  
      $username = "root";  
      $password = '';  
      $database = "testing";  
      try  
      {  
           $connect = new PDO("mysql:host=$host;dbname=$database", $username, $password);  
           $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
           $statement = $connect->prepare("SELECT * FROM tbl_employee");  
           $statement->execute();  
           $number_of_row = $statement->rowCount();  
           $output .= '  
           <table width="100%" border="1" cellpadding="5" cellspacing="0">  
                <tr bgcolor="#ddd">  
                     <th>ID</th>  
                     <th>Name</th>  
                     <th>Gender</th>  
                     <th>Designation</th>  
                </tr>  
           ';  
           if($number_of_row > 0)  
           {  
                $count = 0;  
                foreach($statement as $row)  
                {  
                     $class = '';  
                     if($count%2)  
                     {  
                          $class = 'bgcolor="#ddd"';  
                     }  
                     else   
                     {  
                          $class = 'bgcolor="#ffffff"';  
                     }  
                     $count++;  
                     $output .= '  
                     <tr '.$class.'>  
                          <td>'.$row["id"].'</td>  
                          <td>'.$row["name"].'</td>  
                          <td>'.$row["gender"].'</td>  
                          <td>'.$row["designation"].'</td>  
                     </tr>  
                     ';  
                }  
           }  
           else  
           {  
                $output .= '  
                <tr>  
                     <td colspan="4">No Data found</td>  
                </tr>  
                ';  
           }  
           $output .= '</table>';  
           return $output;  
      }  
      catch(PDOException $error)  
      {  
           return $error->getMessage();  
      }  
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial | How to make Zebra Stripe Table with PHP Mysql</title>  
           <style>  
           .container{  
                width:600px;  
                margin:0 auto;  
           }  
           </style>  
      </head>  
      <body>  
           <br />  
           <div class="container">  
                <h3 align="center">How to make Zebra Stripe Table with PHP Mysql</h3><br />  
                <?php echo zebra_stripes_table(); ?>  
           </div>  
      </body>  
 </html>  

0 comments:

Post a Comment