将PHP替代表格行颜色添加到现有HTML表格 [英] Adding PHP alternate table row colours to existing HTML table

查看:158
本文介绍了将PHP替代表格行颜色添加到现有HTML表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已经用PHP编写的表格,可以从数据库中调用数据,如下所示:

 < TABLE cellSpacing = 1 cellPadding = 2 align = center bgColor =#aaaaaa border = 0 width =100%class =logintbl> 
< TR>
< TD bgColor = whitesmoke colSpan = 0>< B>页面< / B>< / td>
< / tr>
< tr>
< td>
< table align =centercellSpacing = 0 cellPadding = 2 border =0width =100%>
< tr>
< td align =centervalign =bottom>& nbsp;< font color =#4d71a1>< b>页面名称< / b>< / font>& amp ; NBSP;< / TD>
< / tr>
<?php while($ row = mssql_fetch_array($ result)){?>
< tr bgcolor =#eeeeee>

< td align =center><?php echo $ row [PageURL]; ?>< / TD>
< td align =center>
< a href =PageUpdate.php?id =<?php echo $ row [PageID];?>>< img src =images / 0013-pen.gifwidth =16height =16alt =border =0>< / a>& nbsp;& nbsp;& nbsp;
< / td>

< / tr>
<?php}?>
< tr>< td colspan =7>& nbsp;< / td>< / tr>
< tr>
< td colspan =7align =center>
< / td>
< / tr>

< / table>
< / td>
< / tr>
< / table>

我一直在尝试使用PHP代码片段和一些研究来交替行的颜色这样做:

 < tr bgcolor =<?php echo($ clrCounter ++%2 == 0?'#000000 ':'#ffffff');?>> 

它似乎不能正常工作,所以我觉得我错了某处,我知道那里是我可以实现的更长的实现方式。我只是希望有一些简单的东西。我是否在努力试图以这种方式实施?

我将它整合如下:

 < TABLE cellSpacing = 1 cellPadding = 2 align = center bgColor =#aaaaaa border = 0 width =100%class =logintbl> 
< TR>
< td bgColor = whitesmoke colSpan = 0>< B>网页< / B>< / td>
< / tr>
< tr>
< td>
< table align =centercellSpacing = 0 cellPadding = 2 border =0width =100%>
< tr bgcolor =#3A7525>
< td align =centervalign =bottom>& nbsp;< font color =#4d71a1>< b>页面名称< / b>< / font>& amp ; NBSP;< / TD>
< / tr>
<?php while($ row = mssql_fetch_array($ result)){?>

< tr bgcolor =<?php echo($ clrCounter ++%2 == 0?'#C2C2C2':'#ffffff');?>>

< td align =center><?php echo $ row [PageURL]; ?>< / TD>
< td align =center>
< a href =PageUpdate.php?id =<?php echo $ row [PageID];?>>< img src =images / 0013-pen.gifwidth =16height =16alt =border =0>< / a>& nbsp;& nbsp;& nbsp;
< / td>

< / tr>
<?php}?>
< tr>
< td colspan =7align =center>
< / td>
< / tr>

< / table>
< / td>
< / tr>
< / table>

它有点类似,但由于某种原因,第一个条目是蓝色的?当我指定白色和灰色时。

解决方案

您可以在循环从您的数据库返回的结果时使用以下内容:

 <?php 

//定义行颜色
$ color1 =#FFFFFF;
$ color2 =#F4F9FF;

//设置行计数器
$ row_count = 0;

while($ row = mssql_fetch_array($ result)){
$ row_color =($ row_count%2)? $ color1:$ color2;
?>
< tr bgcolor =<?php echo $ row_color;?>>
< td align =center><?php echo $ row [PageURL]; ?>< / TD>
< td align =center>
< a href =PageUpdate.php?id =<?php echo $ row [PageID];?>>< img src =images / 0013-pen.gifwidth =16height =16alt =border =0>< / a>& nbsp;& nbsp;& nbsp;
< / td>
< / tr>
<?php

$ row_count ++;

}

?>

或者,您可以替换bgcolor标记并为每行分配CSS类。


