Jump to content

Multiple foreach loops causing error


Go to solution Solved by cyberRobot,

Recommended Posts

Here is my code: 

$marketingvalueOutput = ""; 
        foreach($marketingimportance2 as $marketingimportance){ 
            $marketingvalueOutput .= "<input type=\"radio\" class=\"radio\" name=\"marketingimportance[]\" value=\"". $marketingimportance ."\">". $marketingimportance ."  \r";   
        } 
$marketingvalueOutput .= ""; 
		
$custexpOutput = ""; 
        foreach($custexp2 as $custexp){ 
            $custexpOutput .= "<input type=\"radio\" class=\"radio\" name=\"custexp[]\" value=\"". $custexp ."\">". $custexp ."  \r";   
        } 
$custexpOutput .= ""; 

$marfuOutput = "";
        foreach($marfu2 as $marfu){ 
            $marfuOutput .= "<input type=\"radio\" class=\"radio\" name=\"marfu[]\" value=\"". $marfu ."\">". $marfu ."  \r";   
        } 
$marfuOutput .= "<br />"; 

$custexpoptionsOutput = "";
        foreach($custexpoptions2 as $custexpoptions){ 
            $custexpoptionsOutput .= "<input type=\"checkbox\" class=\"checkbox\" name=\"custexpoptions[]\" value=\"". $custexpoptions ."\">". $custexpoptions ."  \r";   
        } 
$custexpoptionsOutput .= "<br />"; 


$outsourceoptionsOutput = "";
        foreach($outsource2 as $outsource){ 
            $outsourceoptionsOutput .= "<input type=\"checkbox\" class=\"checkbox\" name=\"outsource[]\" value=\"". $outsource ."\">". $outsource ."  \r";   
        } 
$outsourceoptionsOutput .= "<br />"; 

Here are the two errors I'm getting:

 

Warning: Invalid argument supplied for foreach() in C:\wamp\www\survey\page_3.php on line 135
Notice: Undefined variable: custexp2 in C:\wamp\www\survey\page_3.php on line 135
 
Line 135 is the second foreach loop which says foreach($custexp2 as $custexp).
 
Thanks
Link to comment
https://forums.phpfreaks.com/topic/281891-multiple-foreach-loops-causing-error/
Share on other sites

Usually that means it's not an array. Try print_r($custexp2);   to make sure it contains what you expect it to.

 

Here is my code: 

$marketingvalueOutput = ""; 
        foreach($marketingimportance2 as $marketingimportance){ 
            $marketingvalueOutput .= "<input type=\"radio\" class=\"radio\" name=\"marketingimportance[]\" value=\"". $marketingimportance ."\">". $marketingimportance ."  \r";   
        } 
$marketingvalueOutput .= ""; 
		
$custexpOutput = ""; 
        foreach($custexp2 as $custexp){ 
            $custexpOutput .= "<input type=\"radio\" class=\"radio\" name=\"custexp[]\" value=\"". $custexp ."\">". $custexp ."  \r";   
        } 
$custexpOutput .= ""; 

$marfuOutput = "";
        foreach($marfu2 as $marfu){ 
            $marfuOutput .= "<input type=\"radio\" class=\"radio\" name=\"marfu[]\" value=\"". $marfu ."\">". $marfu ."  \r";   
        } 
$marfuOutput .= "<br />"; 

$custexpoptionsOutput = "";
        foreach($custexpoptions2 as $custexpoptions){ 
            $custexpoptionsOutput .= "<input type=\"checkbox\" class=\"checkbox\" name=\"custexpoptions[]\" value=\"". $custexpoptions ."\">". $custexpoptions ."  \r";   
        } 
$custexpoptionsOutput .= "<br />"; 


$outsourceoptionsOutput = "";
        foreach($outsource2 as $outsource){ 
            $outsourceoptionsOutput .= "<input type=\"checkbox\" class=\"checkbox\" name=\"outsource[]\" value=\"". $outsource ."\">". $outsource ."  \r";   
        } 
$outsourceoptionsOutput .= "<br />"; 

Here are the two errors I'm getting:

 

Warning: Invalid argument supplied for foreach() in C:\wamp\www\survey\page_3.php on line 135
Notice: Undefined variable: custexp2 in C:\wamp\www\survey\page_3.php on line 135
 
Line 135 is the second foreach loop which says foreach($custexp2 as $custexp).
 
Thanks

 

Thank you -- just tried that and it seems that I made a minor error with variable names.

 

Another quick question... I want to add checked="checked" to the radio buttons and checkboxes that have been selected. How is this possible?

 

Thanks

Here is the rest of the code: 

$outsource2 = array("Marketing Agency","PR Agency","Design Studio","Marketing Consultant", "Media Agency", "Advertising Agency", "Service or Product Design Agency", "Customer Experience Agency"); 

  if($_SERVER['REQUEST_METHOD'] == 'POST') {


	    if(isset($_POST['outsource'])){     
        foreach($outsource2 as $outsource){ 
                  $value = (in_array($outsource,$_POST['outsource']) ? "True" : "False"); 
               $_SESSION[$outsource] = $value; 
                $result[$outsource] = $value; 
        }
}		
		else {
      unset($_SESSION['outsource']);	
		}
   
}

Like I said, I just need to know how to add checked="checked" for the options that have been selected.

 

Thanks

  • Solution

First, radio buttons should have the same name. There's no need for the array format. Also, you wouldn't need to escape all those double quotes, if the input tags are enclosed in single quotes. Double quoted strings are only needed if the string contains things like variables or other special characters (\r).

<?php
//...

foreach($marketingimportance2 as $marketingimportance){
     $marketingvalueOutput .= '<input type="radio" class="radio" name="marketingimportance" value="' . $marketingimportance . ' \>' . $marketingimportance . "  \r";   
}
 
//...
?>

With the radio buttons having the same name, you can use a single POST variable to populate the radio buttons. Here's a quick example (note that you'll need to adapt it for your own code):

<form method="post" action="">
<?php
$marketingimportance2 = array('this', 'that');
foreach($marketingimportance2 as $marketingimportance){ 
     print '<input type="radio" class="radio" name="marketingimportance" value="' . $marketingimportance . '"';
     if(isset($_POST['marketingimportance']) && $_POST['marketingimportance']==$marketingimportance) { print ' checked="checked"'; }
     print ' />' . $marketingimportance . "  \r";   
}
?>
<input type="submit" name="submit" />
</form>

Hi there cyberRobot

 

Your code is great for radio buttons, but if you look at my original post you'll see two of the form fields use checkboxes, allowing the user to select multiple options. I've just tried your code for checkboxes and it pops up with error: "in_array() expects parameter 2 to be array, string given". 

My original code works much better for this as it saves the following to session data. I just haven't managed to work out how to add checked="checked" for the options that have been selected.
 

    [Marketing Agency] => False
    [PR Agency] => False
    [Design Studio] => False
    [Marketing Consultant] => True
    [Media Agency] => False
    [Advertising Agency] => False
    [Service or Product Design Agency] => False
    [Customer Experience Agency] => False

Thanks

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.