Content |
Deleted: I have a CSV file which has a lot of columns, and hundreds of rows.
| Added: Base on the requirement of my client, I have to create a CSV file which has a lot of columns, and hundreds of rows.
|
Unchanged: The header looks like this: | Unchanged: The header looks like this: |
Unchanged: SKU | category | description | image | small_image | thumbnail | price | color | Unchanged: SKU | category | description | image | small_image | thumbnail | price | color |
Deleted: I want to read the data from other files and put the suitable data in the correct column.
| Added: I need to read the data from other files and put the suitable data in the correct column.
|
Deleted: I have tried many times and many different ways. Finally, I found out a way to do it quickly, here is how I did with PHP:
| Added: I tried many different ways but it didn't work. Finally, I found out a way to do it quickly, here is how I did with PHP:
|
Unchanged: At first, I break out the columns into an array precisely as they appear in the raw CSV file: | Unchanged: At first, I break out the columns into an array precisely as they appear in the raw CSV file: |
Unchanged: <pre class="lang-php prettyprint prettyprinted" ><code><span class="pln">$columns </span><span class="pun">=</span> <span class="pun">[</span><span class="str">" sku"</span><span class="pun">,</span><span class="str">" category"</span><span class="pun">,</span><span class="str">" description"</span><span class="pun">,</span><span class="str">" image"</span><span class="pun">,</span><span class="str">" small_image"</span><span class="pun">,</span><span class="str">" thumbnail"</span><span class="pun">,</span> <span class="str">" price"</span><span class="pun">,</span><span class="str">" color"</span><span class="pun">]; </span></code></pre> | Unchanged: <pre class="lang-php prettyprint prettyprinted" ><code><span class="pln">$columns </span><span class="pun">=</span> <span class="pun">[</span><span class="str">" sku"</span><span class="pun">,</span><span class="str">" category"</span><span class="pun">,</span><span class="str">" description"</span><span class="pun">,</span><span class="str">" image"</span><span class="pun">,</span><span class="str">" small_image"</span><span class="pun">,</span><span class="str">" thumbnail"</span><span class="pun">,</span> <span class="str">" price"</span><span class="pun">,</span><span class="str">" color"</span><span class="pun">]; </span></code></pre> |
Unchanged: Then I open the file and iterate over it, building an associative array of key-value pairs, checking whether or not a file exists for that image name, and if so, assigning the correct name to the array. | Unchanged: Then I open the file and iterate over it, building an associative array of key-value pairs, checking whether or not a file exists for that image name, and if so, assigning the correct name to the array. |
Unchanged: <pre class="lang-php prettyprint prettyprinted" ><code><span class="pln">$rootDir </span><span class="pun">=</span> <span class="str">" "</span><span class="pun">;</span> <span class="com">//Root directory where the image files are located</span><span class="pln"> | Unchanged: <pre class="lang-php prettyprint prettyprinted" ><code><span class="pln">$rootDir </span><span class="pun">=</span> <span class="str">" "</span><span class="pun">;</span> <span class="com">//Root directory where the image files are located</span><span class="pln"> |
Unchanged: $file </span><span class="pun">= </span><span class="pln"> fopen</span><span class="pun">( </span><span class="str">" filehandle.csv" </span><span class="pun">,</span> <span class="str">" r"</span><span class="pun">);</span> <span class="com">//Open the old file for reading</span><span class="pln"> | Unchanged: $file </span><span class="pun">= </span><span class="pln"> fopen</span><span class="pun">( </span><span class="str">" filehandle.csv" </span><span class="pun">,</span> <span class="str">" r"</span><span class="pun">);</span> <span class="com">//Open the old file for reading</span><span class="pln"> |
Unchanged: $newFile </span><span class="pun">= </span><span class="pln"> fopen</span><span class="pun">( </span><span class="str">" newfilehandle.csv" </span><span class="pun">,</span> <span class="str">" w"</span><span class="pun">);</span> <span class="com">//Create a new file for writing</span> | Unchanged: $newFile </span><span class="pun">= </span><span class="pln"> fopen</span><span class="pun">( </span><span class="str">" newfilehandle.csv" </span><span class="pun">,</span> <span class="str">" w"</span><span class="pun">);</span> <span class="com">//Create a new file for writing</span> |
Unchanged: <span class="kwd">while</span> <span class="pun">( (</span><span class="pln">$data </span><span class="pun">= </span><span class="pln"> fgetcsv</span><span class="pun">( </span><span class="pln">$file</span><span class="pun">))</span> <span class="pun">!= =</span><span class="pln"> FALSE</span><span class="pun">)</span> <span class="pun">{</span><span class="pln"> | Unchanged: <span class="kwd">while</span> <span class="pun">( (</span><span class="pln">$data </span><span class="pun">= </span><span class="pln"> fgetcsv</span><span class="pun">( </span><span class="pln">$file</span><span class="pun">))</span> <span class="pun">!= =</span><span class="pln"> FALSE</span><span class="pun">)</span> <span class="pun">{</span><span class="pln"> |
Unchanged: $row </span><span class="pun">= </span><span class="pln"> array_combine</span><span class="pun">( </span><span class="pln">$columns</span><span class="pun">,</span><span class="pln"> $data</span><span class="pun">) ;</span><span class="pln"> | Unchanged: $row </span><span class="pun">= </span><span class="pln"> array_combine</span><span class="pun">( </span><span class="pln">$columns</span><span class="pun">,</span><span class="pln"> $data</span><span class="pun">) ;</span><span class="pln"> |
Unchanged: $filename </span><span class="pun">=</span> <span class="str">" {$row['sku']}.jpeg" </span><span class="pun">;</span> | Unchanged: $filename </span><span class="pun">=</span> <span class="str">" {$row['sku']}.jpeg" </span><span class="pun">;</span> |
Unchanged: <span class="kwd">if</span> <span class="pun">( </span><span class="pln">file_ exists</span><span class="pun">( </span><span class="str">" $rootDir/$filename" </span><span class="pun">))</span> <span class="pun">{</span><span class="pln"> | Unchanged: <span class="kwd">if</span> <span class="pun">( </span><span class="pln">file_ exists</span><span class="pun">( </span><span class="str">" $rootDir/$filename" </span><span class="pun">))</span> <span class="pun">{</span><span class="pln"> |
Unchanged: $row</span><span class="pun">[</span><span class="str">'image'</span><span class="pun">]</span> <span class="pun">= </span><span class="pln"> $filename</span><span class="pun">; </span><span class="pln"> | Unchanged: $row</span><span class="pun">[</span><span class="str">'image'</span><span class="pun">]</span> <span class="pun">= </span><span class="pln"> $filename</span><span class="pun">; </span><span class="pln"> |
Unchanged: $row</span><span class="pun">[</span><span class="str">'small_ image'</span><span class="pun">]</span> <span class="pun">= </span><span class="pln"> $filename</span><span class="pun">; </span><span class="pln"> | Unchanged: $row</span><span class="pun">[</span><span class="str">'small_ image'</span><span class="pun">]</span> <span class="pun">= </span><span class="pln"> $filename</span><span class="pun">; </span><span class="pln"> |
Unchanged: $row</span><span class="pun">[</span><span class="str">'thumbnail'</span><span class="pun">]</span> <span class="pun">= </span><span class="pln"> $filename</span><span class="pun">;</span> | Unchanged: $row</span><span class="pun">[</span><span class="str">'thumbnail'</span><span class="pun">]</span> <span class="pun">= </span><span class="pln"> $filename</span><span class="pun">;</span> |
Unchanged: <span class="pun">}</span><span class="pln"> | Unchanged: <span class="pun">}</span><span class="pln"> |
Deleted: fputcsv</span><span class="pun">( </span><span class="pln">$newFile</span><span class="pun">,</span><span class="pln"> array_values</span><span class="pun">( </span><span class="pln">$row</span><span class="pun">));</span> <span class="com">//write data into new file</span>
| Added: fputcsv</span><span class="pun">( </span><span class="pln">$newFile</span><span class="pun">,</span><span class="pln"> array_values</span><span class="pun">( </span><span class="pln">$row</span><span class="pun">));</span> <span class="com">//Write data into new file</span>
|
Unchanged: <span class="pun">}</span><span class="pln"> | Unchanged: <span class="pun">}</span><span class="pln"> |
Unchanged: fclose</span><span class="pun">( </span><span class="pln">$file</span><span class="pun">) ;</span><span class="pln"> | Unchanged: fclose</span><span class="pun">( </span><span class="pln">$file</span><span class="pun">) ;</span><span class="pln"> |
Deleted: fclose</span><span class="pun">( </span><span class="pln">$newFile</span><span class="pun">);
| Added: fclose</span><span class="pun">( </span><span class="pln">$newFile</span><span class="pun">) ;</span></code></pre>
|
Deleted: </span></code> | |
Deleted: Hope it is useful for you 🙂</pre>
| Added: Hope it is useful to you 🙂
|
Deleted: | |
No comments yet.