Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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 从显示中删除CSV标题_Php - Fatal编程技术网

Php 从显示中删除CSV标题

Php 从显示中删除CSV标题,php,Php,我正在尝试使用PHP以分页格式显示CSV文件。我正在使用HTML显示CSV中的标题信息。我之所以使用HTML,是因为如果我转到其余的页面,标题仍保留在表中。然而,仅在第一页中,我就获得了两次标题信息。我尝试使用str\u replace和preg\u replace删除它,但没有成功。这是我目前掌握的代码 <?php $names = file('demo.csv'); $page = $_GET['page']; //constructor takes three parameters

我正在尝试使用PHP以分页格式显示CSV文件。我正在使用HTML显示CSV中的标题信息。我之所以使用HTML,是因为如果我转到其余的页面,标题仍保留在表中。然而,仅在第一页中,我就获得了两次标题信息。我尝试使用str\u replacepreg\u replace删除它,但没有成功。这是我目前掌握的代码

<?php
$names = file('demo.csv');
$page = $_GET['page'];

//constructor takes three parameters
//1. array to be paged
//2. number of results per page (optional parameter. Default is 10)
//3. the current page (optional parameter. Default  is 1)
$pagedResults = new Paginated($names, 50, $page);
$handle = fopen('demo.csv', 'r');
  if (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
    {
    }

echo "<table id='kwTable' border='4' bgcolor='#adb214' style='float:center; margin:100'>";
echo '<tr><th>'.implode('</th><th>', $data).'</th></tr>';
?>
<tbody id="kwBody">
<?php
//when $row is false loop terminates
while ( $row = $pagedResults->fetchPagedRow())
{
    echo "<tr><td>";
    //echo '<tr><th>'.implode('</th><th>', $data).'</th></tr>';
    //Here I am getting the header information from the CSV file twice. 
    $row1 = str_replace( ',', "</td><td>", $row );
    echo $row1;
    echo "</td></tr>";
}
fclose($handle);
echo "</table>";

//important to set the strategy to be used before a call to fetchPagedNavigation
$pagedResults->setLayout(new DoubleBarLayout());
echo $pagedResults->fetchPagedNavigation();


如果CSV顶部只有一个标题行,则只需在第一次通过时跳过该行:

$header = true;
if (!$page) $page = 1;
while ( $row = $pagedResults->fetchPagedRow())
{
    if ($page == 1 && $header) {
        $header = false;
        continue;  // Skip this header row
    }
    echo "<tr><td>";
    //echo '<tr><th>'.implode('</th><th>', $data).'</th></tr>';
    //Here I am getting the header information from the CSV file twice. 
    $row1 = str_replace( ',', "</td><td>", $row );
    echo $row1;
    echo "</td></tr>";
}
$header=true;
如果(!$page)$page=1;
而($row=$pagedResults->fetchPagedRow())
{
如果($page==1&&$header){
$header=false;
continue;//跳过此标题行
}
回声“;
//回显“”。内爆(“”,$data)。“”;
//在这里,我两次从CSV文件中获取头信息。
$row1=str_替换(“,”,“,$row);
echo$row1;
回声“;
}

我尝试了这个建议。但我还是得到了两次标题信息。[link](omega.uta.edu/~rxv1100/slider.php)您的CSV是否有两行标题?我们也可以处理。不,我的CSV只有一个标题行。我首先使用内爆函数单独获取标题信息,然后将其设置为表标题。我正在再次处理CSV文件以显示数据。然而,在第二次处理CSV文件时,我也得到了头信息以及其他数据。对,这个$header变量应该解决这个问题。查看我的编辑,因为可能还未设置$page。