skip to Main Content

I have a php form with one of the fields being a dropdown box that allows multi select, this field is set as a varchar and points to another table for its options, the options are 1 to 8.

I would like to run an if code after adding a record, i can do this if the value is a single value, ie if($value==1), but as this dropdown allows multi select, it is possible for the field to save as 1,2,4,6 or 2,3,4, i.e. any combination of 1 to 8, and separated by a comma.

So what I need to do is get something like if($value….contains 1) {then do something }

Any ideas please

2

Answers


  1. You can use explode method

    Then you can use foreach loop

    Example code will look like

    $arrayWithValues = explode($value); 
    
    foreach($arrayWithValues as $selectedOption ) {
      if($selectedOption ==1) {
         // Do smth
        }
    }
    
    Login or Signup to reply.
  2. the problem with string_contains is that if value contains a number 15, it would true the condition as it does contain 1 in 15…

    <?php
        $value = explode(",", $value);
        //seperate the value string by a coma and create array
        
        if(gettype(array_search(1, $value)) == 'integer'){
        
        //search the array for '1', if it has it/ returns a number
        //do something
        
        
        }
         ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search