如何使用PHP为交替表行提供不同的背景颜色
发布时间:2020-12-13 13:20:10 所属栏目:PHP教程 来源:网络整理
导读:我有一个数据表,它是根据存储在 mysql数据库中的内容动态生成的. 这就是我的代码的样子: table border="1" tr thName/th thDescription/th thURL/th /tr ?php $query = mysql_query("SELECT * FROM categories"); while ($row = mysql_fetch_assoc($query))
我有一个数据表,它是根据存储在
mysql数据库中的内容动态生成的.
这就是我的代码的样子: <table border="1"> <tr> <th>Name</th> <th>Description</th> <th>URL</th> </tr> <?php $query = mysql_query("SELECT * FROM categories"); while ($row = mysql_fetch_assoc($query)) { $catName = $row['name']; $catDes = $row['description']; $catUrl = $row['url']; echo "<tr class=''>"; echo "<td>$catName</td>"; echo "<td>$catDes</td>"; echo "<td>$catUrl</td>"; echo "</tr>"; } ?> </table> 现在,如果表是静态的,那么我将按重复顺序为每个交替表行分配2种样式中的一种: .whiteBackground { background-color: #fff; } .grayBackground { background-color: #ccc; } 那将是结束.但是由于表行是动态生成的,我该如何实现呢? <?php $x++; $class = ($x%2 == 0)? 'whiteBackground': 'graybackground'; echo "<tr class='$class'>"; ?> 它基本上检查$x是否可以被2均分.如果是,它是偶数. 附:如果你没有看到if else查询的那种风格,它被称为三元运算符. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |