Encapsulating and reading into an array from text file - php

I'm trying to make 2 classes, to encapsulate these text files, Customer.txt and Orders.txt. The customer one I need to read each line into a separate instance of an array for each line of text, how can I do this?
Also, do I have to do something about the comma's in the text files?
I know I need to use fopen, just not sure how to go about doing it.
This is what I've tried so far.
<?php
class readOrders{
$array_of_lines = fopen("orders.txt", 'r') or die("Failed to create file");
foreach($array_of_lines as $line) {
$pieces_of_line = explode(',', $line);
}
class readCustomers{
$array_of_more_lines = fopen("customers.txt", 'r') or die("Failed to create file");
foreach($array_of_more_lines as $line) {
$pieces_of_line = explode(',', $line);
}
?>
Here are my text files
Orders.txt
1,4,0133360903,Building Java Programs,Computer Science
2,6,0321836995,Mathematics All Around,Mathematics
3,24,0321825721,Mathematics for Elementary Teachers with Activity Manual,Mathematics
4,10,0133011208,Business Math,Business
customers.txt
2,Leonie,Köhler,leonekohler#surfeu.de,University of Stuttgart,Theodor-Heuss-Straße 34,Stuttgart,,Germany,70174,+49 0711 2842222
3,Bjørn,Hansen,bjorn.hansen#yahoo.no,University of Oslo,Ullevålsveien 14,Oslo,,Norway,0171,+47 22 44 22 22
4,François,Tremblay,ftremblay#gmail.com,McGill University,1498 rue Bélanger,Montréal,QC,Canada,H2G 1A7,+1 (514) 721-4711

I think fgetcsv will be useful here, example from php-doc:
<?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);
}
?>

Related

Open csv file improper encoding

When I open the csv file
$str = file_get_contents($filename);
$str = mb_convert_encoding($str,"utf-8","sjis");
var_dump($str);
It shows like this
\0000\000.\0000\0000\0008\0000\0005\0007\000,\0000\000.\0000\0001\0000 ....
At first I think , this is the problem of encoding, so I added mb_convert_encoding but in vain.
What cause this improper encoding?
Better you can do like this:
$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);
}
Code copied from :http://php.net/manual/en/function.fgetcsv.php
alternatively : http://php.net/manual/en/function.str-getcsv.php

Reading CSV file data in PHP

I'm trying to create a page which pulls infoprmation from a CSV file, using PHP.
It should then populate a product list with the information.
This is the CSV file:
The code I have is as follows:
$file = file('data-example.csv');
$line = array("name", "id","price", "description");
foreach($file as $line) {
$csv = explode('//',$line);
$item = $csv;
print_r($item);
This is currently printing out the whole of the array. I'd like to know how I can access the individual nodes on the array, so that I could include them as variables within an HTML table.
Thanks
Ali
To amplify, and respond to the replies (thanks!):
This is the amended code, using fgetcsv:
if (($handle = fopen("data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo($data[1][1]);
}
fclose($handle);
}
This seems to work, but returns the column contents. This is the data (data.csv):
"Dandelion","30","13.99","Dandelion 30 "
"Daisy","40","20.99","Daisy 40 "
"Cress","27","10.99","Cress 27 "
"Coral","17","6.99","Coral 17 ",
"Honeysuckle","40","17.99","Honeysuckle 40 "
"Rose","27","9.99","Rose 27"
"
(there's a new line at dandelion, Daisy,Cress,Coral,Honeysuckle and Rose)
When I try echo($data[1]); it gets the whole column as described, i.e 30 40 27 17 40 27 but echo($data[1][1]); returns "342142", and echo($data[1][1][1]); creates an error.
I don't understand it as this is an array?
Thanks
Ali
You should use native php functions to read csv files, such as str_getcsv
Something like:
$Data = str_getcsv($CsvString, "\n"); //parse the rows
foreach($Data as &$Row){
$Row = str_getcsv($Row, ";"); //parse the items in rows
}
Alternatively you can use fgetcsv that parses the line it reads for fields in CSV format and returns an array containing the fields read, i.e.:
<?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);
}
?>

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.

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