仅突出显示第一行数据


Highlight only first row of data

我正试图在表中包含一个高亮显示的行。进入表格的数据设置如下。我有一个输入数据的html页面。在该页面中,我有一个单独的php文件,它将把这些结果写入一个文本文件。然后我有另一个php文件,它从文本文件中读取数据并将数据放入表中。然后对该数据进行排序,因为正在输入的数据将在不同的时间输入。由于数据是动态排序的,我的问题是,如何突出显示该表的第一行?在我的情况下,第一排的时间最快。

这是我的html页面。

<html>
<form method = "POST" action = "TimeSubmitFinal.php" target = "TimeSubmitFinal.php">
<table> 
    <tr>
        <td><h3> Group 1 </h3></td>
    </tr>
    <tr>
        <td> Site: <b><label value = "ASH" readonly = "true" size = "4" name = "ASH" id = "ash"> ASH</label></td>
        <td> Run Time: <input type = "text" size = "5" id = "ash_run" class = "run" name = "ash_run" value = "TBD"> </td>
        <td> Penalty: <input type = "text" class = "penalty" size = "5" id = "ash_pen" name = "ash_pen" value = "TBD"> </td>
        <td> Completed Time: <input type = "text" readonly = "true" size = "5" id = "ash_com" name = "ash_com" class = "com" value = "TBD"> </td>
        <td> <input type = "button" value = "calculate" onclick = ashcalc() id = "ashcalcu"> </td>
    </tr>
    <tr>
        <td> Site: <b><label value = "ATL" readonly = "true" size = "4" name = "ATL" id = "atl"> ATL </label></td> 
        <td> Run Time: <input type = "text" size = "5" id = "atl_run" class = "run" name = "atl_run" value = "TBD"> </td>
        <td> Penalty: <input type = "text" class = "penalty" size = "5" id = "atl_pen" name = "atl_pen" value = "TBD"> </td>
        <td> Completed Time: <input type = "text" readonly = "true" size = "5" id = "atl_com" name = "atl_com" class = "com" value = "TBD"> </td>
        <td> <input type = "button" value = "calculate" onclick = atlcalc()> </td>
    </tr>
    <tr>
        <td> Site: <b><label value = "COL" readonly = "true" size = "4" name = "COL" id = "col"> COL</label></td>
        <td> Run Time: <input type = "text" size = "5" id = "col_run" class = "run" name = "col_run" value = "TBD"> </td>
        <td> Penalty: <input type = "text" class = "penalty" size = "5" id = "col_pen" name = "col_pen" value = "TBD"> </td>
        <td> Completed Time: <input type = "text" readonly = "true" size = "5" id = "col_com" name = "col_com" class = "com" value = "TBD"> </td>
        <td> <input type = "button" value = "calculate" onclick = colcalc() id = "colcalcu"> </td>
    </tr>
    <tr>
        <td> Site: <b><label value = "SAV" readonly = "true" size = "4" name = "SAV" id = "sav"> SAV</label></td>
        <td> Run Time: <input type = "text" size = "5" id = "sav_run" class = "run" name = "sav_run" value = "TBD"> </td>
        <td> Penalty: <input type = "text" class = "penalty" size = "5" id = "sav_pen" name = "sav_pen" value = "TBD"> </td>
        <td> Completed Time: <input type = "text" readonly = "true" size = "5" id = "sav_com" name = "sav_com" class = "com" value = "TBD"> </td>
        <td> <input type = "button" value = "calculate" onclick = savcalc() id = "savcalcu"> </td>
    </tr>
    <tr>
        <td><input type = "submit" value = "Submit"> </input></td>
    </tr>
</table>
</form>
</html

这是我的第一个php页面

