如何在引用两列时为td添加一个类

时间:2015-10-24 14:25:50

标签: php css class html-table

我有一行数据库

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的日期是手动插入的,如何我可以在此基础上改变课程吗?

3 个答案:

答案 0 :(得分:0)

我不确定我是否理解你想要的东西,但我会试一试。只要满足条件,即日期早于30天(tex1)或名字为空(tex2),您就要添加一个类(“红色”)。

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

答案 1 :(得分:0)

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

 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.

答案 2 :(得分:0)

在编写元素时尝试条件:

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