doujianjian2060 2013-07-25 15:14
浏览 103
已采纳

如果其中一个列属性为true,则设置表格行的样式

I'm filling an HTML table with data from the following query:

$content_downloads = mysql_query( "SELECT 
     s.site, s.machine, d.projectid, d.directorySize, d.connectivityError, 
     d.blocked, d.blockedBy, d.blockedSince, d.errorString, d.dashboardTimeStamp 
     FROM rtcdb.site_machines s 
     LEFT JOIN rtcdb.downloadstatuses d 
     on (s.site = d.site and s.machine = d.machine)             
     where s.site in (select d2.site from rtcdb.downloadstatuses d2 
     where  d2.dashboardTimeStamp > (SELECT DATE_ADD(max(dashboardTimeStamp),INTERVAL -1 DAY)  
     from rtcdb.downloadstatuses)) order by s.site, s.machine asc", $con) or die(mysql_error());

This query is going into an HTML table based on the following HTML/PHP:

            <table>
                <thead>
                    <tr>
                        <th>Site</th>
                        <th>Machine</th>
                        <th>Project ID</th>
                        <th>Folder Size</th>
                        <th>Error Description</th>
                    </tr>
                </thead>

                <tbody>
                    <?php
                        while ($row = mysql_fetch_assoc($content_downloads)) {
                            $error_string = "";
                            if ($row["connectivityError"] == true ) {
                                $error_string = "COULD NOT CONNECT: " . $row["errorString"];
                            }
                            elseif ($row["blocked"] == true ) {
                                $error_string = "BLOCKED BY LOCK OWNED BY: " . $row["blockedBy"] . " " . $row["blockedSince"];
                            }

                            echo '
                                <tr>
                                    <td>'.$row["site"].'</td>
                                    <td>'.$row["machine"].'</td>
                                    <td>'.$row["projectid"].'</td>
                                    <td>'.$row["directorySize"].'</td>
                                    <td style = "text-align: left; color: red;">'.$error_string.'</td>
                                </tr> ';
                            }
                        ?>
                </tbody>
        </table>

The error description column in this table rarely has contents. As you can see, when it does, a PHP conditional function (the if and ifelse functions) places a statement before the error description to make the description more understandable. One thing you may notice is that I am coloring the text of error descriptions red. The thing that I am struggling with that I need your help on is to selectively style an entire row with a background-color of yellow, when the error description cell in that row has a value. In other words, when the two conditionals I wrote are true, I would like to make the background-color of the row that includes that error description yellow.

It seems pretty simple, but I have tried various options and can't seem to figure it out. Any tips Stack Overflow?

  • 写回答

4条回答 默认 最新

  • doulupian8725 2013-07-25 15:18
    关注

    Check if the error string is empty:

    echo '
        <tr' . (empty($error_string) ? '' : ' style="background-color:yellow;"') . '>
            <td>'.$row["site"].'</td>
            <td>'.$row["machine"].'</td>
            <td>'.$row["projectid"].'</td>
            <td>'.$row["directorySize"].'</td>
            <td style = "text-align: left; color: red;">'.$error_string.'</td>
        </tr> ';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