1

I have this piece of code in my page:

  $options = array("Repairing", "In guarantee", "Ended", "On Hold", "Waiting for quote");
?>
 <select name='estadoRep[]' id='estadoRep'>
     <?php foreach ($options as $option): ?>
         <option value="<?php echo $option; ?>"<?php if ($estadoR[$i] == $option): ?> selected="selected" <?php endif; ?>>
             <?php echo $option; ?>
         </option>
     <?php endforeach; ?>

This code will send the status of equipment through a form to another page.

When I "receive" those value through the POST method on the other page, I have the need to fill up a variable if all status in the array = "Ended". If all array[%i] = "Ended" the variable $status should be filled with: "Concluded" else if only one as different value the variable should be filled with "Pending";

I tried the following code but it is not working. Can you please give me some advice? Thanks

 $estadoEquip   =   $_POST['estadoRep'];

    $max = sizeof($estadoEquip);
    for ($i=0; $i<$max; $i++) {
      if ($estadoEquip[$i] = 'Ended')
      $status= 'Concluded';

      if ($estadoEquip[$i] != 'Ended')
      $status= 'Pending'; 

    }}

3 Answers 3

1

You have a synthax error in your php code : if ($estadoEquip[$i] == 'Ended') $status= 'Concluded';

Your forgot the double "==", so your $estadoEquip are all equal to "Ended".

Sign up to request clarification or add additional context in comments.

3 Comments

I tried but it didn't work Equipment XPTO1 - Ended Equipment XPTO2 - Ended Equipment XPTO3 - Pending Works fine and the variable $status='Pending' because the Ended condition was not met for all Another example: Equipment XPTO1 - Pending Equipment XPTO2 - Repairing Equipment XPTO3 - Ended Doesn't work fine: The variable $status='Concluded' even though the other two does not met the condition to set the variable that way. It should be also $status='Pending' My need is that $status='Ended' only if Equipment XPTO1 - Ended Equipment XPTO2 - Ended Equipment XPTO3 - Ended
try : $max = sizeof($estadoEquip); for ($i=0; $i<$max; $i++) { if ($estadoEquip[$i] == 'Ended'){ $status= 'Concluded'; } else if ($estadoEquip[$i] != 'Ended'){ $status= 'Pending'; } }
Remains the same :(
0

You should put "==" instead of "=" at the "if ($estadoEquip[$i] = 'Ended')" Otherwise that condition will always be true due to the fact that the variable will always be able to get that value. A simple "=" is not a comparison.

1 Comment

I tried but it didn't work Equipment XPTO1 - Ended Equipment XPTO2 - Ended Equipment XPTO3 - Pending Works fine and the variable $status='Pending' because the Ended condition was not met for all Another example: Equipment XPTO1 - Pending Equipment XPTO2 - Repairing Equipment XPTO3 - Ended Doesn't work fine: The variable $status='Concluded' even though the other two does not met the condition to set the variable that way. It should be also $status='Pending' My need is that $status='Ended' only if Equipment XPTO1 - Ended Equipment XPTO2 - Ended Equipment XPTO3 - Ended
0

Some fundamental errors here that I can see:

1 estadoRep (name) does not need to be an array, unless you are doing something tricky to make it a multiple select.

 <select name='estadoRep' id='estadoRep'>
     <?php foreach ($options as $option): ?>
         <option value="<?php echo $option; ?>"<?php if ($estadoR[$i] == $option): ?> selected="selected" <?php endif; ?>>
             <?php echo $option; ?>
         </option>
     <?php endforeach; ?>

Your form handler then only needs to use that single incoming variable.

So its much simpler and more comprehensible, if not 'Ended' else status is Pending,

  $estadoEquip   =   $_POST['estadoRep'];

  if ($estadoEquip == 'Ended'){
    $status= 'Concluded';
  } else { 
    $status= 'Pending'; 
  }

Maybe contentious, but you could simply write that as :

$status = ($_POST['estadoRep'] == 'Ended') ? 'Concluded' : 'Pending';

Example of Ternary Operator

(untested)

2 Comments

I used an array because I have the need to send multiple states per multiple equipments from the "front page". I do not have only one equipment and, consequently one state. I can have much more. Thank you.
In that case all your name= values will have to be different. HTML forms only send one option per select. Try making a few selects and use var_dump($_POST) in the handler and you should see.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.