Page 1 of 1
2 CSV files in one.
Posted: Fri Jun 13, 2008 6:46 am
by stefanos
Hello everybody,
I am new in php and i have a problem.
I have two csv files. In one there is
Code: Select all
id decription manufactor price etc...
1243 keyboard microsoft 12,45$
5543 tft-lcd lg 220$
3245 mouse microsoft 4$
879 router asus 50$
In the other one i have
id thumpnail photo
2214 http://.... http://...
1243 http://.... http://....
5444 http://.... http://....
I want to put the column of thumpnail and photo at the end of the first cvs with base the right id.
Thanks a lota
Re: 2 CSV files in one.
Posted: Fri Jun 13, 2008 9:26 am
by WebbieDave
What problems are you running into when coding the solution? Can you post the relevant parts of the code?
Re: 2 CSV files in one.
Posted: Fri Jun 13, 2008 10:45 am
by stefanos
Unfortunately i have no write the code because i am beginner in php, so i wonder if someone of you can drive me to start right! Thaks a lot!
Re: 2 CSV files in one.
Posted: Fri Jun 13, 2008 11:33 am
by RobertGonzalez
You want to add a column in the first file with data from the second?
Re: 2 CSV files in one.
Posted: Fri Jun 13, 2008 11:40 am
by stefanos
Yes, i want to have in the first file all the information about one product.The problem is that there are not only same product in two files, because some products haw photos some others not. So i have to search the id of first file compare with second and if they are the same, add the two columns at the end of the first. The final result i want to be :
id decription, manufactor, price, thumpnail, photo
in order to can insert to eshop.
Re: 2 CSV files in one.
Posted: Fri Jun 13, 2008 12:17 pm
by Luke
I wrote a csv library that may be of some assistance to you here. It is only at version 0.2 but it is perfectly capable of what you need...
Code: Select all
$reader1 = new Csv_Reader("./data/info.csv");
$reader2 = new Csv_Reader("./data/images.csv");
$writer = new Csv_Writer("./data/all.csv");
foreach ($reader1 as $info_row) {
foreach ($reader2 as $image_row) {
// key zero is the id, so compare
if ($info_row[0] == $images_row[0]) {
// make a copy of info row
$newrow = $info_row;
// add images to new row
$newrow[] = $image_row[1];
$newrow[] = $image_row[2];
// write data to new csv file
$writer->writeRow($newrow);
}
}
}
// write data to file
$writer->close();
download the library and play around with it a bit... you'll get the hang of it.
http://code.google.com/p/php-csv-utils/downloads/list
Some docs:
http://www.mc2design.com/blog/php-csv-u ... csv-module
http://www.mc2design.com/blog/php-csv-u ... n-released
Re: 2 CSV files in one.
Posted: Fri Jun 13, 2008 1:43 pm
by stefanos
First of all thank you for the quick reply.
I upload th folder "php-csv-utils-0.2" i go to the .../.../test/ and there is the below error
Warning: require_once(simpletest/unit_tester.php) [function.require-once]: failed to open stream: No such file or directory in /home/www/betinfo/aboutman.eu/www/php/php1/tests/index.php on line 47
Fatal error: require_once() [function.require]: Failed opening required 'simpletest/unit_tester.php' (include_path='.:/usr/local/share/pear:/home/www/betinfo/aboutman.eu/www/php-csv-utils-0.2/php-csv-utils') in /home/www/betinfo/aboutman.eu/www/php/php1/tests/index.php on line 47
Sorry if it something simple but i am beginner....
Re: 2 CSV files in one.
Posted: Fri Jun 13, 2008 2:01 pm
by RobertGonzalez
Comment out any lines that look like they are including anything related to Simpletest.
Re: 2 CSV files in one.
Posted: Sat Jun 14, 2008 3:21 pm
by stefanos
Because i want first of all to understand the philosophy to works with libraries. Would you like to tell me what i have to do. Except to write code i have to upload the folder of library and after to paly in folder test?