0

我正在尝试使用 if 语句和两个 .addClass 背景颜色变量,具体取决于“shift_times”的列值是否=“SAT AM/SUN PM”或=“SAT PM/SUN AM”。我正在寻找值是数字而不是文本时的解决方案。

     $query = "SELECT COUNT(confirmed) as cnt, 
     position, shift_times 
     FROM volConfirm 
     WHERE confirmed='yes'  
     GROUP BY shift_times, position
     order by position"; 

     $result = mysql_query($query) or die(mysql_error());


     while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
  echo "<td>" . $row['position'] ." ". $row['shift_times'] ."= " . $row['cnt'] . "</td>";
  echo "</tr>";
       }
4

2 回答 2

2

像这样的东西是你要找的吗?

$query = "SELECT
      COUNT(confirmed) as cnt, 
      position,
      shift_times 
   FROM volConfirm 
   WHERE confirmed='yes'  
   GROUP BY shift_times, position
   ORDER BY position"; 

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)) {
   if ($row['shift_times'] === 'SAT AM/SUN PM') {
      echo '<tr class="class1">';
   } else if ($row['shift_times'] === 'SAT PM/SUN AM') {
      echo '<tr class="class2">';
   } else {
      echo '<tr>';
   }
   echo "<td>" . $row['position'] ." ". $row['shift_times'] ."= " . $row['cnt'] . "</td>";
   echo "</tr>";
}
于 2012-05-09T22:42:18.413 回答
0

我假设您正在根据问题的内容和addClassjQuery 函数这一事实来寻找 jQuery 答案……如果我错了,我深表歉意。如果这就是你的意思,这是你的答案:

PHP:

while($row = mysql_fetch_array($result))
{
    echo "<tr>";
    echo "<td>" . $row['position'] ." <span class='shifttimes'>". $row['shift_times'] ."</span>= " . $row['cnt'] . "</td>";
    echo "</tr>";
}

jQuery:

$(function() {
  $.each($('.shifttimes'), function() {
    var $this = $(this);
    $this.text() == "SAT AM/SUN PM" ? $this.addClass('className') : $this.addClass('otherClassName');
  });
});
于 2012-05-09T22:35:07.120 回答