Avatar of theresel
theresel
Flag for Canada asked on

Paginate CSV Output

Hi,

I need a solution to paginate my csv file output when viewing on the web.  The csv file will become quite large and I'd like to split it to 20 entries per page with links to the next or previous pages.

I already have the following script to show the results which is working fine, just need a little polishing for pagination and including the column titles on each page.  Of course it's enclosed in the php statement...

....
$dir_name = "C:/wamp/logs/ocwa.csv";
$fh = fopen($dir_name, "r");
$row = 1;

echo "<table width=\"100%\" border=\"1\">\n";
while(($data = fgetcsv($fh, 1000, ",")) !== FALSE)
{
    echo "<tr>\n";
    $num = count($data); // count the # of lines that exist
    $row++;  //increment the row variable to the next line
   
//output current line to the screen.
    for ($i=0; $i < $num; $i++)
      {
          echo "<td>\n";
          echo $data[$i]."\n";
          echo "</td>\n";
      }
    echo "</tr>\n";
}
echo "</table>\n";

//close file handler and delete file
fclose($fh);
....

Hope someone can help with a piece of code example.
Microsoft AccessPHP

Avatar of undefined
Last Comment
telyni19

8/22/2022 - Mon
Jeffrey Coachman

It may be easier to just make multiple files
Then the: Next, Previous, Last, First, navigation would navigate the "Files"

What say you?
ASKER CERTIFIED SOLUTION
telyni19

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
theresel

ASKER
Thanks boag2000 for your suggestion, but to make multiple files is not an option with what I want to do.

I tried your code telyni19, but now I can't access my report at all.  I have a 500 Internal error that says the website cannot display the page.

The page where the code resides is report.php which is located under the root directory so users can access it through localhost, but the csv file (ocwa.csv) itself is in the logs directory which is outside of the root folder so we can't access it by using the localhost domain.  Should I put it under the root directory?
telyni19

At least for testing the code, I would say yes, put your csv under the same directory path as the php (and of course, change all my placeholder "server.com" lines and such to the correct path). Was it actually working before with a directory link of
dir_name = "C:/wamp/logs/ocwa.csv";

Open in new window

? Are you expecting site users to have the file themselves, or is this setup just for your own use on your own computer?

I don't have a PHP server myself right now, so I couldn't directly test the code I gave you. I'm happy to help debug though. If you can't resolve the 500 error by moving the csv file, please specify what version of PHP you're running and whether you can successfully use the very first line ($linenum = $HTTP_GET_VARS['linenum']) with just an echo $linenum statement replacing the rest of the code temporarily to show the result.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
theresel

ASKER
Hi Telyni19,

I tried putting the csv file under the root directory and it still doesn't work.  Yes it was working with my previous code, even when in the logs directory.

I don't expect site users to access these files as they are for our personal use only.  We have a small answering service and we need to email messages to clients.  The information is sent via a web form (on our local server) and the information is also saved in a csv file.  The only purpose of the csv report is to check what messages have been sent to clients and when, with web forms we no longer have any traces of sent messages.  I know that we can always open the csv file directly on the computer, but I wanted to make it easier for everyone by accessing a report on a web page.
Ray Paseur

Even if your answering service is small, you should be using a data base for this sort of thing (they are free, like PHP, so there is no financial excuse for not doing so).  This will give you many abilities to organize the data with order, group, limit and other query functions. Hope you'll move in that direction.

When you say, "The csv file will become quite large and I'd like to split it..." what is the meaning of "quite large?"  If you're talking about a few dozen, a few thousand, or some different number of lines there might be different approaches that would make sense.  One easy way to look at this might be to read the entire CSV into a temporary data base table and use the system described in this canonical article about pagination.
http://www.sitepoint.com/perfect-php-pagination/
telyni19

As I said before:
If you can't resolve the 500 error by moving the csv file, please specify what version of PHP you're running and whether you can successfully use the very first line ($linenum = $HTTP_GET_VARS['linenum']) with just an echo $linenum statement replacing the rest of the code temporarily to show the result.
Does the $linenum statement by itself work?

There's fundamentally nothing different about what I gave you from what you originally posted, as far as retrieving information from the csv file. All I did was add a way to track which piece of the file you actually want to display by adding a parameter to the URL. So something about the parameter usage is probably interfering. I can help debug, but only if you can test that piece of it.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
theresel

ASKER
When I type localhost/ocwa.csv?linenum=1 in my browser it seems to be working, but it tries to save the file and does not show the report.
telyni19

You still haven't performed the test I specified. I wanted to know whether a php file with just these two lines worked on your system to display the linenum parameter value:
$linenum = $HTTP_GET_VARS['linenum'])
echo $linenum

Open in new window


If you're using PHP version of at least 4.1, then you could try this statement instead of the $HTTP_GET_VARS statement:

$linenum = $_GET['linenum']

Apparently $HTTP_GET_VARS is an older function, now deprecated. It shouldn't make much difference in terms of getting the file working, but $_GET will be cleaner. So then the test file would be this:
$linenum = $_GET['linenum'])
echo $linenum

Open in new window


When you're running your script, you're normally not just loading your csv file directly, right? You said you typed localhost/ocwa.csv?linenum=1
The csv file doesn't know how to interpret the parameter number though; only the php file does. Don't you have the script saved in a php file, like script.php or ocwa.php or some such?

So then you need to run  localhost/script.php?linenum=1 or http://www.servername.com/logs/script.php?linenum=1 or however you normally run your script.