2

Header of csv is date, eav, ess, etc ... the downloads i track. I have a function that gets a paramenter: ex: eav. I activate function dl_stats("eav"); in the page that handles eav. It should get the number of downloads of today ex: 10 and adds one to 11. etc... If the date in csv is not today created a new line starting from 1. The problem is that it works ok in present day, but after midnight is breaks. does not go on new line, continues on same line.

I spent 3 days trying to fix it but i cant find the solution.

Here is the code.

function dl_stats($produs){
$dir = "stats/"; // file directory - edit directory if you run function from diferent folder
$file = $dir."dl.csv"; // file  -

$file = file($file);
//$file = array_reverse($file); // reverse so last dl is first

$data_array = array();
foreach($file as $f){ // file is csv and is exploded by ,
    $data_array[] = explode(",",$f);
}

// variables
$csv_content = '';
$data = strftime("%d-%m-%Y", time()); // define today
$data = strval($data);
$new_data = false;

$header = array("data","eav","ess","ems","qr", "ecs","ecsp"); // associative array header

$products = array("eav","ess","ems","qr", "ecs","ecsp");
$product = array();

// loop array 
foreach($data_array as $content){

    $content = array_combine($header, $content); // combine to make associative array

    // look to see if the date in csv is the same as today, if not a new row must be created with the new date
    if($data == $content['data']){
        $new_data = false; // if false the date is not changed and will be added +1 to the product
    }else{
        $new_data = true; // if true, new date + zero all dl that day
    }

    //$produs = 'eaven32';
    // set product to add 1 
    foreach($products as $key){
        if($produs == $key){
            $product[$key] = '1';
        }else{
            $product[$key] = '0';
        }
    }

    if($content['data'] == 'data'){
        // do not display the header row
        $csv_content.=  $content["data"] . "," . $content["eav"] . "," . $content["ess"] . "," . $content["ems"] . "," . $content["qr"] . "," . $content["ecs"] . "," . $content["ecsp"];
    }else{
        if($data == $content['data']){
            $csv_content.=  $content["data"] . "," . 
            ($content["eav"] + $product['eav']) . "," . 
            ($content["ess"] + $product['ess']) . "," . 
            ($content["ems"] + $product['ems']) . "," . 
            ($content["qr"] + $product['qr'])   . "," . 
            ($content["ecs"] + $product['ecs']) . "," . 
            ($content["ecsp"] + $product['ecsp']) . "\n";
        }else{
            $csv_content.=  $content["data"] . "," . $content["eav"] . "," . $content["ess"] . "," . $content["ems"] . "," . $content["qr"] . "," . $content["ecs"] . "," . $content["ecsp"];
        }
    }
} // eof foreach($data_array as $content)


$csv = $dir."dl.csv"; // file


if($new_data == false){
    file_put_contents($csv, $csv_content);
}else{
    $prod = implode(",", $product); // implode to get product values
    $csv_content_new = $data . "," . $prod;
    file_put_contents($csv, $csv_content . $csv_content_new);
}

}

Maybe if someone else looks at the code for the first time, can find the solution.

Thanks in advance.

Edit:

The csv format looks like this.

first line(suposed to be the header): every data in it's separate collumn

date | eav | ess | ems | qr | ecs | escp

second and every other lines bellow (suposed to be the data that increments 1 )

18-07-2013| 38 | 50 | 4 | 0 | 2 | 2

If i run it now with this date, will add another row as intended, like so: 05-09-2013| 38 | 50 | 4 | 0 | 2 | 2 but if i let it till tomorrow... this will happen

date | eav | ess | ems | qr | ecs | escp

18-07-2013| 38 | 50 | 4 | 0 | 2 | 2

05-09-2013| 38 | 50 | 4 | 0 | 2 | 206-09-2013| 38 | 50 | 4 | 0 | 2 | 2

The date will come after last row and last column of previous day and not on a new line. I think you can figure it out only if u run it on your own server. And it might trick you that it works fine but the next day csv is messed up :) happened to me 3 days in a row with multiple modifications.

Last Edit:

Ok the code is corect, i think it was a problem with the csv file. If you change to if($new_data == false){ to if($new_data != false){ a new date it will make 2 dates with date of today, and then swith back if($new_data != false){ to if($new_data == false){ and csv is fixed somehow :), it works, and if anyone want to use my code can do it freely. I have a file that displays the csv file in php, if you want it just give me a message.

George
  • 63
  • 1
  • 8
  • Does it *ever* go to a new line? Did you check your timezone settings on the server? – Jason McCreary Sep 05 '13 at 11:46
  • Yes it goes on a new line as intended, but only the next day will not go... this is very strange and confusing, cause if you change date in csv to be the day before it works fine. – George Sep 05 '13 at 13:30

1 Answers1

0

Either in this line (apparently it's not the culprit, but you never know):

if($data == $content['data']){

the condition is either triggering all the time, which causes $new_data to be always false

or this code (triggered by $new_data == true):

$prod = implode(",", $product); // implode to get product values
$csv_content_new = $data . "," . $prod;
file_put_contents($csv, $csv_content . $csv_content_new);

isn't working as you expect.


To check the first part:

try using var_dump($data) and var_dump($content['data']) to see if they are ever different.

To check the second part:

Try inverting your if($new_data == false){ to if($new_data == true){ and see if your code actually writes the new line.

victorantunes
  • 1,141
  • 9
  • 20
  • if($data == $content['data']){ is set to if($data != $content['data']){ it will create a new line with today date and adds one as expected... the thing is code works perfect... if i change the date in csv as being yesterday, adds date of today as expected, but when midnight comes something is wrong cant figure out what... i tried all you sugested before posting... – George Sep 05 '13 at 13:09