Im having problems with a foreach function. The following code results in the error message "Invalid argument supplied for foreach().." is this something to do with my use of $_REQUEST?

<?php
	  if (isset($_POST['submit'])) { 
$answer = array(); 
$answer[1] = $_REQUEST['improvement']; 
$answer[2] = $_REQUEST['planning']; 
$answer[3] = $_REQUEST['confidence']; 
$answer[4] = $_REQUEST['motivation']; 
$answer[5] = $_REQUEST['resources']; 
$answer[6] = $_REQUEST['integration']; 
$answer[7] = $_REQUEST['diagnosis']; 
$answer[8] = $_REQUEST['change']; 
$answer[9] = $_REQUEST['expectations']; 
$answer[10] = $_REQUEST['validated']; 
$answer[11] = $_REQUEST['time']; 
$answer[12] = $_REQUEST['demand']; 
$answer[13] = $_REQUEST['external_support']; 
$answer[14] = $_REQUEST['training']; 
$answer[15] = $_REQUEST['depth']; 
$answer[16] = $_REQUEST['part']; 
$answer[17] = $_REQUEST['cost']; 

function pqasso($answer) 
{ 
    foreach($answer as $key=>$value) 
    { 
        if($value >= "3") 
        { 
            $score = $score + 1000; 
        } 
        else 
        { 
            $score = $score - 1000; 
        } 
    } 
    return $score; 
}

function iso($answer) 
{ 
    foreach($answer as $key=>$value) 
    { 
        if($value >= "3") 
        { 
            $score = $score + 1000; 
        } 
        else 
        { 
            $score = $score - 1000; 
        } 
    } 
    return $score; 
}
function iip($answer) 
{ 
    foreach($answer as $key=>$value) 
    { 
        if($value >= "3") 
        { 
            $score = $score + 1000; 
        } 
        else 
        { 
            $score = $score - 1000; 
        } 
    } 
    return $score; 
}
///// feedback for each score
echo "Look at the scores for each quality system below.  The higher the score is the more relevant and suited that quality system is to your organisation";

//call the functions and echo them: 
if (pqasso($score)>=1000) { print "From your response It looks like PQASSO is an option for you<br>"; } 
else { print "PQASSO may not be an appropriate system for you"; } 
if (iso($score)>=1000) { print "From your response It looks like ISO is an option for you<br>"; } 
else { print "IIP may not be an appropriate system for you"; } 
if (iip($score)>=1000) { print "From your response It looks like IIP is an option for you<br>"; } 
else { print "IIP may not be an appropriate system for you"; } 
}
?> 

Im also trying to echo the score for each function too. Instead of:

if (iso($score)>=1000) { print "From your response It looks like ISO is an option for you<br>"; } 
else { print "IIP may not be an appropriate system for you"; } 

I would like to just echo the score for iip($score). Im not sure what syntax to use after looking at the manual

thx in advance guys

    Before calling each foreach u should try to check if the array u send to foreach is not empty ...

      When calling the functions you should call them with the array and not the result they will return.

        thankyou Bogu- I have had a look at the manual about the empty() function but how to deploy it has confused me more 😕 I can see the benefit of testing to see if an array is empty (i think) but what to do if it is empty is another thing.

        Piranha- I assume what you mean then is that this:

        if (pqasso($score)>=1000) { print "From your response It looks like PQASSO is an option for you<br>"; } 
        else { print "PQASSO may not be an appropriate system for you"; } 
        
        
        if (pqasso($answer)>=1000) { print "From your response It looks like PQASSO is an option for you<br>"; } 
        else { print "PQASSO may not be an appropriate system for you"; } 
        

        if this is the case how do I echo the score for each function?

          Tezread wrote:

          Piranha- I assume what you mean then is that this:

          if (pqasso($score)>=1000) { print "From your response It looks like PQASSO is an option for you<br>"; } 
          else { print "PQASSO may not be an appropriate system for you"; } 
          
          
          if (pqasso($answer)>=1000) { print "From your response It looks like PQASSO is an option for you<br>"; } 
          else { print "PQASSO may not be an appropriate system for you"; } 
          

          if this is the case how do I echo the score for each function?

          Yep, that is what I want. There is several ways to echo the result, a few examples below:

          //Alternative 1:
          $result = qpasso($answer); // Store the result of the function in a variable
          echo $result; // Echo the result
          
          //Alternative 2:
          echo qpasso($answer); // The same as above, but without storing the variable
          
          //Alternative 3:
          // If you want to use it in a if-statement
          if (pqasso($answer)>=1000) // The qpasso($answer) returns the result, 
          // meaning that it might return for example 2000. 
          // Then the statement becomes 8f (2000>=1000) {
          {
              // some code
          }

          I think that before you really understands functions you should always store what is returned from a function and then use it. That way you will understand functions and return values easier.

          I hope that you get it. If not then you need to read up on functions to really get to understand them. Unfortunatley I don't have a good site for that, but it should not be hard to find.

            Write a Reply...