php利用while单循环实现100行10列表格输出且每行颜色不一样
如图所示,是利用js、php中while单循环配合if条件语句而实现每行颜色不同,且鼠标移动到哪一行就显示不同颜色的效果。具体实现代码如下:
[html] view plaincopyprint?
- <?php
- echo "<table border=1 align=center width=800>";
- echo "<caption><h1>学生成绩表</h1></caption>";
- $i=0;
- $j=0;
- while($i<1000){
- if($i%10==0){
- if($j++%2==0){
- $bgc="#cccccc";
- }else{
- $bgc="#aaffaa";
- }
- echo "<tr onmouseover="show(this)" onmouseout="noshow(this)" bgcolor="".$bgc."">";
- }
- echo "<td>".$i."</td>";
- if(++$i%10==0){
- echo "</tr>";
- }
- }
- echo "</table>";
- ?>
- <script>
- function show(obj){
- yc=obj.style.backgroundColor;
- obj.style.backgroundColor="red";
- }
- function noshow(obj){
- obj.style.backgroundColor=yc;
- }
- </script>
<script> function show(obj){ yc=obj.style.backgroundColor; obj.style.backgroundColor="#E95A70"; } function noshow(obj){ obj.style.backgroundColor=yc; } </script> [<a href="input.php">继续a添加</a>] <?php //这里是PHP代码 header("content-Type: text/html; charset=utf-8"); $sql = "SELECT * FROM `list`";//需要执行的SQL语句(这里是浏览数据功能) require"conn.php"; //调用conn.php文件,执行数据库操作 ?> <table width="100%" border="1"> <tr bgcolor="#5BFFAD"> <td ><strong>电话</strong></td> <td ><strong>邮箱</strong></td> <td ><strong>姓名</strong></td> <td ><strong>操作</strong></td> </tr> <?php while($row = mysql_fetch_row($result)) //循环开始 { static $j=0; if($j++%2==0){ $bgc="#cccccc"; }else{ $bgc=""; } ?> <tr onmouseover="show(this)" onmouseout="noshow(this)" bgcolor="<?php echo $bgc; ?>"> <td><?php echo $row[1]; ?></td> <td><?php echo $row[2]; ?></td> <td><?php echo $row[3]; ?></td> <td><div align="center"> [<a href="edit.php?id=<?php echo $row[0]; ?>">编辑</a>] [<a href="del.php?id=<?php echo $row[0]; ?>">删除</a>] </div></td> </tr> <?php } ?> </table>
www.palmdy.com
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: thinkphp 模板截取中文字符串函数
- 下一篇: Mongodb性能调优 -性能优化建议