0

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>
clemens
  • 16,716
  • 11
  • 50
  • 65
  • this cant be real code its invalid –  Nov 21 '17 at 05:22
  • i have shortened it for the post.Real problem is with css and every other thing is working fine – Hassan Rana Nov 21 '17 at 05:25
  • @HassanRana Try https://stackoverflow.com/questions/47405708/in-my-table-only-even-rows-color-appear-on-whole-table/47405838#47405838 – helpdoc Nov 21 '17 at 05:26
  • Problem is not solved i am just telling @nogad that there is no problem with php code only css has problem.That means only even row color appear on whole table – Hassan Rana Nov 21 '17 at 05:29
  • @HassanRana If this is a problem with css - you should create snippet with generated page – fen1x Nov 21 '17 at 05:36
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Dec 25 '17 at 14:58

1 Answers1

0

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>
helpdoc
  • 1,910
  • 14
  • 36