PHP中的while循环在偶数和奇数之间交替

发布于 2024-11-09 07:19:39 字数 737 浏览 0 评论 0原文

我一直在编写几行代码,但似乎无法让它工作。基本上我想通过 while 循环在偶数和奇数表格样式之间交替。我做错了什么?

似乎每次都只循环 if() 。

谢谢!

<?php
    include 'connect.php';
    echo "<table id='hor-zebra'>";
    $i = 0;
    while($row = mysql_fetch_array($result))
    {
       if(i%2 == 0)
       {
          echo "<tr class='even'>";
          echo "<td>" . $row['departure'] ." ✈ ". $row['destination'] . "</td>";
          echo "</tr>";
       }
       
       else
       {
          echo "<tr>";
          echo "<td>" . $row['departure'] ." ✈ ". $row['destination'] . "</td>";
          echo "</tr>";
       }
       $i++;
    }
    echo "</table>";

    mysql_close($con);

  ?>

I have been working on couple of lines of code but I can't seem to get it to work. Basically I want to alternate between even and odd table-styles via a while loop. What am I doing wrong?

Seems as though it only loops through the if() everytime.

Thanx!

<?php
    include 'connect.php';
    echo "<table id='hor-zebra'>";
    $i = 0;
    while($row = mysql_fetch_array($result))
    {
       if(i%2 == 0)
       {
          echo "<tr class='even'>";
          echo "<td>" . $row['departure'] ." ✈ ". $row['destination'] . "</td>";
          echo "</tr>";
       }
       
       else
       {
          echo "<tr>";
          echo "<td>" . $row['departure'] ." ✈ ". $row['destination'] . "</td>";
          echo "</tr>";
       }
       $i++;
    }
    echo "</table>";

    mysql_close($con);

  ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

塔塔猫 2024-11-16 07:19:39

您的 if 条件中有拼写错误。它应该是:

   if($i%2 == 0)

您还可以通过将类名分配给 if 和 else 块中的变量来节省一些击键:

   if($i%2 == 0)
   {
      $class = 'even';
   }
   else
   {
      $class = 'odd';
   }

   echo "<tr class='$class'>";
   echo "<td>" . $row['departure'] ." ✈ ". $row['destination'] . "</td>";
   echo "</tr>";

You have a typo in your if condition. It should be:

   if($i%2 == 0)

You can also save a few keystrokes by just assigning the class name to a variable in the if and else blocks:

   if($i%2 == 0)
   {
      $class = 'even';
   }
   else
   {
      $class = 'odd';
   }

   echo "<tr class='$class'>";
   echo "<td>" . $row['departure'] ." ✈ ". $row['destination'] . "</td>";
   echo "</tr>";
猫七 2024-11-16 07:19:39

您还可以使用 css .nth-child 属性

   tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}

按照 W3 示例

you can also use the css .nth-child property

   tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}

As per W3 example

江湖彼岸 2024-11-16 07:19:39

CSS-Tricks 针对这个问题发布了一个非常非常优雅的解决方案。
它看起来就像超级还原论的 C++ 魔法。本质上他们是这样做的:

<div class="example-class<?php echo ($xyz++%2); ?>">

这适用于任何循环:for、foreach 和 while。
修改整数可以提供更大的步长,即在 3 后重置,在 4 后重置,依此类推。

CSS-Tricks 最终解决方案

CSS-Tricks has posted a very very elegant solution to this problem.
It appears like super-reductionist C++ magic. Essentially they do this:

<div class="example-class<?php echo ($xyz++%2); ?>">

This works in any loop: for, foreach and while.
Modifying the integer gives you larger step sizes, i.e. reset after 3, reset after 4 and so on.

CSS-Tricks final solution

酒浓于脸红 2024-11-16 07:19:39

你忘记了一个 '$'

 if(i%2 == 0)

应该是

 if(($i % 2) == 0)

You forgot a '$'

 if(i%2 == 0)

Should be

 if(($i % 2) == 0)
将军与妓 2024-11-16 07:19:39

将此行...

if(i%2 == 0)

...替换为以下内容:

if($i % 2 == 0)

Replace this line...

if(i%2 == 0)

...with the following:

if($i % 2 == 0)
谜兔 2024-11-16 07:19:39

这可以进一步改进。

foreach($post_array as $array => $row) {
    $class = ($array %2 == 0) ? 'even' : 'odd';
    echo '
    <tr class="'.$class.'">
        <td>' .$row['title']. '</td>
        <td>' .$row['content']. '</td>
        <td>' .$row['catid']. '</td>
        <td>' .$row['id']. '</td>
        <td>' . '<form action="edit.php?id='.$row['id'].'" method="post">
            <input type="hidden" name="id" id="id" value="'.$row['id'].'" />
            <input type="submit" name="edit" value="Edit" />
        </form>' . '</td>
        <td>' . '<form action="" method="post">
            <input type="hidden" name="id" id="id" value="'.$row['id'].'" />
            <input type="submit" name="delete" value="Delete" />
        </form>' . '</td>
    </tr>';
}

This can be improved further.

foreach($post_array as $array => $row) {
    $class = ($array %2 == 0) ? 'even' : 'odd';
    echo '
    <tr class="'.$class.'">
        <td>' .$row['title']. '</td>
        <td>' .$row['content']. '</td>
        <td>' .$row['catid']. '</td>
        <td>' .$row['id']. '</td>
        <td>' . '<form action="edit.php?id='.$row['id'].'" method="post">
            <input type="hidden" name="id" id="id" value="'.$row['id'].'" />
            <input type="submit" name="edit" value="Edit" />
        </form>' . '</td>
        <td>' . '<form action="" method="post">
            <input type="hidden" name="id" id="id" value="'.$row['id'].'" />
            <input type="submit" name="delete" value="Delete" />
        </form>' . '</td>
    </tr>';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文