I have a Table already written in PHP that echos out data called from the database like so:

<TABLE cellSpacing=1 cellPadding=2 align=center bgColor=#aaaaaa border=0 width="100%" class="logintbl">
             <TR>
              <TD bgColor=whitesmoke colSpan=0><B>Pages</B></td>
            </tr>
            <tr>
                <td>
                    <table align="center" cellSpacing=0 cellPadding=2 border="0" width="100%">
                        <tr>
                            <td align="center" valign="bottom">&nbsp;<font color="#4d71a1"><b>Page Name</b></font>&nbsp;</td>
                        </tr>
                        <?php while ($row = mssql_fetch_array($result)) { ?>
                        <tr bgcolor="#eeeeee">

                            <td align="center"><?php echo $row["PageURL"]; ?></td>
                            <td align="center">
                            <a href="PageUpdate.php?id=<?php echo $row["PageID"]; ?>"><img src="images/0013-pen.gif" width="16" height="16" alt="" border="0"></a>&nbsp;&nbsp;&nbsp;
                            </td>

                        </tr>
                <?php } ?>
                        <tr><td colspan="7">&nbsp;</td></tr>
                        <tr>
                            <td colspan="7" align="center">
                            </td>
                        </tr>

                    </table>
                </td>
            </tr>
        </table>

I have been trying to alternate the colours of the rows, using a snippet of PHP and after some research implemented this:

<tr bgcolor="<?php echo ($clrCounter++ % 2 == 0 ? '#000000' : '#ffffff'); ?>">

It doesn't seem to work correctly, so I feel I am going wrong somewhere, I know there is longer ways to implement this that I could implement. I was just hoping for something simple. Am I wasting effort trying to implement it this way?

I integrated it as follows:

 <TABLE cellSpacing=1 cellPadding=2 align=center bgColor=#aaaaaa border=0 width="100%" class="logintbl">
            <TR>
              <td bgColor=whitesmoke colSpan=0><B>Pages</B></td>
            </tr>
            <tr>
                <td>
                    <table align="center" cellSpacing=0 cellPadding=2 border="0" width="100%">
                        <tr bgcolor="#3A7525">
                            <td align="center" valign="bottom">&nbsp;<font color="#4d71a1"><b>Page Name</b></font>&nbsp;</td>
                        </tr>
                        <?php while ($row = mssql_fetch_array($result)) { ?>

                        <tr bgcolor="<?php echo ($clrCounter++ % 2 == 0 ? '#C2C2C2' : '#ffffff'); ?>">

                            <td align="center"><?php echo $row["PageURL"]; ?></td>
                            <td align="center">
                            <a href="PageUpdate.php?id=<?php echo $row["PageID"]; ?>"><img src="images/0013-pen.gif" width="16" height="16" alt="" border="0"></a>&nbsp;&nbsp;&nbsp;
                            </td>

                        </tr>
                <?php } ?>
                        <tr>
                            <td colspan="7" align="center">
                            </td>
                        </tr>

                    </table>
                </td>
            </tr>
        </table>

It sort of works, but for some reason the very first entry is blue???? when I've specified white and grey.

解决方案

You could use the following when looping through the results returned from your db:

<?php

// Define row colors
$color1 = "#FFFFFF";
$color2 = "#F4F9FF";

// Set row counter
$row_count = 0;

while ($row = mssql_fetch_array($result)) {
    $row_color = ($row_count % 2) ? $color1 : $color2;
?>
    <tr bgcolor="<?php echo $row_color; ?>">
        <td align="center"><?php echo $row["PageURL"]; ?></td>
        <td align="center">
            <a href="PageUpdate.php?id=<?php echo $row["PageID"]; ?>"><img src="images/0013-pen.gif" width="16" height="16" alt="" border="0"></a>&nbsp;&nbsp;&nbsp;
        </td>
    </tr>
<?php

    $row_count++;

}

?>

Alternatively, you could replace the bgcolor tags and assign a CSS class to each row.

这篇关于将PHP替代表格行颜色添加到现有HTML表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