<?php
//make sure text file is in directory 
$txt1 = file("data1.txt");   
//Group1 POSTS
$ash = 'ASH';
$ashrun = $_POST['ash_run'];
$ashpen = $_POST['ash_pen'];
$ashcom = $_POST['ash_com'];
$atl = 'ATL';
$atlrun = $_POST['atl_run'];
$atlpen = $_POST['atl_pen'];
$atlcom = $_POST['atl_com'];
$col = 'COL';
$colrun = $_POST['col_run'];
$colpen = $_POST['col_pen'];
$colcom = $_POST['col_com'];
$sav = 'SAV';
$savrun = $_POST['sav_run'];
$savpen = $_POST['sav_pen'];
$savcom = $_POST['sav_com'];
$result1 = '';

foreach($txt1 as $line1)
{
    if(substr($line1,0,3) == 'ASH')
    {
        $result1 .= $ash. ' '. $ashrun. ' '. $ashpen. ' '. $ashcom. "'r'n".
        $atl. ' '. $atlrun. ' '. $atlpen. ' '. $atlcom. "'r'n".
        $col. ' '. $colrun. ' '. $colpen. ' '. $colcom. "'r'n".
        $sav. ' '. $savrun. ' '. $savpen. ' '. $savcom. "'r'n"; 
    }
}
file_put_contents("data1.txt", $result1);
?>

这是我的php,它显示数据

<html>
<head>
<?php 
    $lines = file("data.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $data = array_map(function($v){
        list($site, $runtime, $pentime, $comtime) = explode(" ",  $v);
        return ["site" => $site, "runtime" => $runtime, "pentime" => $pentime, "comtime" => $comtime];
    }, $lines);
    usort($data, function($a, $b){
        if($a["comtime"] == $b["comtime"])
            return 0;
        return $a["comtime"] > $b["comtime"] ? 1 : -1;
    });
</head>
<body onload = "timedRefresh(10000)">
<table class = "centertab">
    <tr>
        <td> 
            <table border = "1" id = "table3" class = "tablesorter" style = "font-size: 20pt">
            <thead>
            <caption style = "font-size: 25pt; font-weight:bold;"> Eastern Region </caption>
            <tr> 
                <th> Site </th>
                <th> Run Time </th>
                <th> Penalty Time </th>
                <th class = "string-max"> Complete Time </th>
            </tr>
            </thead>
            <tbody>
            <?php foreach($data1 as $site1) { ?>
                <tr>
                    <td> <b><?php echo $site1["site"]; ?> </td>
                    <td> <b><?php echo $site1["runtime"];?> </td>
                    <td> <b><?php echo $site1["pentime"]; ?> </td> 
                    <td> <b><?php echo $site1["comtime"]; ?> </td>
                </tr>
            <?php } ?>
            </tbody>
            </table>
        </td>
</tr>
</table>
</body>
</html>

将以下代码添加到CSS文件中:

.tablesorter tbody tr:first-child td {
    background-color: #FFFF66;
}

将颜色代码更改为您喜欢的颜色。

在循环之前设置一个应用高亮类的字符串,然后在循环中将其设置为空字符串。

$class = ' class="highlight"';
foreach($data1 as $site1) { ?>
    <tr<?php echo $class; //highlight is applied the first time ?>>    
        <td> <b><?php echo $site1["site"]; ?> </td>
        <td> <b><?php echo $site1["runtime"];?> </td>
        <td> <b><?php echo $site1["pentime"]; ?> </td>
        <td> <b><?php echo $site1["comtime"]; ?> </td>
    </tr>
<?php
    $class = ''; // highlight will not be applied in subsequent iterations 
 } ?>

这应该有效:

<?php $highlight=TRUE; foreach($data1 as $site1) { ?>
    <tr <?php echo ($highlight ? 'class="highlight"' : ''); $highlight=false; ?>>
        <td> <b><?php echo $site1["site"]; ?> </td>
        <td> <b><?php echo $site1["runtime"];?> </td>
        <td> <b><?php echo $site1["pentime"]; ?> </td> 
        <td> <b><?php echo $site1["comtime"]; ?> </td>
    </tr>
<?php } ?>