仅突出显示第一行数据

时间:2016-04-18 20:40:11

标签: php html

我正在尝试在我的表格中包含一行突出显示。进入表格的我的数据设置如下。我有一个输入数据的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>

3 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

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

$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 
 } ?>

答案 2 :(得分:0)

这应该有效:

<?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 } ?>