简体   繁体   中英

Coloring the table with alternate color

Below i have added my css file and my html file i need to color my table with alter name color but its not working i don't know where i have done the mistake.

CSS FILE :-

table.gridtable{
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    float:center;
    border-width:1px;
    border-style:groove;
    border-color:black;
    border-collapse:collapse;
    width:600px;
    }
    table.gridtable th{
    border-width:1px;
    padding:3px;
    border-style:solid;
    border-color:black;
    }
    table.gridtable td{
    border-width:1px;
    padding:3px;
    border-style:solid;
    border-color:black;
    }
    .oddrowcolor{
    background-color: black;
    }
    .evenrowcolor{
    background-color: red;
    }
    table.gridtable td.darkcol{
    border-top:0px;
    border-bottom:0px;
    border-right: 1px;
    }
    table.gridtable td.emptycol{
    border-bottom:0px;
    border-right: 1px;
    }

HTML & PHP:-

 <table class="gridtable" id="alternatecolor" align="center"
 <tr><th>disease Name/th><th>Gene Name/th><th>OMIM Number</th></tr>  
 <?php
    $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
    $selected = mysql_select_db("missensencemuttation",$dbhandle) or die("Could not select disease");
    $result = mysql_query("SELECT DISTINCT * FROM `gene_data` ORDER BY disease_name");
        $last=" ";
    while($row = mysql_fetch_array($result))    

        $now=$row[2];
        if($last != $now){
                $rcnt=mysql_query("select count(gene_name) from gene_data where gene_name='$now'");
        if($rcnt==1){
        echo "<tr><td>";   
        echo "<a href='d1.php?d_name=".$row['disease_name']."'>".$row['disease_name'];
        echo "</a></td>";       
        echo "<td>".$row['gene_name']."</td>";
        echo "<td>".$row['ommi_number']."</td></tr>";
        }
        else{
        echo "<tr><td class='emptycol'>";   
        echo "<a href='d1.php?d_name=".$row['disease_name']."'>".$row['disease_name'];
        echo "</a></td>";       
        echo "<td>".$row['gene_name']."</td>";
        echo "<td>".$row['ommi_number']."</td></tr>";
        }
        }
        else
        {
        echo "<tr'>";
        echo "<td class='darkcol'>";   
        echo '&nbsp';
        echo "</td>";       
        echo "<td>".$row['gene_name']."</td>";
        echo "<td>".$row['ommi_number']."</td></tr>";
        }
        $last=$row[2];      

can any one help me with this ??

Maybe I'm misunderstanding, but it looks like you want to apply oddrowcolor or evenrowcolor to alternating rows - but in your actual php/html, I don't see those class names anywhere. They're not being put on the rows, so their rules wont be applied. Just add them to the appropriate <tr> tags in your php and it should work. Alternatively you could use the nth-of-type selector to apply the rules without altering your php:

tr:nth-of-type(odd) {
    background-color: black;
}
tr:nth-of-type(even) {
    background-color: red;
}

If you want alternate colors for each table row, then a simple CSS should do it. This way, if you want to insert new rows dynamically, it will adjust the colors automatically.

.stripped-rows:nth-child(odd){
   background-color:red;
 }
.stripped-rows:nth-child(even){
   background-color:blue;
 }

and html ...

 <table>
 <tr class = 'stripped-rows'>
     <td>sad</td>
 </tr>
 <tr class = 'stripped-rows'>
  <td>sad</td>
</tr>
 <tr class = 'stripped-rows'>
   <td>sad</td>
</tr>

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM