简体   繁体   English

在PHP中切换类名

[英]Toggle class name in PHP

I am trying to stripe a table based on the time in each row. 我正在尝试根据每一行中的时间来划分表。 For example: 例如:

Blah -  time 1
Blah2 - time 1
Blah3 - time 2
Blah4 - time 3
Blah5 - time 4 

I need time 1 to be one colour (blue for example) time 2 to be colour black, time 3 to toggle back blue. 我需要时间1为一种颜色(例如蓝色),时间2为黑色,时间3为蓝色。 I am trying to toggle the colour changed based on when the time has changed. 我正在尝试根据时间的变化切换更改的颜色。 My PHP so far: 到目前为止,我的PHP:

    while($main_Row = $main_Results->fetch_assoc()){
        if( $pickup_Time == $main_Row['pickup_Time'] ){
            $stripe = "blue";   
        }
        else{
            $stripe = "black";  
            $pickup_Time = $main_Row['pickup_Time'];
        }

        echo '<tr class="'.$stripe.'">';
        echo '<td>Blah'.$i.'</td>';
        echo '<td>'.$main_Row['pickup_Time'].'</td>';
        echo '</tr>';
    }

What I have currently is not giving me the results I desire. 我目前没有给我想要的结果。 I feel I'm missing something, any help would be greatly appreciated. 我觉得我缺少什么,任何帮助将不胜感激。

$pickup_Time = "";
$stripe = "";
while($main_Row = $main_Results->fetch_assoc()){
    if( $pickup_Time != $main_Row['pickup_Time'] ){
        $stripe = $stripe == "blue" ? "black" : "blue";   
    }

    echo '<tr class="'.$stripe.'">';
    echo '<td>Blah'.$i.'</td>';
    echo '<td>'.$main_Row['pickup_Time'].'</td>';
    echo '</tr>';

    $pickup_Time = $main_Row['pickup_Time'];
}

Quickly threw something together, it's a little verbose, but I wanted to make the general intent and process clear. 很快将一些内容放在一起,有点冗长,但是我想弄清楚总体意图和过程。

This will stripe per row, to stripe per column you would add your classes onto the td. 这将按行分条,若要按列分条,则将类添加到td中。 If you want to change how many stripes before it cycles over again, change the modulo to whatever number you want. 如果要在再次循环之前更改多少个条纹,请将模数更改为所需的数字。

<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>

        <style>
            .red {
                background-color: red;
            }

            .blue {
                background-color: blue;
            }
        </style>
    </head>
    <body>
        <?php

        $mysqli = new mysqli('', '', '', '');

        $qry = "SELECT * FROM data";
        $result = $mysqli->query($qry);

        $counter = 1;

        echo "<table>";
        while ( $row = $result->fetch_assoc() ) {

            $class = "";
            if( $counter % 3 == 1 ) {
                $class = "blue";
            } else if ( $counter % 3 === 2 ) {
                $class = "red";
            }

            echo '<tr class="' . $class . '">';
            echo '<td>' . $row['id'] . '</td>';
            echo '<td>' . $row['id'] . '</td>';
            echo '<td>' . $row['id'] . '</td>';
            echo '</tr>';

            $counter += 1;
        }
        echo "</table>";

        $result->free();

        $mysqli->close();
        ?>
    </body>
</html>

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

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