-1

如何将回显表头添加到我当前的代码中?

我对如何做到这一点有点迷茫,因为 $lines[0] 只打印我的标题行,但是当我执行 if 语句来回显<th>if $lines[0] else echo<tr>它会回显多个空<th>的,所以我如果有人可以提供帮助,我会有点失落。

function schedule_gen()
{
    //set file
    $filename='schedule.txt';
    //open
    $handler=fopen('schedule.txt','r');
    //read through file
    $file=fread($handler,filesize($filename));

    //start table creation
    echo "<table id='schedule_table'>"; 

    //split into array by return\linefeed
    $lines=explode("\r\n",$file);

    //loop through rows
    for($i=0;$i<count($lines);$i++) 
    { 
        //if not blank then print row
        if($lines[$i]!=""&&$lines[$i]!=" ")
        {

            echo "<tr class='schedule_row'>"; 

            //split into array by commas
            $items=explode("\t",$lines[$i]);
            //loop through cells 
            for($j=0;$j<count($items);$j++) 
            { 
                 //if not blank then print cell
                 if($items[$j]!=""&&$items[$j]!=" ")
                 {
                     echo "<td class='schedule_cell'>".$items[$j]."</td>"; 
                 }
            } 
            echo "</tr>"; 
        }
     } 
     echo "</table>"; 
     //end table creation

     fclose($handle);
     //close file 
}

schedule.txt 示例:

Employee/schedule restrictions  Thur 4/26   Fri 4/27    Sat 4/28    Sun 4/29    Mon 4/30    Tue 5/1 Wed 5/2
Administrative                          
Assistant   8a-4    8a-4    no  no  8a-4p   8a-4p   8a-4p

QC Team -Manager                            
QC team / no Tues or Sat    8a-4    8a-4    no  8a-4p   8a-4p   no  8a-4p
QC team 6p-2a   6p-2a   6p-2a   no  6p-2a   6p-2a
4

1 回答 1

1

不太确定我是否正确阅读了您的问题,但这是您需要的吗?

function schedule_gen()
{
//set file
$filename='schedule.txt';
//open
$handler=fopen('schedule.txt','r');
//read through file
$file=fread($handler,filesize($filename));

//start table creation
echo "<table id='schedule_table'>"; 

//split into array by return\linefeed
$lines=explode("\r\n",$file);

//loop through rows
for($i=0;$i<count($lines);$i++) 
{ 
//if not blank then print row
if($lines[$i]!=""&&$lines[$i]!=" ")
{
$t_type="td";
if($i==0){$t_type="th";}

//split into array by commas
$items=explode("\t",$lines[$i]);
//loop through cells 
for($j=0;$j<count($items);$j++) 
{ 
//if not blank then print cell
if($items[$j]!=""&&$items[$j]!=" ")
{
echo "<$t_type class='schedule_cell'>".$items[$j]."</$t_type>"; 
}
} 
echo "</tr>"; 
}
} 
echo "</table>"; 
//end table creation

fclose($handle);
//close file 
}
于 2012-04-23T15:44:50.797 回答