php - In my table, colour is appearing only on the even rows


Keywords:php 


Question: 

When I try to get data from database I want to change the colour for even and odd rows. But only the colour of the even rows is appearing on the table. I have defined different colours for even and odd rows. But the odd row's colour is not appearing. My code is shown below.

Any suggestion will be appreciated

   <head><style type="text/css">
      .colr tr:nth-child(odd){
        background-color: #4286f4; }
      .colr tr:nth-child(even){
        background-color: #92f441;}
    </style>
     </head>
    <body>                   
         <body>
          <?php 
    $con=@mysql_connect("localhost","root","")or die(mysql_error());
    $db=@mysql_select_db("portal",$con) or die(mysql_error());
    echo "<div class='table-users'>
       <div class='header'>Applicants</div>

       <table cellspacing='0'>
          <tr>
    <th >ID </th>
    <th>Application for</th>
    <th>Name</th>
    <th>Date Of Birth</th>
    <th>Qualification</th>
    <th>Passing Year</th>
       </tr> </table>
    </div>";

     $sql='SELECT * FROM tbl_applicantinfo ';
     $sql1=mysql_query('Select * FROM tbl_academic');
    $retval = mysql_query( $sql, $con );
    if(! $retval )
    {
      die('Could not get data: ' . mysql_error());
    }
    $selected=$_GET['aap_position'];

    if($_GET['aap_position']=="all"){

      $sql=mysql_query('SELECT * FROM tbl_applicantinfo;
     echo "<div class='table-users'>
     ";
      while ($row=mysql_fetch_array($sql)){

 echo "<div >
         <table cellspacing='0' class='colr'>
         <tr> 
            <td >{$row['SrNo']}</td>
    <td>{$row['position']}</td>
    <td>{$row['applicantname']}</td>
    <td>{$row['date_birtth']}</td>
    <td>{$row['degree']}</td>
    <td>{$row['year_passing']}</td>
    </tr> </table></div>";
      }}
    mysql_close($con); 
    ?> </body>

1 Answer: 

Try this one: (start table before while loop, only <tr> will be in while loop, if you put table in your while loop then every time a new table generated, then only one css (for ODD or EVEN tr) will applied)

   <head><style type="text/css">
      .colr tr:nth-child(odd){
        background-color: #4286f4; }
      .colr tr:nth-child(even){
        background-color: #92f441;}
    </style>
     </head>
    <body>                   
         <body>
          <?php 
    $con=@mysql_connect("localhost","root","")or die(mysql_error());
    $db=@mysql_select_db("portal",$con) or die(mysql_error());
    echo "<div class='table-users'>
       <div class='header'>Applicants</div>

       <table cellspacing='0'>
          <tr>
    <th >ID </th>
    <th>Application for</th>
    <th>Name</th>
    <th>Date Of Birth</th>
    <th>Qualification</th>
    <th>Passing Year</th>
       </tr> </table>
    </div>";

     $sql='SELECT * FROM tbl_applicantinfo ';
     $sql1=mysql_query('Select * FROM tbl_academic');
    $retval = mysql_query( $sql, $con );
    if(! $retval )
    {
      die('Could not get data: ' . mysql_error());
    }
    $selected=$_GET['aap_position'];

    if($_GET['aap_position']=="all"){

      $sql=mysql_query('SELECT * FROM tbl_applicantinfo;
     echo "<div class='table-users'>
     <div ><table cellspacing='0' class='colr'>";
      while ($row=mysql_fetch_array($sql)){

 echo "<tr> 
            <td >{$row['SrNo']}</td>
    <td>{$row['position']}</td>
    <td>{$row['applicantname']}</td>
    <td>{$row['date_birtth']}</td>
    <td>{$row['degree']}</td>
    <td>{$row['year_passing']}</td>
    </tr> ";
      } echo "</table></div>"; }
    mysql_close($con); 
    ?> </body>