2016-04-18 112 views
0

我正試圖在表格中突出顯示一行。我進入表格的數據設置如下。我有一個輸入數據的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> 

回答

1

下面的代碼添加到您的CSS文件:

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

將顏色代碼更改爲您p的顏色參考。

+0

.tablesorter TBODY TR:第n個孩子(1)TD { 背景顏色:#FFFF66; } –

+0

您也可以使用第n個孩子(1)。 –

+0

非常感謝您的幫助。這正是我所期待的。 –

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