比较两个值并用MySQL的颜色标记文本

时间:2015-09-08 01:19:04

标签: php html mysql

我的查询如下:

  $results6 = $mysqli->query("
          SELECT competitor.competitor_id, competitor.c_dob, competitor.klass,
     competitor.egaclass, competitor.category, competitor.l_name,
     competitor.f_name, competitor.team, competitor.riik_id, 
            riik.lyhend, riik.pilt,
            charge.racefee, charge.party, charge.extraparty, charge.paid,
            (racefee + extraparty) AS rahakokku,
CASE WHEN egaclass = 'E' THEN 'Elite/Adults' 
     WHEN egaclass = 'J' THEN 'Juior' 
     WHEN egaclass = 'K' THEN 'Kids'ELSE 'Veterans' END AS ageclass_text
          FROM competitor
          inner join riik on competitor.riik_id = riik.riik_id
          inner join charge on competitor.competitor_id = charge.competitor_id
          order by f_name, l_name");

和表格代码是:

print '<table class="mytable7">';
echo "<tr><th>jrNr</th><th>Competitor </th><th>D.O.B.</th><th>Age</th><th>Ageclass</th><th>Dinner</th><th>Fee</th><th>Extra dinner</th><th>Sum</th><th>Paid</th><th>Country</th><th>Status</th></tr>";
while($row = $results6->fetch_array()) {
$timestamp = strtotime($row['c_dob']);
    print '<tr>';
    print '<td>'.$row["competitor_id"].'</td>';
    print '<td>'.$row["f_name"].'  '.$row["l_name"].'</td>';
    print '<td>'.$date = date('d-m-Y', $timestamp).'</td>';
    print '<td>'.$row["vanus"].'</td>';
    print '<td>'.$row["ageclass_text"].'</td>';
    print '<td>'.$row["party"].'</td>';
    print '<td>'.$row["racefee"].'</td>';
    print '<td>'.$row["extraparty"].'</td>';
    print '<td>'.$row["rahakokku"].'</td>';
    print '<td>'.$row["paid"].'</td>';
    echo '<td><img src="data:image/jpeg;base64,' . base64_encode($row['pilt']) . '" width="30" height="20"></td>';
    print '<td>'.$row["olek"].'</td>';

 print '</tr>';

直观地看表: enter image description here

我如何比较“rahakokku”(总和)与“付费”字段,当“付费”与“rahakokku”(总和)相同时,状态为白色绿色“OK”,如果不相同,则为红色.something让我们说“假”

1 个答案:

答案 0 :(得分:1)

您可以使用

之类的语句执行此操作

在此处,替换状态列的打印代码。

if ($row["rahakokku"]==$row["paid"]) {
     // display green text
     print '<td style="background-color:#128824">'.$row["olek"].'</td>';
}
else {
     // display red text
     print '<td style="background-color:#CC1212">'.$row["olek"].'</td>';
}

这将更改列状态的背景颜色。