Search code examples
phpwordpressexport-to-csvqtranslate

Merge 2 rows in csv file and creating a new CSV to store the merged data with PHP?


Good day. I'm having a csv file with the structure like this

Id |    ArticleId  | LanguageId | Title             | Content
----------------------------------------------------------------------
1         2              1        Francais Title    Francais Content
2         2              2        English Title      English Content

How can I merge an article to my expecting result ? Expect(Note : the [:fr] and [:en] are shortcodes for q-translate wordpress, I just have to define this so that I can import into database)

ArticleId     | Title                       | Content
------------------------------------------------------------------
      2       |  [:fr]Francais Title[:en]  |   [:fr]Francais Content
              |   English Title            |    [:en]English Content

My current php code in the generate file.

<?php 
$row = 1;
if (($handle = fopen("dbo.ArticleTranslation.csv", "r")) !== FALSE) {
    $query = '';
    $flag = true;
    while (($data = fgetcsv($handle, 2000, ",")) !== FALSE) {
        if($flag) { $flag = false; continue; }
        $num = count($data);

        $content = '';
        $title = '';

        if ($data[2] == '1') {
            $content .= '[:fr]' . $data[4];
            $title .= '[:fr]' . $data[3];

        } else {
            $title .= '[:en]' . $data[3];
            $content .= '[:en]' . $data[4];
        }
        //ie(var_dump($content));
        //var_dump($content);

       var_dump($data);

        $row++;
       // var_dump($mergeContent);
    }
    fclose($handle);
} 
?>

Solution

  • Here is a solution : first you have to create an empty array which will contain the extracted data. then, you loop over the csv lines. for each line, you extract the article data. if the article is already existing you just append the lang to the array. if not, you create the article into the result array.

    if (($handle = fopen("dbo.ArticleTranslation.csv", "r")) !== false) {
        $extractedData = [];
        fgetcsv($handle, 2000, ","); // skip first line
        while (($line = fgetcsv($handle, 2000, ",")) !== false) {
            $articleId = $line[1];
            $lang = $line[2];
            $title = $line[3];
            $content = $line[4];
    
            // convert lang id to code
            if ($lang == 1) $lang = '[:fr]';
            if ($lang == 2) $lang = '[:en]';
    
            // build string with lang code
            $content = $lang.$content;
            $title = $lang.$title;
    
            // check if an article with this id was already extracted
            if (array_key_exists($articleId, $extractedData)) {
                // append lang to existing article
                $extractedData[$articleId]['Content'] .= $content;
                $extractedData[$articleId]['Title'] .= $title;
            } else {
                // add new article
                $extractedData[$articleId] = [
                    'ArticleId' => $articleId,
                    'Content' => $content,
                    'Title' => $title
                ];
            }
        }
        fclose($handle);
    }