I am new to PHP and trying to display the CSV data in a tabular format in my web page with pagination option. This is the code I have so far.
<?php
$names = file('demo.csv');
$page = $_GET['page'];
$pagedResults = new Paginated($names, 20, $page);
echo "<table border='2' bgcolor='#fbd998' style='float:center; margin:20'>";
//when $row is false loop terminates
while ( $row = $pagedResults->fetchPagedRow())
{
echo "<tr><td>";
$row1 = str_replace( ',', "</td><td>", $row );
echo $row1;
echo "</td></tr>";
}
echo "</table>";
//important to set the strategy to be used before a call to fetchPagedNavigation
$pagedResults->setLayout(new DoubleBarLayout());
echo $pagedResults->fetchPagedNavigation();
?>
The problem was, I was not able to display the CSV header information except the first page. To avoid this, I used the handler to extract the first line alone from CSV file (which contains the header information). I added the below piece of code before the while loop.
if (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
{
}
echo "<table border='2' bgcolor='#fbd998' style='float:center; margin:20'>";
echo '<tr><th>'.implode('</th><th>', $data).'</th></tr>';
This strategy seems to be working fine in all the pages except that in the first page, I have the CSV header twice. For example, my current first page looks like below.
- Column1 Column2 Column3
-
Column1 Column2 Column3
-
Rest of my data
Can someone please guide me on how to resolve this problem?