我有一个理想情况下有13行和9列的php表。它是由shell脚本给出的文本文件创建的。它在2分钟后变化,问题有时最后2,3行没有9列,它们的列与标题不对齐。列向左移动。有没有办法创建一个表,即使它的前列缺失
也应保留列位置file_get_contents('/c:/personal/data') or die ("Unable")));
//$length = count($new_array);
//print_r($new_array)
$table1 = '<table class = "table table-hover" >';
$trimmed = trim($myfile);
$filearray = explode("\n", $trimmed);
foreach($filearray as $row) {
$table1 .= '<tr>';
// here separate your row that is a string, into an array
$cols = explode(" ", $row);
foreach($cols as $value) {
$table1 .= '<td style = "text-align: left" >'.$value.'</td>';
}
$table1 .= '</tr>';
} $ table1。=&#39;&#39;;
echo $table1;
答案 0 :(得分:1)
您可以检查该行是否存在,如果没有,则插入一个空单元格,如下所示:
...
foreach($filearray as $row) {
$table1 .= '<tr>';
// here separate your row that is a string, into an array
$cols = explode(" ", $row);
for ($i=0; $i < 9; $i++){
if (isset($cols[$i]))
$table1 .= '<td style = "text-align: left" >'.$value.'</td>';
else
$table1 .= '<td style = "text-align: left" ></td>';
}
$table1 .= '</tr>';
}
答案 1 :(得分:0)
问题有时最后2,3行没有9列,他们的 列与标题
未对齐
您需要检查,实际列数等于理想数。如果它更少 - 添加空列:
$ideal = 9;
$n = count($cols);
if($n < $ideal)
{
for ($i = $n; $i < $ideal; ++$i)
{
$cols[$i] = '';
}
}