Jump to content

Skip Bank Cells CSV Import


aaron1988

Recommended Posts

Hi wondered if someone could help me i have this code:

 

if (isset($data['product_image'])) {

foreach ($data['product_image'] as $image) {

 

$image = $image.".jpg";

$image = str_replace (" ", "", $image);

 

$filename = '/home/purchase/public_html/fancydresscostumesonline.co.uk/image/'. $image;

 

if (file_exists($filename)) {

$this->db->query("INSERT INTO " . DB_PREFIX . "product_image SET product_id = '" . (int)$product_id . "', image = '" . $this->db->escape($image) . "'");

} else {

$this->db->query("INSERT INTO " . DB_PREFIX . "product_image SET product_id = '" . (int)$product_id . "', image = 'no_image.jpg'");

}

 

}

}

 

This part of the code adds a jpg at the end of the product_image additional images so in my csv the 10 columns which have additional numbers for example in my csv it has: say 00532 so this code adds 00532.jpg once imported the the next part which is the else statement no_image.jpg adds a no_image.jpg but instead of it inserting a no_image.jpg for all the blank cells i would like it to SKIP the blank cells for the additional images which is product_image so if no number in the cell it SKIPS it. but have no clue how to do it.

 

Hope someone can help

 

Regards,

Aaron

Link to comment
Share on other sites

I cannot make sense out of your request above. This is not a "chat" room, please use punctuation.

 

From what I *think* I understand you simply don't want the ELSE condition to run if there is no image. Then just remove the else condition.

 

Also, your script is very inefficient due to the queries being run in a loop. You should create one query to insert all the records and run it at the end. Example

if (isset($data['product_image']))
{
    $insertValues = array();
    foreach ($data['product_image'] as $image)
    {
        $image = str_replace (' ', '', "{$image}.jpg");
        $filename = '/home/purchase/public_html/fancydresscostumesonline.co.uk/image/'. $image;
        if (file_exists($filename))
        {
            $id  = (int) $product_id;
            $img = $this->db->escape($image);
            $insertValues[] = "('$id', '$img')";
        }
    }
    $query = "INSERT INTO " . DB_PREFIX . "product_image (product_id, image)
              VALUES " . implode(', ', $insertValues);
    $this->db->query($query); 
}

Link to comment
Share on other sites

Hi cheers for that code but  your are correct in a sense i dont want the else statement but what i need to happen is once it does the import if some of the product_image fields are empty it skips those and just imports the ones which has DATA in.

 

Regards,

Aaron

Link to comment
Share on other sites

Also just do you no you have  syntax error, as i tried import to see what happened if your code worked for actual import and i get:

 

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2
Error No: 1064
INSERT INTO product_image (product_id, image) VALUES 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.