Link to home
Start Free TrialLog in
Avatar of ncoo
ncoo

asked on

CSV - Display Values

I'm trying to make a php script to output a csv file.

What I'd like to happen is to have a max of 10 results per page, and a next page previous page option.

This is the code I've got so far, it may be simpler to just start again I don't mind either away. Just as long as the list is split into the same elements.

<?php

$filename = "example.csv";

$id = fopen($filename, "r"); //open the file
while ($data = fgetcsv($id, filesize($filename))) //start a loop
$table[] = $data;
fclose($id); //close file

echo "<table>\n";

foreach($table as $row)
{
echo "<tr>";
   foreach($row as $data)
      list ($key_number, $title, $sub_title, $category, $extra, $x, $y) = $line;

   echo "<td>$key_number</td><td>$title</td>";
echo "</tr>\n";
}

echo "</table>\n";

?>
Avatar of aib_42
aib_42

You want your first foreach loop to count the rows it has 'seen'. If your page starts at the, say, 'pagestart'th row, it should ignore the first 'pagestart' rows, print 10 rows following the 'pagestart'th row, then quit.

'pagestart' will be a parameter, probably received using the GET method, with a default of 0. Your 'next' link should point to itself with pagestart=pagestart+10 and the 'previous' link with pagestart=pagestart-10. Here is a quick and dirty example:

$pagestart = $_GET['pagestart'];

$i = 0;
foreach($table as $row)
{
    if ($i >= $pagestart) {
        /* print the row */
    }

    if ($i >= $pagestart + 10)
        break;

    $i = $i + 1;
}

echo "<a href=\"me.php?pagestart=".$pagestart-10."\">previous</a>\n";
echo "<a href=\"me.php?pagestart=".$pagestart+10."\">next</a>\n";
$i could be renamed to '$currentrow' to make the code more readable.
Avatar of ncoo

ASKER

That doesn't really work.  That's why it may be best to start the code from the begining, if I explain the problem fully hopefully it will help.

I've got a CSV file, I want to display this with 10 results per page.  Idealy I'd like the next tag and previous tag to only appear when need, I.e. so the previous tag doesn't appear at the start of the file, I'd also like some page numbers at the bottom to help skip through the file (I don't know if you've herd of pager.php but this covers this part of the script but I've never worked out how to use it correctly).

I hope that explains the problem better.
ASKER CERTIFIED SOLUTION
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial