Jump to content

Invalid argument supplied for foreach() warning


mallen

Recommended Posts

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";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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);
		}

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.