mallen Posted September 12, 2012 Share Posted September 12, 2012 This part of my code is causing a Invalid argument supplied for foreach() warning. I understand that the array is empty but not sure how to format this. This is used for importing a excell database. If there is no files for that particular item it will enter empty data to that table. Which is what I don't want. But making these changes causes the error. if($data[11] == "" || empty($data[11])) $documents = ""; else $documents = explode(",", $data[11]); foreach($documents as $addFile) { if(strpos($addFile, ".doc") !== false) $addFileType = "DOC"; else $addFileType = "PDF"; Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 $documents = ""; Is not an array, you can't use foreach on it. Quote Link to comment Share on other sites More sharing options...
mallen Posted September 12, 2012 Author Share Posted September 12, 2012 Solved it. if ( ! empty($documents) ) // added foreach($documents as $addFile) NowI just need to find a way to trim file names. Just ltrim() the document name. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 And the problem is??? Quote Link to comment Share on other sites More sharing options...
mallen Posted September 12, 2012 Author Share Posted September 12, 2012 And the problem is??? Sometimes there may be spaces before file names due to user error. If someone edits the exell file. Then when its imported I want to strip the blank space before entering it in the database. $documents = explode(",", $data[11]); Quote Link to comment Share on other sites More sharing options...
premiso Posted September 12, 2012 Share Posted September 12, 2012 ... you said it yourself, using trim. Not sure what you are trying to get at, without you pasting more code so we can see where the filename is being used / where the trim would need to apply. Alternatively, you could just apply it to the whole documents array using array_filter Quote Link to comment Share on other sites More sharing options...
mallen Posted September 12, 2012 Author Share Posted September 12, 2012 I have tried both of these. $documents = ltrim(explode(",", $data[11])); $documents = explode(",", ltrim($data[11])); Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 You have to do it on each item, or use array_filter as premiso said. Quote Link to comment Share on other sites More sharing options...
premiso Posted September 12, 2012 Share Posted September 12, 2012 Is $data[11] the item that needs trimmed? It seems like you are just exploding it. $documents = explode(",", $data[11]); array_walk($documents, 'ltrim'); Should do what you want, not really knowing what you want and thinking that you do not know what you want. Quote Link to comment Share on other sites More sharing options...
mallen Posted September 12, 2012 Author Share Posted September 12, 2012 Thanks Premiso for trying to help me out with this. Yes I now what I want to do just not how to. I need to trim any blank or empty space that may appear before a document's name. I tried that and got this error: Warning: array_walk() expects parameter 1 to be array, string given in Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 You need to POST your code when you make changes, and post WHAT you tried. Quote Link to comment Share on other sites More sharing options...
mallen Posted September 12, 2012 Author Share Posted September 12, 2012 You need to POST your code when you make changes, and post WHAT you tried. * Please only comment if you are willing and able to help. I posted the code I tried from premiso. I will post it again. $documents = explode(",", $data[11]); array_walk($documents, 'ltrim'); Quote Link to comment Share on other sites More sharing options...
premiso Posted September 12, 2012 Share Posted September 12, 2012 if ( ! empty($documents) && is_array($documents)) // added array_walk($documents, 'ltrim'); foreach($documents as $addFile) Try it like that. Or like this: if ( ! empty($documents) && is_array($documents)) // added array_walk($documents, 'ltrim'); foreach($documents as $addFile) { $addFile = ltrim($addFile); Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 I posted the code I tried from premiso. I will post it again. No, YOU didn't. She posted that code. YOU posted an error. There are plenty of people who post on here who would read her post, change the code, and say it didn't work. There's no logical reason for that code to cause that error, explode CANNOT return a string. It can return a BOOLEAN or an array. Therefore, the REST of your code matters too. * Please only comment if you are willing and able to help. I'm certainly able, but if you're not able to learn how to GET help, you won't get it. Don't worry, I won't bother trying to help you anymore. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 12, 2012 Share Posted September 12, 2012 array_walk cannot be used with call-back functions that return values. It needs to be used with call-back functions that modify a value that is passed by reference. array_map works with a call-back function that return a value. Quote Link to comment Share on other sites More sharing options...
mallen Posted September 12, 2012 Author Share Posted September 12, 2012 Premiso, Sorry I should have been more clear. This part of my code works. There is a addFile and also a addFileTYPE, The addFile is the part that needs the spaces trimmed. if($data[11] == "" || empty($data[11])) $documents = ""; else $documents = explode(",", $data[11]); if ( ! empty($documents) ) foreach($documents as $addFile) { if(strpos($addFile, ".doc") !== false) $addFileType = "DOC"; else $addFileType = "PDF"; $insertAddFile = "INSERT INTO " FILES . "(`prod_id`, `file_name`, `file_type`) VALUES('$lastId', '$addFile', '$addFileType')"; $wpdb->query($insertAddFile); Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 SO TRIM THE VARIABLE. FFS. Quote Link to comment Share on other sites More sharing options...
premiso Posted September 12, 2012 Share Posted September 12, 2012 *premiso facepalms if ( ! empty($documents) && is_array($documents)) // added foreach($documents as $addFile) { $addFile = ltrim($addFile); Should do what you want. Quote Link to comment Share on other sites More sharing options...
mallen Posted September 12, 2012 Author Share Posted September 12, 2012 Thanks. I tried what you suggested and it changed all my file types to "PDF" and also didn't strip the spaces. if ( ! empty($documents) ) foreach($documents as $addFile) { if(strpos($addFile, ".doc") !== false) $addFileType = "DOC"; else $addFileType = "PDF"; if ( ! empty($documents) && is_array($documents)) // added foreach($documents as $addFile) { $addFile = ltrim($addFile); } $insertAddFile = "INSERT INTO ". FILES . "(`prod_id`, `file_name`, `file_type`) VALUES('$lastId', '$addFile', '$addFileType')"; $wpdb->query($insertAddFile); } Quote Link to comment Share on other sites More sharing options...
mallen Posted September 12, 2012 Author Share Posted September 12, 2012 Solved it. if ( ! empty($documents) ) foreach($documents as $addFile) { $addFile = ltrim($addFile);// <--- added here if(strpos($addFile, ".doc") !== false) $addFileType = "DOC"; else $addFileType = "PDF"; Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 THATS WHAT SHE WROTE MY HEAD IS EXPLODING. 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.