2

I am new to PHP and, I have a CSV file which, am displaying in my web page. I want the CSV file to be displayed with the pagination option so that the web page would look nice. This is the code I have so far.

<?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, 20, $page);

echo "<ul>";

while($row = $pagedResults->fetchPagedRow()) {  
//when $row is false loop terminates
    echo "<li>{$row}</li>";
}

echo "</ul>";

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

However, the CSV file gets displayed with the commas in the screen. Let us consider the below example. Let's assume we have 40 records in my csv file. The contents of the CSV file are as below.

and so on.

In my web page, I am getting the output in 2 pages (as I have set my pagination option to display 20 records in each page.

$pagedResults = new Paginated($names, 20, $page);

The output however still contains the comma from the original CSV file. I want my output to be like below.

First Page:

and so on.

Second Page:

  • Author1 Name1 Name2 Email

and so on.

1 Answer 1

1

This is because you are pulling the line as a row, but not parsing the line and outputting it cleanly.

The easiest solution is to parse it and then output it separated by div's with each row wrapped in a div. Then make the whole thing pretty with CSS.

Like this:

<?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, 20, $page);

echo "<div class='CSVtable'>";

while($row = $pagedResults->fetchPagedRow()) {
    $data = str_getcsv($row);
    $dataRow = implode("</div><div class='csvCol'>", $data);
    echo "<div class='csvRow'><div class='csvCol'>{$dataRow}</div>";
}

echo "</div>";

//important to set the strategy to be used before a call to fetchPagedNavigation
$pagedResults->setLayout(new DoubleBarLayout());
echo $pagedResults->fetchPagedNavigation();
Sign up to request clarification or add additional context in comments.

4 Comments

Div's and custom tags are the way to go these days. No decent designer uses table tag elements. But you could for a quick and dirty.
I tried the suggested changes. I am not getting any output in my screen.
Try adding some debugging code at various points. My favorite down and dirty method is to spit out the contents of variables until you find where the process is breaking down. Try this line: echo "<pre>" . print_r($someVar, true) . "</pre>";
In that code I just gave you put in the variable you want to test in the place of $someVar. Also try just putting a simple test echo in the while loop to ensure that you are even getting into the loop. Something like: echo "In the loop, keep going!<br />";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.