SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Update the record in all csv file

    There are 50 csv files. each csv file have the same column's name. some content in the column are the same, some don't.

    eg:
    Code:
    test1.csv,  example1.csv, hello.csv,  world.csv.......test70.csv.
    now, i want to make a change to two column's content.

    a, all csv files have a column named qty. whose rows content are all 0. now, i want to change it into 888

    b, all csv files have a column named img. whose rows content are all

    Code:
     upload/img1.jpg
     upload/img2.jpg
     upload/img3.jpg
     upload/img01.jpg
     upload/img14.jpg
     upload/img01.jpg
    .......
    If i open each csv file then do a search and replace. i fell it too bored.

    Thank you in advance.

  2. #2
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    the csv file:

    http://phplist.xxmn.com/csv.jpg

    i want to delete Upload in the image column, change 0 to 888 in the qty column.

  3. #3
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

  4. #4
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Take a look these two articles, they may help you to get started doing the update on your own:

    http://www.homeandlearn.co.uk/php/php10p6.html
    http://alpho011.hubpages.com/hub/PHP...-into-CSV-file

    The first article shows you how to retrieve the data from the csv file. Once you got the data, you simply update each line according to your requirement and then write it back to the file. Note the example in second article is inserting or adding the data back into the file but you would overwrite your file in your case.

    If you're having trouble doing the update part then post the data you got from one of your files here and we will suggest a method to update them.

    Edit: I didn't see that you have already posted a file. Let us know if you still having trouble doing the update after reading those articles.

  5. #5
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <?php
    $dir    = getcwd();
    $files = array_diff(scandir($dir), array('..', '.','change.php'));
    foreach ($files as $file) {
        if (($handle = fopen($file, "r")) !== FALSE) {
            $new_content = '';
            while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) {
                //var_dump($data);
               $data[2]=888; 
                $new_content .= implode(',', $data);
                //var_dump($new_content);
            }
           file_put_contents($file, $new_content);
            echo 'down';
        }
    }
    
    ?>
    this is my code. why it can't work thank u.the csv file. http://phplist.xxmn.com/women.csv

  6. #6
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Try it with this code and see if the results are what you're looking for:

    PHP Code:
    <?php
       $dir   
    getcwd();
       
    $files scandir($dir);
       foreach (
    $files as $file)
       {
           
    $parts pathinfo($file);
           if(
    $parts['extension']!="csv") continue;
           if ((
    $handle fopen($file"r")) !== FALSE)
           {
               
    $new_content '';
               while ((
    $data fgetcsv($handle100000",")) !== FALSE)
               {
                   
    $data[2]=888
                   
    $new_content .= implode(','$data)."\n";
               }
               
    fclose($handle);
               
    file_put_contents($file$new_content);
           }
       }
    ?>
    Note that the files to be updated must have csv file extension; also the first line (title) will also get the update.

  7. #7
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by runeveryday View Post
    [CODE]<?php
    ...

    this is my code. why it can't work thank u.the csv file. http://phplist.xxmn.com/women.csv
    I took a second look and see why yours did not work - the end of line character (\n) is missing. Try that and see if it works with the change.

  8. #8
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    i put your code in, it changed the header. how to exclude the header?

  9. #9
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Here is one way:
    PHP Code:
    <?php
       $dir   
    getcwd();
       
    $files scandir($dir);
       foreach (
    $files as $file)
       {
           
    $parts pathinfo($file);
           if(
    $parts['extension']!="csv") continue;
           if ((
    $handle fopen($file"r")) !== FALSE)
           {
               
    $new_content =  implode(','fgetcsv($handle100000","))."\n";
               while ((
    $data fgetcsv($handle100000",")) !== FALSE)
               {
                   
    $data[2]=888
                   
    $new_content .= implode(','$data)."\n";
               }
               
    fclose($handle);
               
    file_put_contents($file$new_content);
           }
       }
    ?>

  10. #10
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    i am sorry. it still can't work. if you test the code to phplist.xxmn.com/women.csv this csv file. you will be find. the csv content are all disorderd

  11. #11
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You’re showing me a file that is entirely different than the one in discussion.

  12. #12
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    http://phplist.xxmn.com/women.csv you can test it. thank you

  13. #13
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your original file has the following:

    store,image,qty,min_qty,use_config_min_qty,is_qty_decimal,backorders,use_config_backorders,min_sale_qty,use_config_min_sale_qty,max_sale_qty,use_config_max_sale_qty,is_in_stock

    While the last one has this:

    store,websites,attribute_set,type,category_ids,sku,has_options,name,url_key,gift_message_available,meta_title,meta_description,image,small_image,thumbnail,gallery,options_container,page_layout,image_label,small_image_label,thumbnail_label,url_path,price,special_price,cost,weight,status,tax_class_id,visibility,enable_googlecheckout,is_recurring,is_imported,description,short_description,meta_keyword,custom_layout_update,special_from_date,special_to_date,news_from_date,news_to_date,custom_design_from,custom_design_to,qty,min_qty,use_config_min_qty,is_qty_decimal,backorders,use_config_backorders,min_sale_qty,use_config_min_sale_qty,max_sale_qty,use_config_max_sale_qty,is_in_stock,low_stock_date,notify_stock_qty,use_config_notify_stock_qty,manage_stock,use_config_manage_stock,stock_status_changed_automatically,use_config_qty_increments,qty_increments,use_config_enable_qty_increments,enable_qty_increments,product_name,store_id,product_type_id,product_status_changed,product_changed_websites

    You want to update qty ($data[2]), but the column for the last one is attribute_set. Those two files are not the same.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •