2014-02-07 38 views
0

我有一個文件文件3:從一個文件,生成動態表中檢索數據

[File3] 
Time | Name | Name | ID1 | ID2 
15:50 | Alex | Web | * 38 | 5 
10:50 | Xyz | Xxx | * 55 | 65 
10:50 | Volume | Xxx | 55 | 65 
12:50 | Kate | Uh | 35 | 62 
15:50 | Maria | Zzz | 38 | 67 

時,將生成文件和時間只能有一個行與下一行時間可以是30

I」 m不擅長用PHP編寫,想編寫一個腳本來下載一個文件並加載到一個動態表格中,其中標有[*]的標誌的行應該是紅色的。

結果:

==================================== 
| Time | Name |Surname| ID1 | ID2 | 
==================================== 
|15:50 | Alex | Web | * 38 | 5 | - red color background 
==================================== 
|10:50 | Xyz | Xxx | * 55 | 65 | - red color background 
==================================== 
|10:50 | asd | Xxx | 55 | 69 | 
==================================== 
|12:50 | Kate | Uh | 35 | 62 | 
==================================== 
|15:50 | Maria| Zzz | 38 | 67 | 
==================================== 

我的PHP腳本的HTML:

<div class="Xyz" style="width:800px;height:50px;"> 
<?php 
print "<h1 align=\"left\"> </br></h1>"; 
$file = fopen("File3.txt", "r"); 
while (false !== ($line = fgets($file))) { 
$wynik=explode("+",$line); 
$wynik=array_slice($wynik,0,10); 
$grid=pc_grid_horizontal($wynik,10); 
print $grid; 
} 
function pc_grid_horizontal($array, $size) { 
    $table_width = 100; 
    $width = intval($table_width/$size); 
    $tr = '<tr align="center">'; 
    $td = "<td width=\"$width%%\">%s</td>"; 
    $grid = "<table border=1 cellspacing=0 width=\"$table_width%\">$tr"; 
    $i = 0; 
    foreach ($array as $e) { 
     $grid .= sprintf($td, $e); 
     $i++; 
     if (!($i % $size)) { 
      $grid .= "</tr>$tr"; 
     } 
    } 

    while ($i % $size) { 
     $grid .= sprintf($td, '&nbsp;'); 
     $i++; 
    } 
    $end_tr_len = strlen($tr) * -1; 
    if (substr($grid, $end_tr_len) != $tr) { 
     $grid .= '</tr>'; 
    } else { 
     $grid = substr($grid, 0, $end_tr_len); 
    } 
    $grid .= '</table>'; 

    return $grid; 
} 
?> 

請幫助我。

回答

0

爲什麼不更簡單些?您實際上不需要使用古老的HTML屬性來設置整個表的樣式。您可以使用外部樣式表中的css。

例如:

<?php 
$file = file('File3.txt'); 

$table = array(); 
foreach($file as $line=>$content) { 
    $parts = explode('|', $content); // turn your single line into an array of fields 
    $parts = array_map('trim',$parts); // trim off any spaces from all parts 
    if($line == 0) { 
     $class = 'header'; // class for the header, that is the first line of the table 
    } elseif($parts[3][0] == '*') { 
     $class = 'emphasis'; // class for a row that needs to be red 
    } else { 
     $class = ''; // no class usually 
    } 
    $table[] = '<tr class="'.$class.'"><td>' . implode('</td><td>', $parts) . '</td></tr>'; 
} 

echo '<table id="yourTbl">', implode("\r\n", $table), '</table>'; 

那麼你添加包含CSS:

table#yourTbl { 
    width: xx px; /* set to whatever width you want the table to be. If you wrap it in a <div> with a fixed with, you can change this one to 100% */ 
} 
#yourTbl td { 
    /* default style for all table data fields. Overwritten for specific fields in the next 2 definitions */ 
    text-align: center; /* make sure all values are centered inside the table */ 
} 
/* your first row, with the table heading */ 
#yourTbl tr.header td { 
    font-weight: bold; 
} 
/* your row with emphasis, which contains a * */ 
#yourTbl tr.emphasis td { 
    color: red; 
} 
+0

我會用'」 | ''(包括空格)在'explode()'調用中。 –

+0

我不知道他的數據是如何完全格式化的。我也不知道任何管道是否逃脫,或者是否允許內部值。以防萬一,我爆炸'|'然後修剪掉它的空白。 – Tularis

+0

我做了一個測試,發現一個錯誤:解析錯誤: 語法錯誤,第14行index.php中出現意外的T_FOREACH 腳本中有14行:foreach($ file as $ line => $ content){ – user3281767