Content |
Deleted: <p> | |
Deleted: I have a CSV file which has 14 columns, and hundreds of rows. There is a header which is "SKU" ,"category"," description" ,"brand" etc. | |
Deleted: </p> | |
Deleted: <p> | |
Deleted: All data is already there but I'm trying to add some image filenames inside the CSV, in some specific columns ( "images", "small_image", "thumbnail" ). | |
Deleted: </p> | |
Deleted: <p> | |
| 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. |
Deleted: The header kind of looks like this:
| Added: The header looks like this:
|
Deleted: </p> | |
Deleted: <p> | |
Deleted: SKU | category | description | image | small_image | thumbnail | price | color
| Added: SKU | category | description | image | small_image | thumbnail | price | color
|
Deleted: </p> | |
Deleted: <p> | |
Deleted: so knowing how the header looks like, then the first row would be like sku01 | category name whatever | description here | | | | 99$ | black | |
Deleted: </p> | |
Deleted: <p> | |
Deleted: The filename for the jpeg is the same as the "SKU" value on the same row, with the jpeg extension. So for example, if there is "sku01" for the SKU column on the first row, the filename would be sku01.jpeg in the image column on that same row. So would be the small_image value and the thumbnail value. | |
Deleted: </p> | |
Deleted: <p> | |
Deleted: <strong>However, the filename would only be specified IF the jpeg exists in a folder.</strong> | |
Deleted: </p> | |
Deleted: <p> | |
| Added: I need to read the data from other files and put the suitable data in the correct column. |
| 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: |
| Added: At first, I break out the columns into an array precisely as they appear in the raw CSV file: |
| Added: <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> |
Deleted: So far I know how to use the fopen function to open the CSV file, eventually store all data in an array ( I don't know if that would help in my case), then at some point use the fputcsv function after checking if file_exist in the specific folder on the server. | Added: 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. |
Deleted: </p> | |
Deleted: <p> | |
Deleted: But, to me, it's still a big mess in my head as far as in what order I should use the functions. I 'm stuck. Also, I have no idea how to put the jpeg filename in the right columns ( image, thumbnail, small_image), because these columns are in the "middle" of the CSV file, among other columns. They are not at the end of the CSV file | |
Deleted: </p> | |
| Added: <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"> |
| Added: $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"> |
| Added: $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> |
| Added: <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"> |
| Added: $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"> |
| Added: $filename </span><span class="pun">=</span> <span class="str">" {$row['sku']}.jpeg" </span><span class="pun">;</span> |
| Added: <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"> |
| Added: $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"> |
| Added: $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"> |
| Added: $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> |
| Added: <span class="pun">}</span><span class="pln"> |
| 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> |
| Added: <span class="pun">}</span><span class="pln"> |
| Added: fclose</span><span class="pun">( </span><span class="pln">$file</span><span class="pun">) ;</span><span class="pln"> |
| Added: fclose</span><span class="pun">( </span><span class="pln">$newFile</span><span class="pun">) ;</span></code></pre> |
| Added: Hope it is useful to you 🙂 |
No comments yet.