简体   繁体   English

更改行值时更改表背景颜色PHP

[英]Change table background color when change row value PHP

I have a table on PHP like bellow and I need that, when the first column changes value, the line changes color. 我有一个像波纹管这样的PHP表,我需要它,当第一列更改值时,该行更改颜色。 Eg: at table in example, first and second lines will be red, third line will be blue, fourth red again and so on 例如:在表格中,第一行和第二行将为红色,第三行将为蓝色,第四行将再次为红色,依此类推

<table>
<tr>
    <td>1</td>
    <td>Test 1</td>
    <td>Today</td>
</tr>
<tr>
    <td>1</td>
    <td>Test 2</td>
    <td>Tomorrow</td>
</tr>
<tr>
    <td>2</td>
    <td>Test 3</td>
    <td>Yesterday</td>
</tr>
<tr>
    <td>3</td>
    <td>Test 1</td>
    <td>Next Saturday</td>
</tr>
</table>

I can do in CSS, jQuery or another language, but I need help. 我可以使用CSS,jQuery或其他语言进行操作,但需要帮助。

Thanks 谢谢

You can set two classes for TRs, eg rowOdd & rowEven 您可以为TR设置两个类,例如rowOdd和rowEven

$days = array(1=>'Day1', 2=>'Day2', 3=>'Day3');
$trClass='Odd';
echo '<table>';
foreach ($days as $key=>$value) {
    echo '<tr class="row'.$trClass.'"><td>'.$key.'</td><td>'.$value.'</td></tr>';
    if ($trClass=='Odd') { $trClass='Even'; } else { $trClass='Odd'; }
}
echo '</table>';

CSS 的CSS

.rowOdd { background-color: #F00; }
.rowEven { background-color: #0F0; }

Remember you can always search for answers to your question in stack overflow. 请记住,您始终可以在堆栈溢出中搜索问题的答案。 odds are you are not the first to have this kind of issue. 您不是第一个遇到这种问题的人。

please refer to the following: 请参考以下内容:

Adding PHP alternate table row colours to existing HTML table 将PHP备用表格行颜色添加到现有HTML表格中

You can use a flag veriable for count. 您可以使用可验证的标记进行计数。

For example: 例如:

$count = 0;
while($rows = mysql_fetch_array($query)){
    $count++;
    if(($count%2) == 0){
        echo '<tr style="background:#FFF">';
        echo    '<td>';
        echo    '</td>';
        echo '</tr>';
    }
    else{
        echo '<tr style="background:#000">';
        echo    '<td>';
        echo    '</td>';
        echo '</tr>';
    }
}

Try this one: 试试这个:

$bgcolor       = ""; 
$new_bgcolor   = "#F1F1F1";
$bgcolor_trans = "";

if ( $current_row > $last_row ){
   $bgcolor_trans = $bgcolor;
   $bgcolor       = $new_bgcolor;
   $new_bgcolor   = $bgcolor_trans;    
} 

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

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