skip to Main Content

I have 3 dropdowns selects (each drop-down has values from 0 to 25):

<select class="form-select" id="select1" name="select1">
<option selected value="0">label...</option>
<option value="5">label...</option>
<option value="10">label...</option>
<option value="15">label...</option>
<option value="20">label...</option>
<option value="25">label...</option>
</select>
<select class="form-select" id="select2" name="select2">
<option selected value="0">label...</option>
<option value="5">label...</option>
<option value="10">label...</option>
<option value="15">label...</option>
<option value="20">label...</option>
<option value="25">label...</option>
</select>
<select class="form-select" id="select3" name="select3">
<option selected value="0">label...</option>
<option value="5">label...</option>
<option value="10">label...</option>
<option value="15">label...</option>
<option value="20">label...</option>
<option value="25">label...</option>
</select>

I’m trying to calculate the total score for all 3 dropdowns using _GET:

//get select values
$select1= $_GET['select1'];
$select2= $_GET['select2'];
$select3= $_GET['select3'];

$totalValues = array($select1, $select2, $select2);
$totalScore = array_sum($totalValues);

But also use conditions as follows:

//if any values are between 5-9
if ($totalScore <= 15)
     { 
         //this is the condition I'm stuck on - checking for min/max values in the array
      if ($totalValues >= 5 && $totalValues <=9)
         {
         $rating = 5;
         }  
        
  }

//and the same logic below...
  • if any values are between 10-14
  • if any values are between 15-19
  • if any values are between 20-25

What is the best approach to use?

I pased all 3 values into an array then am trying to use in_array() to check the above conditions.

Hope that makes sense.

2

Answers


  1. Chosen as BEST ANSWER

    Think I've solved it with this logic:

    if ($totalScore <= 15)
    {
        if ($select1 <= 5 && $select2 <= 5 && $select3 <= 5)
        {
            $rating = 5;
        }
        if ($select1 == 10 || $select2 == 10 || $select3 == 10)
        {
            $rating = 4;
        } 
    }
    

  2. You can set up your logic like below to support several cases:

    • If any value is between 5-9

    • if any values are between 10-14

    • if any values are between 15-19

    • if any values are between 20-25

      if ($totalScore < 15) {
       // If any value is between 5-9
      if ($totalValues >= 5 && $totalValues <=9) {
         $rating = 5;
       }  
      else if($totalValues >= 10 && $totalValues <=14) {
       // if any values are between 10-14
      }
      } elseif ($totalScore < 20) {
        // if any values are between 15-19
      } elseif ($totalScore <= 25) {
       // if any values are between 20-25
      }
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search