Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-07-2012, 12:27 PM   PM User | #1
runeveryday
Regular Coder

 
Join Date: Jul 2009
Posts: 152
Thanks: 8
Thanked 0 Times in 0 Posts
runeveryday can only hope to improve
why can't update the record in the 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: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
Code:
 888
b, all csv files have a column named image. 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.


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

i using the file change.php to update the record.

one csv file. http://phplist.xxmn.com/women.csv



the code:

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';
    }
}

?>
the code doesn't work how to correct it . thank u

Last edited by runeveryday; 05-07-2012 at 12:29 PM..
runeveryday is offline   Reply With Quote
Old 05-07-2012, 04:15 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I'd say stick with the csv handling, and read and write the entire block.
PHP Code:
if (false !== ($fp fopen($file'w+')))
{
    
$aData = array();
    while (
false !== ($line fgetcsv($handle)))
    {
        
$line[2] = 888;
        
$aData[] = $line;
    }
    
ftruncate($fp0);
    
rewind($fp);
    foreach (
$aData AS $fields)
    {
        
fputcsv($fp$aData);
    }
    
fclose($fp);

Give that a test.
Fou-Lu is offline   Reply With Quote
Old 05-08-2012, 02:51 AM   PM User | #3
runeveryday
Regular Coder

 
Join Date: Jul 2009
Posts: 152
Thanks: 8
Thanked 0 Times in 0 Posts
runeveryday can only hope to improve
Quote:
Originally Posted by Fou-Lu View Post
I'd say stick with the csv handling, and read and write the entire block.
PHP Code:
if (false !== ($fp fopen($file'w+')))
{
    
$aData = array();
    while (
false !== ($line fgetcsv($handle)))
    {
        
$line[2] = 888;
        
$aData[] = $line;
    }
    
ftruncate($fp0);
    
rewind($fp);
    foreach (
$aData AS $fields)
    {
        
fputcsv($fp$aData);
    }
    
fclose($fp);

Give that a test.

i put this code in. it's an infinite loop. what's wrong with my code?
Code:
<?php
$dir    = getcwd();
$files = array_diff(scandir($dir), array('..', '.','change.php'));
foreach ($files as $file) {
if (false !== ($fp = fopen($file, 'w+')))
{
    $aData = array();
    while (false !== ($line = fgetcsv($handle)))
    {
        $line[42] = 888;
        $aData[] = $line;
    }
    ftruncate($fp, 0);
    rewind($fp);
    foreach ($aData AS $fields)
    {
        fputcsv($fp, $aData);
    }
    fclose($fp);
}
}
runeveryday is offline   Reply With Quote
Old 05-08-2012, 02:44 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Nothing, I biffed it bad.
Test this one:
PHP Code:
if (false !== ($fp fopen($file'r+')))
{
    
$aData = array();
    while (
false !== ($line fgetcsv($fp)))
    {
        
$line[2] = 888;
        
$aData[] = $line;
    }
    
ftruncate($fp0);
    
rewind($fp);
    foreach (
$aData AS $fields)
    {
        
fputcsv($fp$fields);
    }
    
fclose($fp);

Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:59 AM.


Advertisement
Log in to turn off these ads.