[英]Retrieving data from a file and generation dynamic table
我有一個文件File3:
[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
我不太擅長使用PHP編寫代碼,而是想編寫一個腳本來下載文件並將其加載到動態表中的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 |
====================================
我的HTML腳本php:
<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, ' ');
$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;
}
?>
請幫我。
為什么不簡單一些? 您實際上不需要使用古老的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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.