aaron1988 Posted March 21, 2011 Share Posted March 21, 2011 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 21, 2011 Share Posted March 21, 2011 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); } Quote Link to comment Share on other sites More sharing options...
aaron1988 Posted March 21, 2011 Author Share Posted March 21, 2011 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 Quote Link to comment Share on other sites More sharing options...
aaron1988 Posted March 21, 2011 Author Share Posted March 21, 2011 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 21, 2011 Share Posted March 21, 2011 Also just do you no you have syntax error... See my sig - I'm not always going to take the time to test the code I provide. In some instances it would require me to set up a database and other data that would be prohibitive. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.