简体   繁体   中英

odd even colored rows into a repeat region

I try to make it work into the repeat region but it doesn't work.

<body>
<?php do { ?>
<table class="test">
<tr>
<td><?php echo $row_Recordset1['fld1']; ?></td>
<td>...</td>
<td><?php echo $row_Recordset1['fld2']; ?></td>
<td>...</td>
<td><?php echo $row_Recordset1['fld3']; ?></td>
</tr>
</table>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</body>

and the style is

<head>
<style type = "text/css">
.test tr:nth-child(even) {background: #A4D1FF;}
.test tr:nth-child(odd) {background: #F2F2F2;}
</style>
</head>

Actually the dynamically table generates more than 50 rows so it needs to be into the PHP loop... The full page code is but it still doesn't work ... thank's all for your answers...

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type = "test">
.test tr:nth-child(even) { 
    background: #A4D1FF;
}
.test tr:nth-child(odd) {
    background: #EAF4FF;
}
</style>
</head>
<body>
  <table class="test">
<?php do { ?>
    <tr>
      <td><?php echo $row_Recordset1['movies']; ?></td>
      <td></td>
      <td><?php echo $row_Recordset1['et_paragogis']; ?></td>
      <td></td>
      <td><?php echo $row_Recordset1['format']; ?></td>
    </tr>
  <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
  </table>  
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

The answer is simple: Take the <table> tags outside of the PHP loop. You should only be generating one set of <table> tags at the start and end of the table, not for every row. Once you do this, your CSS stands a chance of working.

if you put your table out of the PHP loop ( <?php do { ?> ) you will get something like this

 .test tr:nth-child(odd) { background: #F2F2F2; } .test tr:nth-child(even) { background: #A4D1FF; } 
 <table class="test"> <tr> <td> odd </td> <td>odd</td> </tr> <tr> <td> even </td> <td> even </td> </tr> </table> 

Take the <table> tag outside of the do-while loop.

<body>
    <table class="test">
        <?php do { ?>
        <tr>
            <td><?= $row_Recordset1['fld1']; ?></td>
            <td>...</td>
            <td><?= $row_Recordset1['fld2']; ?></td>
            <td>...</td>
            <td><?= $row_Recordset1['fld3']; ?></td>
        </tr>
        <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
    </table>
</body>

NB Try to use <?= ?> instead of <?php ?> tag when you echo something. It's more flexible. PHP Tags

Solved with jquery In case somebody will need it in the future Thank's everybody

    <?php do { ?>
  <table border="0" cellpadding="0" cellspacing="0">
    <tbody>
    <script src="jquery.js"></script>
  <script> 
  $(document).ready(function()
  {
      $("tr:even").css("background-color", "#ffffff");
      $("tr:odd").css("background-color", "#f2f2f2");
  });
  </script>
      <tr>
        <td width="60" align="left"><?php echo $row_Recordset1['et_paragogis']; ?></td>
        <td width="5">&nbsp;</td>
        <td width="30" align="right"><?php echo $row_Recordset1['movies']; ?></td>
        <td width="8">&nbsp;</td>
        <td width="90" align="left"><?php echo $row_Recordset1['format']; ?></td>
      </tr>

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