【问题标题】:.addClass with IF statement based on value.addClass 与基于值的 IF 语句
【发布时间】:2012-05-09 22:04:24
【问题描述】:

我正在尝试使用 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>";
       }

【问题讨论】:

    标签: php jquery mysql


    【解决方案1】:

    您是否正在寻找类似的东西?

    $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>";
    }
    

    【讨论】:

    • 啊!这是一件美丽的事情!我总是对“if else/if”感到困惑......谢谢!
    【解决方案2】:

    我假设您正在根据问题的内容以及addClass 是一个 jQuery 函数这一事实来寻找 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');
      });
    });
    

    【讨论】:

    • 为什么 JQuery 会参与进来?似乎使用严格的 PHP 会容易得多。
    • 回想起来,这会更有意义:-D 我把头撞在墙上十分钟试图弄清楚 PHP 中的 addClass 是什么,而不是解决直接问题
    • 我也认为我需要使用 JQuery——也许 :)
    猜你喜欢
    • 2015-12-21
    • 2020-07-09
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多