Link to home
Start Free TrialLog in
Avatar of ramesh4046
ramesh4046

asked on

CSV file contents display in a web page with pagination

I am developing a web page which takes a CSV file as an input. I am trying to display the CSV file in the web page with the pagination option. From this article, I found out a way to add pagination to my web page. However, in the example, they are using the PHP array for pagination. I believe if am able to convert the CSV file input to an PHP array, I can achieve what am looking for. I am completely new to PHP and  web development. Can someone please help me out?
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

PHP has a function for converting csv to an array:

http://php.net/manual/en/function.str-getcsv.php

is the manual page that outlines it.

You will need to load the csv into a string using file_get_contents() first.

http://php.net/manual/en/function.file-get-contents.php

Cd&
If you're new to PHP this article will help you find some good learning resources, and more importantly, avoid the bad learning resources!
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

If you want to post a link to the CSV file we might be able to integrate the SitePoint pagination example into a working code set for you.
Avatar of ramesh4046
ramesh4046

ASKER

From another link, I have figured out a solution for pagination. This is the code I have so far.

<?php
require_once "Paginated.php";
require_once "DoubleBarLayout.php";
?>
<html>
<head>
<title>Pagination</title>

<!-- Just a little style formatting. Has no bearing on example -->
<style type="text/css">
	body {
		font-family: Verdana;
		font-size: 13px;
	}
	
	a {
		text-decoration: none;
	}
	
	a:hover {
		text-decoration: underline;
	}
</style>
<!-- End style formatting -->
</head>

<body>

	<?php
	
	//create an array of names in alphabetic order. A database call could have retrieved these items
	$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);
	//$pagedResults = new Paginated($line, 10, $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();
	?>
</body>
</html>

Open in new window


However, as per the above code, I am getting the output with commas. For example, if the CSV file is like below,

1,name1,name2,name3

I am expecting the output like,

1 name1 name2 name3

I do not want commas in my output. Can someone please help me with this?
I wish to incorporate more features to my pagination web page. So, as the first step I would like to display the CSV file in the pagination format. After that, I am going to have a slider (like how we have in amazon for price ranges). In that slider, I am planning to let the user select the minimum and maximum values so that only those rows in the csv file that matches that criteria will be displayed.
We can help you with this if we can see the input CSV file.  Please post that file (your test data version) or a link to the test data.  If you do not have any test data yet, please create some!
Alright, my test data is present in this location at this time.

My demo CSV file
If you are not able to download it, please let me know.
This should help get things started.  The $arr variable is an array, and each of the elements in the array is also an array.

<?php // RAY_temp_ramesh4046.php
error_reporting(E_ALL);
echo '<pre>';

// LOCATION OF THE TEST DATA
$url = 'http://omega.uta.edu/~rxv1100/demo.csv';

// READ THE CSV FILE INTO AN ARRAY
$arr = array();
$fpo = fopen($url, 'r');
if ($fpo)
{
    while (!feof($fpo))
    {
        $arr[] = fgetcsv($fpo);
    }
}
else
{
    trigger_error("FAILED TO OPEN $url", E_USER_ERROR);
}

// SHOW THE RESULT -- AN ARRAY OF ARRAYS.
print_r($arr);

Open in new window

Hi Ray, I am getting the CSV file displayed like below.

Array
(
    [0] => Array
        (
            [0] => Author
            [1] => Keyword1
            [2] => Keyword2
            [3] => Keyword3
        )

I need to display the csv data like a table as below.

User generated image
I have posted this question in the stackoverflow site also. The link to my question is this

Kindly, let me know if you need more information.
@ramesh4046: The PHP print_r() function is provided to help you visualize the data.  It's not intended to be a complete solution.  You need a little more programming to get the tabular display.  If I have a moment I'll try to show you how that might be done, then you can add the pagination into the programming.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America 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
Thanks a lot for helping me out.
Thanks for the points, and good luck with your project, ~Ray