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
Related
I have updated my question post with the code that currently have. Hope someone can help me.
I can't seem to figure out how to put all the values from the CSV to an HTML table
<?php
$files = glob("./data/*.csv");
foreach($files as $file) {
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 4096, ",")) !== FALSE) {
echo implode("\t", $data);
}
echo "<br>";
fclose($handle);
} else {
echo "Could not open file: " . $file;
}
}
?>
<?php
$files = glob("./data/*.csv");
$display_csv = array();
foreach($files as $file) {
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$xyz = array('company_name'=>$data[0],'case_id'=>$data[1]);
array_push($display_csv, $xyz);
}
fclose($handle);
}else
{
echo "Could not open file: " . $file;
}
}
echo "<table class='table table-dark table-striped' id='call_csv' border='2'><thead><tr><th>Company Name</th><th>Case ID</th></tr></thead><tbody>";
foreach($display_csv as $row){
echo "<tr><td>" . $row['company_name'] . "</td>";
echo "<td>" . $row['case_id'] . "</td></tr>";
}
echo "</tbody></table>";
?>
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];
}
}
}
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];
}
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.
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.