2 CSV files in one.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stefanos
Forum Newbie
Posts: 5
Joined: Fri Jun 13, 2008 6:15 am

2 CSV files in one.

Post 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
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: 2 CSV files in one.

Post by WebbieDave »

What problems are you running into when coding the solution? Can you post the relevant parts of the code?
stefanos
Forum Newbie
Posts: 5
Joined: Fri Jun 13, 2008 6:15 am

Re: 2 CSV files in one.

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: 2 CSV files in one.

Post by RobertGonzalez »

You want to add a column in the first file with data from the second?
stefanos
Forum Newbie
Posts: 5
Joined: Fri Jun 13, 2008 6:15 am

Re: 2 CSV files in one.

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: 2 CSV files in one.

Post 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
stefanos
Forum Newbie
Posts: 5
Joined: Fri Jun 13, 2008 6:15 am

Re: 2 CSV files in one.

Post 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....
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: 2 CSV files in one.

Post by RobertGonzalez »

Comment out any lines that look like they are including anything related to Simpletest.
stefanos
Forum Newbie
Posts: 5
Joined: Fri Jun 13, 2008 6:15 am

Re: 2 CSV files in one.

Post 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?
Post Reply