How do I retrieve data csv just a few lines with php - php

I have a CSV file the results of scanning. I just want to read the first column and read the lines up to and including line 6.
This is what I have tried:
<html>
<body>
<h1>MAC Address have been verified</h1>
<table width="50%" border="1">
<tr>
<th>NO</th>
<th>MAC Address</th>
</tr>
<?php
if (($handle = fopen("data.csv", "r")) !== FALSE) {
$row = 1;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo "<tr>";
echo "<td><center>".$row++."</center></td>";
echo "<td>".$data[0]."</td>";
echo "</tr>";
} //end while
fclose($handle);
} //end if
?>
</table>
</body>enter code here
</html>

The important line below is
if ( $row > 6 ) break;
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ( $row > 6 ) break;
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>

Related

fgetcsv return not exact values

I'm trying to read this value "<! PC:IDJLOGIN; >" but I get this one IDJLOGIN
whith ths code ,(its the code of the php doc),I have tried with utf-8 and ANSI codification
$row = 1;
if (($handle = fopen($url, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}

PHP color of the first line in a loop

Im trying to read a txt file and show the content via PHP. Loading it and displaying it is working fine. I would like the different words to have a different color. But the first line never gets the correct color.
<HTML>
<HEAD>
</HEAD>
<BODY>
<?php
$fontcolor=Black;
$row = 1;
echo "<TABLE width=100%>";
echo "<TD>";
### State section
if (($handle = fopen("sdmon12images\State.txt", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
if ($data[$c] == "Busy"):
$fontcolor=Red;
endif;
if ($data[$c] == "Pause"):
$fontcolor=Blue;
endif;
if ($data[$c] == "Idle"):
$fontcolor=Green;
endif;
if ($data[$c] == "Post processing"):
$fontcolor=Orange;
endif;
echo "<font color=$fontcolor>" . $data[$c] . "<br />\n";
$fontcolor=Black;
}
}
fclose($handle);
}
echo "</TD>";
echo "</TABLE>";
?>
</HTML>
The result is that the first line is standard color and the other lines are colored correctly. How do i get the first line colored in the correct color?
I think this should work:
<HTML>
<HEAD>
</HEAD>
<BODY>
<?php
$fontcolor="Black";
$row = 1;
echo "<table width='100%'>";
echo "<td>";
### State section
if (($handle = fopen("sdmon12images\State.txt", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
if ($data[$c] == "Busy"):
$fontcolor="Red";
endif;
if ($data[$c] == "Pause"):
$fontcolor="Blue";
endif;
if ($data[$c] == "Idle"):
$fontcolor="Green";
endif;
if ($data[$c] == "Post processing"):
$fontcolor="Orange";
endif;
echo "<font color='$fontcolor'>" . $data[$c] . "<br />\n";
$fontcolor="Black";
}
}
fclose($handle);
}
?>

how to read Open Financial Exchange (OFX) file and send to php pages

how can i read ofx(Open financial exchange) with php that is with csv format
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>

Show CSV rows in reverse order with PHP

Trying to get this code to echo the rows it gets from the CSV file in reverse order. Any idea how to do this?
<?php
$row = 1;
$FILE = "file.csv";
if (($handle = fopen($FILE, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
if ($c == 0) $first = $data[$c];
if ($c == 1) $second = $data[$c];
}
}
fclose($handle);
}
?>
Create a specific function for this task and call the function. It is always easier to split your program into smaller pieces. You need to read the data into an array and the reverse the order.
<?php
function loadCSV($file) {
$rows = array();
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
array_push($rows, $data);
}
fclose($handle);
}
return array_reverse($rows);
}
?>
Here is how to use this function:
<?php
$data = loadCSV('data.csv');
print_r($data);
?>

How to parse CSV files in PHP

I want to stock some fields in MySQL coming from Excel but I'm not arrived to cut the lines from Excel in PHP
example:
123,15,0,01/01/2000,456462ABCD,,,
any help please ?
Just use the function for parsing a CSV file
http://php.net/manual/en/function.fgetcsv.php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}

Categories