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?
ramesh4046Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

COBOLdinosaurCommented:
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&
0
Ray PaseurCommented:
If you're new to PHP this article will help you find some good learning resources, and more importantly, avoid the bad learning resources!
http://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.
0
ramesh4046Author Commented:
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?
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

ramesh4046Author Commented:
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.
0
Ray PaseurCommented:
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!
0
ramesh4046Author Commented:
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.
0
Ray PaseurCommented:
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

0
ramesh4046Author Commented:
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.

The actual output that am expecting in the screen
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.
0
Ray PaseurCommented:
@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.
0
Ray PaseurCommented:
<?php // RAY_temp_ramesh4046.php
error_reporting(E_ALL);

// 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);
}

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

// CREATE A TABULAR DISPLAY FOR THE HEADER
$hed = '<tr>';
foreach ($arr[0] as $txt)
{
    $hed .= '<th>' . $txt . '</th>';
}
$hed .= '</tr>' . PHP_EOL;

// HEADERS ARE NO LONGER USED
unset($arr[0]);

// ITERATE OVER THE REMAINING ARRAYS
$out = NULL;
foreach ($arr as $row)
{
    $out .= '<tr>';
    foreach ($row as $txt)
    {
        $out .= '<td>' . $txt . '</td>';
    }
    $out .= '</tr>' . PHP_EOL;
}

// FINISH THE TABLE
$tbl = '<table>' . PHP_EOL;
$tbl .= $hed;
$tbl .= $out;
$tbl .= '</table>' . PHP_EOL;

// SHOW THE WORK PRODUCT
echo $tbl;

Open in new window

HTH, ~Ray
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ramesh4046Author Commented:
Thanks a lot for helping me out.
0
Ray PaseurCommented:
Thanks for the points, and good luck with your project, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.