php while fgetcvs function - displaying only last x lines - php

I have a code that works just fine:
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, "|")) !== FALSE) {
echo $data[0].$data[1].$data[2]."<br />\n";
}
fclose($handle);
}
But it gets all the contents from file.csv and I want just last x lines. I tried slice method and etc. thanks in advance for your help.

If the command line utility tail is on your PATH, then you can use this snippet:
$x = 3;
if (($handle = popen(sprintf("tail --lines=%d file.csv", $x), "rb")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
echo $data[0].$data[1].$data[2]."<br />\n";
}
fclose($handle);
}
The PHP function fgetcsv will always parse one row at a time.

Related

VLOOKUP IN PHP FOR A .CSV FILE

I am simply trying to do what excel calls vlookup using php based on a .csv file
.CSV FILE is the following:
ID;NAME;ADDRESS
123;John;999 Street
123;John;234 Blvd
999;Mark;777 Avenue
I have a textbox for inputting "ID". If I type "123" and press submit I want the following result:
123;John;999 Street
123;John;234 Blvd
This is what I got but I need help
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
if($data[0] == '123')
array_push($data);
}
fclose($handle);
var_dump($result);
}
?>
...
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
if($data[0] == 'yes')
array_push($result, $data);
}
fclose($handle);
var_dump($data);
}
?>
outputs
Warning: array_push() [function.array-push]: First argument should be an array in C:\xampp\htdocs\index.php on line 7
Warning: array_push() [function.array-push]: First argument should be an array in C:\xampp\htdocs\index.php on line 7
bool(false)

PHP read CSV file line by lines

I have a CSV file loaded from an URL, and I want to loop over the lines with PHP.
Here is a typical line of this CSV:
1004000018;active;"TEST1";"TEST2";"TEST3";"TEST4"
I would like to get this result, for each row:
1004000018
active
TEST1
TEST2
TEST3
TEST4
You can achieve this using the php function fgetcsv, this should work :
PHP
$file = fopen('file.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
//$line[0] = '1004000018' in first iteration
print_r($line);
}
fclose($file);
This will help you for read csv:
if (($handle = fopen("$source_file", "r")) !== FALSE) {
$columns = fgetcsv($handle, $max_line_length, $delemietr);
if (!$columns) {
$error['message'] = 'Empty';
return ($error);
}
while (($rows = fgetcsv($handle, 10000, "\t")) !== false) {
if ($rows[1] && array(null) !== $rows) { // ignore blank lines
$data1 = $rows[1];
}
}
}

Adding text after each while loop

This is my code
if (($handle = fopen("data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo "Title: $data[1]<BR>";
echo "Time: $data[2]<BR>";
}
echo 'Results End. <hr>';
}
I want the horizontal line to appear after each result however it just appears after the last one. What am I missing?
Thanks
You need to echo "Results End.<hr>" in while loop.
if (($handle = fopen("data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo "Title: $data[1]<BR>";
echo "Time: $data[2]<BR>";
echo "Results End.<hr>";
}
}
You just have to put a line after each result :)
if (($handle = fopen("data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo "Title: $data[1]<br/>";
echo "Time: $data[2]<hr/>";
}
echo 'Results End.';
}
You'll need to have the <hr> tag inside the while loop to have it with each result. <br> is a newline, <hr> is the horizontal line

Find the next row fgetcsv

I'm using fgetcsv to open some data. I can display the current row but how do you then display certain data about the next row within the current row?
Thanks
if (($handle = fopen("data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo $data[2];
}

How do I skip the first piece of data when extracting from a csv file with PHP

The following code is what I am using to get an entire column of data from my csv files. (There are many columns, but I only need #2). This code will put all of the data into the array called $cbsmin. What I need to do is skip the very first piece of data in each file as it is the category name not actually a piece of data.
How would this best be done?
for($i=0;$i<count($files);$i++){
if (($handle = fopen($files[$i], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$cbsmin[] = $data[2];
}
fclose($handle);
}
}
Just use a simple flag to mark if it is the first line. If it is, reset the flag to false and skip that line.
$isfirst = true;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($isfirst)
{
$isfirst = false;
continue;
}
$cbsmin[] = $data[2];
}
Just execute a single
fgetcsv($handle, 1000, ",");
before the while loop.
Just throw in an extra read prior to going into the while loop
for($i=0;$i<count($files);$i++){
if (($handle = fopen($files[$i], "r")) !== FALSE) {
// eat the header
fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$cbsmin[] = $data[2];
}
fclose($handle);
}
}
Another technique I use for this is to use the header row for keys in the data. This works if the header rows are consistent over time and from file to file.
for($i=0;$i<count($files);$i++){
if (($handle = fopen($files[$i], "r")) !== FALSE) {
// eat the header
$header = fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data = array_combine($header, $data);
$cbsmin[] = $data["Customer Name"];
}
fclose($handle);
}
}
It beats having to use field offsets if you are doing anything non-trivial with a lot of the fields.

Categories