Previously I had two tables. I have a php script to copy the contents of one table one to table 2, to identify the ids which are copied I made a csv file to check for the last row inserted id like below
"Date",lastid(int)
Previously I was checking the last id using the index of csv by using fopen as 0 and 1.
But now I have another table 3 to copy to table 2 and the process is same. but When i write for the new table it gets overwritten so I cant identifiy for the previous table. If i append it the file will get extended. Is there any way to put the table 1 contents in the first line and table two last id in the second line as
"Date",lastid(int), // for t1 copied to t2
"Date",lastid(int) // for t3 copied to t2
and taking 0, 1 for table 1 and 2,3 for table 2. Even when table 2 is done first.
public function writeNumberOfRowsCopiedForTable2FromTable1($row_write)
{
$file = fopen('rowsCopied.csv', 'w+');
fputcsv($file, $row_write);
fclose($file);
}
calling it by
writeNumberOfRowsCopiedForTable2FromTable1([$this->global_date, $last_row_copied]);
getting values for inserted values in t2 from t1
public function getLastRowCopied()
{
$end_row_id = 0;
if (file_exists('rowsCopied.csv')) {
$handle = fopen('rowsCopied.csv', 'r');
$data = fgetcsv($handle);
$end_row_id += intval($data[1]);
$this->global_date = $data[0];
fclose($handle);
}
return $end_row_id;
}
Hope I explained the question. Someone knows the answer?
I've written a couple of methods which should do what your after. You will need to integrate them with how you currently use them, but this is just so I can test them for myself.
Each method takes a $rowNumber
parameter, this is the row in the stored file that you want to set/get the value for. In the code it adjusts it, so row 1 is actually element 0 of the array. I've used file()
to read the whole file in and then get/set the row your interested in. In the writeNumberOfRowsCopied
method it then uses file_put_contents()
to write the whole file out in one go (imploding the lines of the file).
function writeNumberOfRowsCopied($rowNumber, $row_write)
{
$fileName = 'rowsCopied.csv';
$file = file($fileName, FILE_IGNORE_NEW_LINES);
$file[$rowNumber-1] = implode(",", $row_write);
file_put_contents($fileName, implode(PHP_EOL, $file));
}
function getLastRowCopied($rowNumber)
{
$fileName = 'rowsCopied.csv';
$file = file($fileName, FILE_IGNORE_NEW_LINES);
$row = explode(",", $file[$rowNumber-1]);
return (int)$row[1];
}
writeNumberOfRowsCopied(2, ["aa", 22121]);
print_r(getLastRowCopied(2));