invalid argument foreach()

I want to display a message for each of the results.

I am having a problem with this line here:
[php]foreach($category_delete as $category_delete_result){//List each category that has been deleted[/php]

Here is the rest of my code:
[php]
if ($_POST[‘bulk_action’] == ‘delete’ && !empty($_POST[‘checkbox’])) {//Check if delete option is selected and check if boxes are ticked
foreach($_POST[‘checkbox’] as $check_id) {//Delete each box that has been ticked
$get_category_details = $wpdb->get_results('SELECT name FROM ’ . $wpdb->prefix . ‘terms WHERE term_id=’ . $check_id);//Get name of deleted category
$category_delete = wp_delete_category($check_id);

		foreach($category_delete as $category_delete_result){//List each category that has been deleted  	
			if($category_delete_result){// if successful 
				foreach($get_category_details as $category_details) {
					$actionMessage = "<strong>Success!</strong> Deleted the category: <strong>".$category_details->name."</strong> from the database!"; 
				}
			}//END if successful 
			else {// failure to delete category 
				foreach($get_category_details as $category_details) {
					$actionMessage = "<strong>Warning!</strong> Failed to delete the category: <strong>".$category_details->name."</strong> from the database!"; 
				}
			}//END failure to delete category
	}//END List each category that has been deleted 
}//END Delete each box that has been ticked

}//END Check if delete option is selected and check if boxes are ticked
[/php]

Do you mean you get an error message?
Did you check if $category_delete is an array, and is not empty?

I created three categories.

Now I want to delete those three categories.

I select the three categories and it deletes them perfectly, but it doesn’t echo a success OR failure message.

Instead it just returns:

Warning: Invalid argument supplied for foreach() x3

On this line:
[php]foreach($category_delete as $category_delete_result){//List each category that has been deleted[/php]

You need to check if this array is properly populated from database. For debugging just add this before your foreach loop:
[php]var_dump($category_delete);[/php]

Okay. I’m not exactly sure what the return message means, but this is what it gave me.

bool(true)

This means that function wp_delete_category() returned a scalar boolean value, not an array of deleted categories as you are expecting.

How else could I go about achieving my goal?

I mean, to me the way it’s written makes sense but obviously it’s incorrect.

For each checkbox that’s ticked, get the category name of the checkbox id.
Then, delete each category.
For each category deleted, if it’s successful for each deleted result, echo a message.
For each category that fails to delete, echo a failure message.

I have tried just about everything I can think of and still no luck.

See, you already have a loop within which you are deleting categories. So, there is no need in the second foreach loop within the first loop. Do something like this:
[php]if ($_POST[‘bulk_action’] == ‘delete’ && !empty($_POST[‘checkbox’])) {//Check if delete option is selected and check if boxes are ticked
$actionMessage = ‘’;
foreach($_POST[‘checkbox’] as $check_id) {//Delete each box that has been ticked
$get_category_details = $wpdb->get_results('SELECT name FROM ’ . $wpdb->prefix . ‘terms WHERE term_id=’ . $check_id);//Get name of deleted category
$category_delete = wp_delete_category($check_id);

if($category_delete){  // sucessfully deleted
  $actionMessage. = "<strong>Success!</strong> Deleted the category: <strong>".$get_category_details->name."</strong> from the database!<br>"; 
}
else{
  $actionMessage. = "<strong>Warning!</strong> Failed to delete the category: <strong>".$get_category_details->name."</strong> from the database!<br>";
}

}
}[/php]

Notice the dot in this expression: $actionMessage. = (we are appending message on each iteration)

Thank you for your quick replies.

I modified a few things…

[php]if ($_POST[‘bulk_action’] == ‘delete’ && !empty($_POST[‘checkbox’])) {//Check if delete option is selected and check if boxes are ticked
$actionMessage = ‘’;
foreach($_POST[‘checkbox’] as $check_id) {//Delete each box that has been ticked
$get_category_details = $wpdb->get_results('SELECT name FROM ’ . $wpdb->prefix . ‘terms WHERE term_id=’ . $check_id);//Get name of deleted category
$category_delete = wp_delete_category($check_id);

foreach($get_category_details as $category_details) {
  if($category_delete){  // sucessfully deleted
    $actionMessage.="<strong>Success!</strong> Deleted the category: <strong>".$category_details->name."</strong> from the database!<br>"; 
  }
  else{
    $actionMessage.="<strong>Warning!</strong> Failed to delete the category: <strong>".$category_details->name."</strong> from the database!<br>";
  }
}

}
}
[/php]

$get_category_details->name was not displaying the category names. So I added another foreach around the success/fail check and renamed $get_category_details->name to $category_details->name.

Removed the spaces in the $actionMessage.

$actionMessage. = "";

to

$actionMessage.="";

I’m glad to hear you get it working. Just a small notice - spaces does not affect anything here:
[php]$actionMessage. = “”;[/php]

You can have many spaces, new lines and tabs or none - code will be correct.

With the spaces, I was getting blank lines for the $actionMessage. When I removed the spaces, the messages were displaying correctly. :o

Can I suggest a feature for threads on this site?

Marking topics as “Resolved” or “Not Resolved”?

By the way I believe it’s supposed to be $var .= ‘’; and not $var. = ‘’;

Yes, you’re right .= is correct operator, spaces are ok before or after this combination, NOT in between . and =

Sponsor our Newsletter | Privacy Policy | Terms of Service