Jump to content

How to insert into one row in csv file?


soyaslim
 Share

Recommended Posts

Hello guys, I have been working on this simple project, however, seems a little bit tricky. So, I have an array which I want to write the data into csv file. The array below:
 

Array
(
    [0] => 13763-1.jpg
    [1] => 13763.jpg
    [2] => 13763-2.jpg
    [3] => 13764-2.jpg
    [4] => 13764.jpg
    [5] => 13764-1.jpg
    [6] => 13765.jpg
)

I sorted the array using rsort($array) so it looks like below:

Array
(
    [0] => 13765.jpg
    [1] => 13764.jpg
    [2] => 13764-2.jpg
    [3] => 13764-1.jpg
    [4] => 13763.jpg
    [5] => 13763-2.jpg
    [6] => 13763-1.jpg
)

So, I want to output this data into csv file like below:

image.png.c3fb0823690a103668d47351570751fd.png

However, I could not able to achieve the additional images value in the CSV data. My code below:

$file = fopen($directory."M2_import_image.csv", 'w');

    // insert header to import file
    $header= array("product_id", "base_image", "small_image", "thumbnail_image", "swatch_image", "additional_images");
    fputcsv($file, $header, ',');

    $images = glob($directory.'*.{jpg,png,gif,jpeg,JPG}', GLOB_BRACE);
    rsort($images);
    // natcasesort($images);
   
    $csvData = [];
    $counter = 0;
    foreach ($images as $image) {
        $filename = pathinfo($image);
        $filename = $filename['filename'];
        $rolanId = substr($filename, 0, strpos($filename, '-'));
        
        $additionalImages[] = $rolanId.'.jpg';

        if (strstr($filename, $rolanId) != FALSE) {
            $counter++;
        } else if (strstr($filename, $rolanId) == FALSE)
            $counter = 0;

        if ($counter > 0) {
           $additionalImages[] = $filename.'.jpg';
        } 
        
    }
    print_r($additionalImages);

    fclose($file);

 

Link to comment
Share on other sites

I think I'm getting an aneurysm.

What you're describing does not seem to match what the screenshot of the file shows. I think what you actually want to do is (1) find all the files in the directory, (2) group them by the initial number in the name, then (3) sync up the CSV file according to the new set of files. But what to do with files in the CSV that aren't in the directory? Seems you want to discard them?

So one step at a time. You need to get an array - one that is not $additionalImages but something entirely different - which has a list of all the files grouped by that initial number. With your example, it will look like

array(
	"13763.jpg" => array(
		"13763-1.jpg",
		"13763-2.jpg"
	),
	"13764.jpg" => array(
		"13764-1.jpg",
		"13764-2.jpg"
	),
	"13765.jpg" => array()
)

That means there is a 13763.jpg with two additional files, 13764.jpg with two more additional files, and 13765.jpg without any additional files.

Once you have that then you can move onto the next step of writing it to the file...

Link to comment
Share on other sites

4 minutes ago, requinix said:

I think I'm getting an aneurysm.

What you're describing does not seem to match what the screenshot of the file shows. I think what you actually want to do is (1) find all the files in the directory, (2) group them by the initial number in the name, then (3) sync up the CSV file according to the new set of files. But what to do with files in the CSV that aren't in the directory? Seems you want to discard them?

So one step at a time. You need to get an array - one that is not $additionalImages but something entirely different - which has a list of all the files grouped by that initial number. With your example, it will look like

array(
	"13763.jpg" => array(
		"13763-1.jpg",
		"13763-2.jpg"
	),
	"13764.jpg" => array(
		"13764-1.jpg",
		"13764-2.jpg"
	),
	"13765.jpg" => array()
)

That means there is a 13763.jpg with two additional files, 13764.jpg with two more additional files, and 13765.jpg without any additional files.

Once you have that then you can move onto the next step of writing it to the file...

You are exactly right! but how do I achieve this array

Link to comment
Share on other sites

 I tried to check if the next array data has '-' or rolanId in the string but still could not achieve the array.
 

10 minutes ago, soyaslim said:

You are exactly right! but how do I achieve this array

foreach ($images as $image) {
        $filename = pathinfo($image);
        $filename = $filename['filename']; // 13763
        $rolanId = substr($filename, 0, strpos($filename, '-')); // 13763
        
        $additionalImages[] = $rolanId.'.jpg';
        $csvData[$rolanId] = array(
            $rolanId.'.jpg',
            (strpos($filename, "-") != FALSE)? $filename : '',
        );
        
    }
    print_r($csvData);

 

Link to comment
Share on other sites

37 minutes ago, requinix said:

I think I'm getting an aneurysm.

What you're describing does not seem to match what the screenshot of the file shows. I think what you actually want to do is (1) find all the files in the directory, (2) group them by the initial number in the name, then (3) sync up the CSV file according to the new set of files. But what to do with files in the CSV that aren't in the directory? Seems you want to discard them?

So one step at a time. You need to get an array - one that is not $additionalImages but something entirely different - which has a list of all the files grouped by that initial number. With your example, it will look like

array(
	"13763.jpg" => array(
		"13763-1.jpg",
		"13763-2.jpg"
	),
	"13764.jpg" => array(
		"13764-1.jpg",
		"13764-2.jpg"
	),
	"13765.jpg" => array()
)

That means there is a 13763.jpg with two additional files, 13764.jpg with two more additional files, and 13765.jpg without any additional files.

Once you have that then you can move onto the next step of writing it to the file...

Is there any way I can achieve this? I checked if the array data has "-" and matches the id but did not able to do it.

Link to comment
Share on other sites

Hmm, looks like you'll need a modification or two to the array I suggested. More like

array(
	13763 => array(
		"base_image" => "13763.jpg",
		"additional_images" => array(
			"13763-1.jpg",
			"13763-2.jpg"
		)
	),

0. Start with an empty array for you to store the lists of additional files you'll be building.
1. Use glob() to get the list of files. It already comes sorted in ascending order, you need it sorted in ascending order, and I'm not sure why you want to sort it in reverse order.
2. Use pathinfo() to get the different parts of the file name. Look at the different parts it returns because you'll want more than one of them.
3. If the filename-without-extension portion is just a number then add an entry to the array. You know the base_image value already. Start with the additional_images empty.
4a. If the filename-without-extension portion is not just a number then split it into (1) the initial number and (2) the hyphen and everything after it.
4b. Look for an entry in the array for the number. If it exists then add the file to the additional_images array. If not then I don't know what you want to do.

At the end of this, you should have an array that looks like the sample above...

Link to comment
Share on other sites

5 hours ago, requinix said:

Hmm, looks like you'll need a modification or two to the array I suggested. More like

array(
	13763 => array(
		"base_image" => "13763.jpg",
		"additional_images" => array(
			"13763-1.jpg",
			"13763-2.jpg"
		)
	),

0. Start with an empty array for you to store the lists of additional files you'll be building.
1. Use glob() to get the list of files. It already comes sorted in ascending order, you need it sorted in ascending order, and I'm not sure why you want to sort it in reverse order.
2. Use pathinfo() to get the different parts of the file name. Look at the different parts it returns because you'll want more than one of them.
3. If the filename-without-extension portion is just a number then add an entry to the array. You know the base_image value already. Start with the additional_images empty.
4a. If the filename-without-extension portion is not just a number then split it into (1) the initial number and (2) the hyphen and everything after it.
4b. Look for an entry in the array for the number. If it exists then add the file to the additional_images array. If not then I don't know what you want to do.

At the end of this, you should have an array that looks like the sample above...

Could u please shed a light on me? I really appreciate your help and the time u had already spent. So, I manage to change the images filenames array that I got from the directory, I first replace the extension '.jpg' and directory name '/images' and sorted the array as below:
 

array (
    '13763',
    '13763-1',
    '13763-2',
    '13764',
    '13765',
    '13765-1',
);

So, I am going through the foreach loop as $image which gives me each element and each loop I am checking the string has '-' character or not. If it has no '-' character than I assign $currentId = $image but if it has '-' then I assign $additionalImage .= ', '.$image.'.jpg'. But the problem is I only want to insert one row if it has additional images '13763.jpg, 13763-1.jpg, 13763-2.jpg'. Below is my code what I have done so far.
 


$currentId = '';
$additionalImages = '';
$addId = '';

foreach ($images as $image) {
   
    if (strpos($image, '-') == FALSE) {
        $currentId = $image;
        $additionalImages = $image.'.jpg';
    }

    if (strpos($image, '-') != FALSE) {
        $addId = substr($image, 0, strpos($image, '-'));
        $additionalImages .= ', '.$image.'.jpg';
    }

    
    // insert into csv file

}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.