Foreach / Invalid argument

hello,
I have a form that users can select multiple items…
after submit, it gets handled but if the user doesn’t select any items, I get an error (Invalid argument supplied for foreach) on the lines below.

	 // Cooling Type
	 	foreach($coolingtype as $ctvalue)
		{
		$coolingtypeA = $coolingtypeA . $ctvalue . ', ';
		}

its obviously because its blank, so, do i only need to wrap each one with ‘isset’ like below (or os there another way to do it as a whole/ group)?

	// Cooling Type
	 	if (isset($coolingtype)) {

		foreach($coolingtype as $ctvalue)
		{
		$coolingtypeA = $coolingtypeA . $ctvalue . ', ';
		};}

Im still new, I have a bunch of pages to correct, so just wanted to make sure Im going about it correctly…

thanks

You’re error is most likely because the variable $coolingtype is not an array.

To avoid this error you can first check to make sure it’s an array. Then do your foreach loop. Here is an example:


if (is_array($coolingtype)) { // Checking to make sure it's an array first, otherwise do nothing
  $coolingtypeA = ''; // We're going to use this later

  foreach($coolingtype as $ctvalue) {
    $coolingtypeA .= $coolingtypeA == '' ? $ctvalue :  ', '.$ctvalue; // Short if statement. If the $coolingtypeA is empty concatenate the value of $ctvalue with no comma, otherwise, add a comma to the start of the value.
  }
  echo $coolingtypeA; // Output whatever is in $coolingtypeA
}

Thanks for reply.
Would I skip the 1st coolingtypeA = ‘’; (that you wrote on line 2) since im using a form? wouldn’t that empty my array?

Im just asking because the form sends over an array from a multiselect field

<select name="coolingtype[]" multiple="multiple" size="5" >
            <option>None</option>
            <option>Wall AC Unit</option>
            <option>Window AC Unit</option>
            <option>Attic Fan</option>
            <option>Geothermal</option>
            <option>Central AC</option>
            <option>See Remarks</option>
        </select>

and the foreach works UNLESS none of the choices are selected.
I’ve been up for over 28 hours, so my brain is probably fried. it seems so simple yet i cant get it.

Oh I’m sorry I was going too fast :slight_smile:

Here we go:


if (is_array($coolingtype)) { // Checking to make sure it's an array first, otherwise do nothing
  $coolingTypes = ''; // We're going to use this later
 
  foreach($coolingtype as $ctvalue) {
    $coolingTypes .= $coolingTypes == '' ? $ctvalue :  ', '.$ctvalue; // Short if statement. If the $coolingtypeA is empty concatenate the value of $ctvalue with no comma, otherwise, add a comma to the start of the value.
  }
  echo $coolingTypes; // Output whatever is in $coolingTypes
}

You are right about wrapping an isset(), but if all you are doing is adding commas in between, you should really just use the implode function…


echo implode(', ', $coolingtype);

A foreach is overkill in this scenario.

http://us2.php.net/manual/en/function.implode.php

Thanks to both of you.

great. i like that implode shortcut. especially since i have to redo a few pages.

:slight_smile: