【问题标题】:How can I add a class for td in reference to two columns如何参考两列为 td 添加一个类
【发布时间】:2023-04-08 05:48:01
【问题描述】:

我有一个数据库行的渲染

echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";

// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {

    // echo out the contents of each row into a table
    echo "<tr>";
    echo '<td>' . $row['id'] . '</td>';
    echo '<td>' . $row['tex1'] . '</td>';
    echo '<td>' . $row['tex2'] . '</td>';
    echo '<td>' . $row['tex3'] . '</td>';
    echo '<td>' . $row['tex4'] . '</td>';
    echo '<td>' . $row['tex5'] . '</td>';
    echo '<td>' . $row['tex6'] . '</td>';
    echo '<td>' . $row['tex7'] . '</td>';
    echo '<td>' . $row['tex8'] . '</td>';
    echo '<td>' . $row['tex9'] . '</td>';
    echo '<td>' . $row['tex10'] . '</td>';
    echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
    echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
    echo "</tr>"; 
} 

// close table>
echo "</table>";

并且在tex1中是一个日期,我的问题是tr的一个类,当tex2为空并且tex1的日期超过30天时,tr有红色类,tex1的日期是手动插入的,如何我可以在此基础上更改课程吗?

【问题讨论】:

    标签: php css class html-table


    【解决方案1】:

    在编写元素时尝试条件:

    echo '<tr'.
        ( ((strtotime($row['tex1']) < strtotime('30 days ago'))
          && (empty($row['tex2']))) ? ' class="red"' : '').
    '>';
    

    【讨论】:

    • 谢谢,这就是我想要的,但另一个问题是我如何呈现欧洲 Dd/Mm/Yy 而不是 Mm​​/Dd/Yy 的日期?
    • 嗯,这是另一个问题。如果您的脚本有其他问题,如果它们不涉及“如何插入课程”问题,请将此标记为已回答并发布另一个问题。
    • 我想你正在寻找类似的东西: date_format(date_create($row['tex1']),"d/m/Y")
    【解决方案2】:

    您应该检查您在问题中提到的条件。这可以像这样完成。

     if(strtotime($row['tex1']) + 30 * 24 * 60 * 60 < time() && $row['tex2'] =="") {
           $class="class='red'";
     } else {
           $class= "";
     }
    
     echo "<tr $class>";// id condition satisfies, class='red' will get echo, else echo will be null.
    

    【讨论】:

      【解决方案3】:

      我不确定我是否正确理解了您想要的内容,但我会试一试。只要满足日期早于 30 天 (tex1) 或名字为空 (tex2) 的条件,您就想向 中添加一个类(“red”)。

      echo "<table border='1' cellpadding='10'>";
      echo "<table border='1' cellpadding='10'>";
      echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
      
      // loop through results of database query, displaying them in the table
      while($row = mysql_fetch_array( $result )) {
      
          // echo out the contents of each row into a table
          // I assume since you're just printing it out, the date is a String
          // So first convert it to a timestamp
          $date = strtotime($row['tex1']);
          // Determine the difference, I'm doing it this way to make it easy for you to understand
          $days = 60*60*24*30; // 30 days
          if (empty($row['tex2']) || time()-$date > $days) {
              echo '<tr>';
          } else {
              echo '<tr class="red">';
          }
          echo '<td>' . $row['id'] . '</td>';
          echo '<td>' . $row['tex1'] . '</td>';
          echo '<td>' . $row['tex2'] . '</td>';
          echo '<td>' . $row['tex3'] . '</td>';
          echo '<td>' . $row['tex4'] . '</td>';
          echo '<td>' . $row['tex5'] . '</td>';
          echo '<td>' . $row['tex6'] . '</td>';
          echo '<td>' . $row['tex7'] . '</td>';
          echo '<td>' . $row['tex8'] . '</td>';
          echo '<td>' . $row['tex9'] . '</td>';
          echo '<td>' . $row['tex10'] . '</td>';
          echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
          echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
          echo "</tr>"; 
      } 
      
      // close table>
      echo "</table>";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-10
        • 2018-03-14
        • 2016-09-20
        • 2021-05-02
        • 1970-01-01
        • 2012-10-21
        • 1970-01-01
        相关资源
        最近更新 更多