Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中创建列不同的表_Php - Fatal编程技术网

如何在php中创建列不同的表

如何在php中创建列不同的表,php,Php,我有一个php表,理想情况下有13行9列。它是根据shell脚本提供的文本文件创建的。2分钟后会发生变化,问题是有时最后2,3行没有9列,它们的列与标题不对齐。列向左移动。是否有一种方法可以创建一个表,即使前面的列丢失,它也应该保留列位置 file_get_contents('/c:/personal/data') or die ("Unable"))); //$length = count($new_array); //print_r($new_array) $table1 = '&l

我有一个php表,理想情况下有13行9列。它是根据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>';

您可以检查该行是否存在,如果不存在,则插入一个空单元格,如下所示:

...
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>';
}
。。。
foreach($filearray作为$row){
$table1.='';
//这里将作为字符串的行分隔为一个数组
$cols=分解(“,$row”);
对于($i=0;$i<9;$i++){
if(isset($cols[$i]))
$table1.=''.$value'.';
其他的
$table1.='';
}
$table1.='';
}
问题是有时最后2,3行没有9列,它们的 列与标题不对齐

您需要检查实际列的数量是否等于理想列的数量。如果小于-添加空列:

$ideal = 9;
$n = count($cols);
if($n < $ideal)
{
    for ($i = $n; $i < $ideal; ++$i)
    {
        $cols[$i] = '';
    }
}
$ideal=9;
$n=计数($cols);
如果($n<$ideal)
{
对于($i=$n;$i<$ideal;++$i)
{
$cols[$i]='';
}
}

$table1.=''-此代码用于什么?:)好吧,很明显,什么都不加
$ideal = 9;
$n = count($cols);
if($n < $ideal)
{
    for ($i = $n; $i < $ideal; ++$i)
    {
        $cols[$i] = '';
    }
}