Find the next row fgetcsv - php

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];
}

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)

How to call a image link with an id on php by using csv file

I have a csv file with id of the image and the link of the image.
I want to make a php script that call the id and open the suitable image of the id. Now I am using this code to show the csv file.
$row = 1;
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
print_r($data);
}
fclose($handle);
}
My csv file got only two columns
with ID and Image Link
Any idea how to do it?
This is what you need:
<?php
$row = 0;
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
if ($row > 0) {
if ($data[0] == '1234') echo $data[1];
}
$row++;
}
fclose($handle);
}
$data[0] will be your check for ID, $data[1] will be your image link. I have added the row check to exclude the first row as sounds like you have your first row as the heading.
Replace:
'ID_HERE'
With:
$_POST['id'];
//or
$_GET['id'];
// or however you get your id you want to check

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

php while fgetcvs function - displaying only last x lines

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.

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