简体   繁体   English

将CSS类应用于动态表格数据

[英]Apply CSS class to dynamic table data

I have a MySQL database with 35 different columns labeled 1-35. 我有一个MySQL数据库,其中包含35个标记为1-35的不同列。 The data is displayed in a dynamic table. 数据显示在动态表中。 I am looking for the PHP code that would allow me to apply a CSS class to specific cells based on what is returned inside them. 我正在寻找PHP代码,该代码将允许我根据内部返回的内容将CSS类应用于特定的单元格。

EXAMPLE: Column 1 can return either TRUE or FALSE value. 示例:列1可以返回TRUE或FALSE值。 If a TRUE value is returned, I want to give that cell a different background color, or highlight it. 如果返回TRUE值,我想给该单元格一个不同的背景色,或突出显示它。

I have the CSS class as: 我的CSS类为:

.highlight {
background-color: #FC0;
}

After the CSS class has then been applied to the specific cells from columns 1-35, I would like the PHP code that can total the number of highlighted cells in the column labeled TOTAL. 然后将CSS类应用于第1-35列中的特定单元格之后,我想要PHP代码可以总计标记为TOTAL的列中突出显示的单元格数量。

Can't seem to find the PHP code to tie all this together. 似乎找不到将所有这些结合在一起的PHP代码。 If someone could advise, I'd be very grateful. 如果有人可以提供建议,我将不胜感激。

<td class="<?php if ($row['item'] == TRUE)
{
    echo 'highlight';
    $highlights++;
}
else
{
    echo 'no_highlight';
}
?>"><?php echo $row['item'];?></td>

and then... 接着...

<td id="totalHighlights"><?php echo $highlights;?></td>

EDIT: Sample code... 编辑:示例代码...

<?php

//We've queried the database and passed the results into $result via mysql_fetch_assoc().

while ($row = $result)
{
    //Clear highlights and values arrays for each row.
    $highlights = array();
    $values = array();
    foreach($row as $entry)
    {
        if ($entry == TRUE)
        {
            //Put a truthy value into highlights array.
            array_push($highlights, 1);
        }
        else
        {
            //Put a falsey value into highlights array.
            array_push($highlights, 0);
        }

        //Push actual field value to values array.
        array_push($values, $entry);
    }
    echo '<tr class="row">';

    //Create cell with total highlights per row using the array_sum() of highlights.
    echo '<td class="row_total_highlights">'.array_sum($highlights).'</td>';

    //Create each value cell and populate them with each field value from values array.
    $i = 0;
    foreach ($values as $value)
    {
        echo '<td class="entry ';
        echo ($highlights[$i] == 1 ? 'trueClass' : 'falseClass');
        echo '">'.$value.'</td>';
        $i++;
    }
    echo '</tr>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM