CSV - Display Values

Posted on 2004-11-24
Last Modified: 2008-02-01
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.


$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";

Question by:ncoo
    LVL 7

    Expert Comment

    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)

        $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";
    LVL 7

    Expert Comment

    $i could be renamed to '$currentrow' to make the code more readable.
    LVL 15

    Author Comment

    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.
    LVL 25

    Accepted Solution

    You might like to try the PEAR pagers instead. The documentation on them is quite good, and it automatically deals with making irrelevant links disappear.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Problem to echo 6 29
    PHP Word Writer 1 29
    Get error when search query run in loop 3 24
    Total days in a month 12 27
    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now