Reading CSV in PHP and out put its contents by array value - php

I want to display the contents of a CSV file by assigning it to a variable and then display it using echo statement.
This code is not working could someone point the error in it
$fh = fopen('db.csv', 'r');
$now = time();
$data=fgetcsv($fh);
$data[0]=$name;
echo $name;
Am a newbie to coding and scripting.
Thanks
This is the CSV line that I want to be printed
katz,26-11-2011,http//www.google.com

Why you first valorize the $data with an array of your CSV and then you overwrite the first position with the $name variable (which apparently is null)?
$data[0] = $name;
should be
$name = $data[0];

You could use the handy fgetcvs function for this.
Might as well post an example for want of a complete answer, shamelessly ripped from that same
manpage:
<?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);
}
?>
Hope that fits your needs. Happy coding.

Related

Scan a csv file for certain value with php

I'm just wondering is it possible to have a csv file (with many values) and scan the file for a specific value with php?
Ideally if this is possible, Id also like to get the index of that value in the file (i.e what row number the value falls on).
I'm quiet new to php so I'm not sure where to start.
All help is greatly appreciated.
Sure,
Not tested:
<?php
$search = 'mysearchstring';
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++)
{
if ($data[$c] == $search)
{
echo "Found in row $row in column {$c+1} <br />\n";
}
}
}
fclose($handle);
}
?>

PHP Get values from CSV into an array

I have the following csv file:
9 carat gold,11.87
18 carat gold,23.73
Silver,0.49
Platinum,27.52
I would like to put this in a Multidimensional Array but i have no idea how to.
Thanks for any help.
Try this code
$csv = file_get_contents($file);
$data = explode(PHP_EOL, $csv);
$arr = array();
foreach ($data as $entry) {
$arr[] = str_getcsv($entry);
}
print_r($arr);
You can go through this link for more details about str_getcsv function
You can use fgetcsv to parse a CSV file without having to worry about parsing it yourself.
Example from PHP Manual:
$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);
}
You can use fgetcsv to read a csv file open with fopen.

how to extend the length of time when fgetcsv function of php is reading csv file?

am having a problem here, i read a csv file using fgetcsv function then loop it, and write it into an xml file...when am using the script in a not so big data, everything works perfectly and writes the xml file....but when i use it in a huge csv, the xml can't finished being written and end up in an unclosed token thing
if (($handle = fopen("C:\\xampp\htdocs\yii\branch\seo_tagsdev.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 10000000, ",")) !== FALSE) {
$num = count($data);
// echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++)
{
$url = $doc->createElement('url');
$root->appendChild($url);
$loc = $doc->createElement('loc',htmlentities($data[$c]));
$url->appendChild($loc);
$lastmod = $doc->createElement('lastmod',date('Y-m-d'));
$url->appendChild($lastmod);
$freq = $doc->createElement('changefreq','daily');
$url->appendChild($freq);
$priority = $doc->createElement('priority',0.9);
$url->appendChild($priority);
// echo $data[$c] . "<br />\n";
}
$doc->save("C:\\xampp\htdocs\yii\branch\seo_tagsdev.xml");
}
i even adjusted the length parameter of fgetcsv and yet it doesn't work at all

Tools / Scripts for importing csv to db table

In tried importing a csv into a table using phpMyAdmin and I'm getting permission errors so I'm looking for any php scripts available around that I can use.
What scripts are available? Can ane point to any free one's?
fgetcsv is the php function you should use.
For details see here.
Sample code here..
$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++) {
// write insert query here
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
Thanks.

How to parse excel contents from excel document using php

I have an excel document with many columns.I want to parse that data.Please help in php.Provide simple means to guide
Thanks
look at this class
http://phpexcel.codeplex.com/
you can find an example of read here
if it saves as CSV this is how to read it:
<?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);
}
?>
I've used this in one of my projects...works like a charm :P
http://sourceforge.net/projects/phpexcelreader/

Categories