Question

 public function loadCSV()
 {

                $file  = fopen($this->path, 'r');
                $dbcnt = $this->getdbrows();


                while(!feof($file))
                {

                        if(fgetcsv($file)[$this->group] == '610')
                        {

                                $date   = fgetcsv($file)[$this->datePos];
                                $num    = $this->formatNum(fgetcsv($file)[$this->numPos]);
                                $rec    = fgetcsv($file)[$this->recPos];

                                if(!(in_array($num, $this->filter)))
                                {

                                        array_push($this->csvitem, $date . ';' . $num . ';' . $rec);

                                }
                        }

                }

        $this->writeCSV();

        }

So I load a csv file using fopen, then I go through it line by line and save date, num and rec to their variables. However, since I read the same file over and over again. I wish not to store old data over and over again. Therefor I get the number of db rows with $dbcnt and then I wish to start at that row.

You have any ideas?

No correct solution

OTHER TIPS

I would use SplFileObject. It is very fast and supports a nice linebased seek-feature :

public function loadCSV() {
    $file = new SplFileObject($this->path);
    $dbcnt = $this->getdbrows();
    $file->seek($dbcnt); // <---- here

    while (!$file->eof()) {
        $record = $file->fgetcsv();
        if ($record[$this->group] == '610') {
        .. and so on..

This link from @ex3v did help me How do I open a file from line X to line Y in PHP?

All I needed a part from $dbcnt was an if statement saying that

if($dbcnt >= $line)
{

}

Where $line is a counter for each irretation of fgetcsv

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top