SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast Shane Is My Name's Avatar
    Join Date
    Oct 2009
    Location
    New York
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code:
    	 // 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)?

    Code:
    	// 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
    Shane

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code PHP:
    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
    }

  3. #3
    SitePoint Enthusiast Shane Is My Name's Avatar
    Join Date
    Oct 2009
    Location
    New York
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    Code:
    <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.
    Shane

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh I'm sorry I was going too fast

    Here we go:

    Code PHP:
    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
    }

  5. #5
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    United States
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...

    PHP Code:
    echo implode(', '$coolingtype); 
    A foreach is overkill in this scenario.

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

  6. #6
    SitePoint Enthusiast Shane Is My Name's Avatar
    Join Date
    Oct 2009
    Location
    New York
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to both of you.

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

    Shane


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •