添加或类奇数

时间:2014-07-23 14:25:52

标签: php html

我需要添加一个类“奇数”,或者像示例一样:

<tr>
    <li></li>
</tr>
<tr class="odd">
    <li></li>
</tr>
<tr>
    <li></li>
</tr>
<tr class="odd">
    <li></li>
</tr>

第一个tr添加类“奇”,下一个没有类,下一个类“奇”,下一个没有类。

代码:

<?php
    echo "
    <div class=\"table-blue\">
        <h4>Preços de domínios</h4>
        <table class=\"table\">
            <thead>
                <tr>
                    <th><b>Extenção</b></th>
                    <th><b>1 Ano</b></th>
                    <th><b>2 Anos</b></th>
                    <th><b>3 Anos</b></th>
                    <th><b>5 Anos</b></th>
                    <th><b>10 Anos</b></th>
                    <th style=\"text-align: center;\"><b>Acção</b></th>
                </tr>
            </thead>
            <tbody>";
            while($row = mysql_fetch_array($result))
                echo "
                    <tr class=\"odd\">
                        <td><b>".$row['extension']."</b></td>
                        <td>" . ($row['msetupfee'] =='-1.00' ? '<i>N/D</i>' : $row['msetupfee'] . " " . $row['curprefix']) . "</td>
                                    <td>" . ($row['qsetupfee'] =='-1.00' ? '<i>N/D</i>' : $row['qsetupfee'] . " " . $row['curprefix']) . "</td>
                                    <td>" . ($row['ssetupfee'] =='-1.00' ? '<i>N/D</i>' : $row['ssetupfee'] . " " . $row['curprefix']) . "</td>
                                    <td>" . ($row['bsetupfee'] =='-1.00' ? '<i>N/D</i>' : $row['bsetupfee'] . " " . $row['curprefix']) . "</td>
                                    <td>" . ($row['biennially'] =='-1.00' ? '<i>N/D</i>' : $row['biennially'] . " " . $row['curprefix']) . "</td>
                                    <td style=\" width: 100px;\"><a class=\"button small blue\" href=\"http://nova-data.eu/whmcs/cart.php?a=add&domain=register&tld=".$row['extension']."\" target=\"_blank\">Comprar</a></td>
                                </tr>
                                ";
                        ?>

有人可以帮助我吗?

3 个答案:

答案 0 :(得分:4)

您必须计算行数,当您有奇数行时,将一个类添加到表行。所以:

$i = 0;
while ($row = mysql_fetch_array($result)) {
    echo '<tr' . ($i % 2 == 1 ? ' class="odd"' : '') . '>';
        echo '<td>...';
        // other TDs
    echo '</tr>';

    $i++;
}

如果您只需要此类来设置样式,则可以在CSS中直接使用此类名:

tr:nth-child(2n+1) {/* style for odd rows */}

OR

tr:nth-child(odd) {/* style for odd rows */}

答案 1 :(得分:1)

<?php
echo "
<div class=\"table-blue\">
    <h4>Preços de domínios</h4>
    <table class=\"table\">
        <thead>
            <tr>
                <th><b>Extenção</b></th>
                <th><b>1 Ano</b></th>
                <th><b>2 Anos</b></th>
                <th><b>3 Anos</b></th>
                <th><b>5 Anos</b></th>
                <th><b>10 Anos</b></th>
                <th style=\"text-align: center;\"><b>Acção</b></th>
            </tr>
        </thead>
        <tbody>";
        $x=0;
        while($row = mysql_fetch_array($result))
          $x++;
          if ($x % 2 == 0) {
            $class = 'odd';
          }else{
            $class = '';
          }
            echo "
                <tr class='".$class."'>
                    <td><b>".$row['extension']."</b></td>
                    <td>" . ($row['msetupfee'] =='-1.00' ? '<i>N/D</i>' : $row['msetupfee'] . " " . $row['curprefix']) . "</td>
                                <td>" . ($row['qsetupfee'] =='-1.00' ? '<i>N/D</i>' : $row['qsetupfee'] . " " . $row['curprefix']) . "</td>
                                <td>" . ($row['ssetupfee'] =='-1.00' ? '<i>N/D</i>' : $row['ssetupfee'] . " " . $row['curprefix']) . "</td>
                                <td>" . ($row['bsetupfee'] =='-1.00' ? '<i>N/D</i>' : $row['bsetupfee'] . " " . $row['curprefix']) . "</td>
                                <td>" . ($row['biennially'] =='-1.00' ? '<i>N/D</i>' : $row['biennially'] . " " . $row['curprefix']) . "</td>
                                <td style=\" width: 100px;\"><a class=\"button small blue\" href=\"http://nova-data.eu/whmcs/cart.php?a=add&domain=register&tld=".$row['extension']."\" target=\"_blank\">Comprar</a></td>
                            </tr>
                            ";
                    ?>

答案 2 :(得分:0)

 $i=0;
 while($row = mysql_fetch_array($result))
      $i++;
      if ($i % 2 == 0) {
          echo "<tr class=\"even\">\n";
      } else {
          echo "<tr class=\"odd\">\n";
      }
      //<TD> etc..