Search code examples
phpcomparisonselectedvalue

if > 3 selected choices are the same php


I have tried to figure this out but cannot get to the bottom of it so am kindly asking for your assistance please. The best way I can explain is with an example.

Let's say you have 6 select boxes, where the user chooses items from them out of a list, once submitted the values will be sent via "GET" and display results on a screen.

How would I go about checking if 3 or more of the 6 selected choices are the same please? I know how to obtain the selected values from GET, but not how to check if 3 or more are the same choice.

Any help will be greatly appreciated.

Regards

<select class="form-control">
            <option value="" disabled="" selected="">Select Choice</option>
            <option value="choice1" >Choice 1</option>
            <option value="choice1">Choice 2</option>
            <option value="choice1">Choice 3</option>
        </select>
<select class="form-control">
            <option value="" disabled="" selected="">Select Choice</option>
            <option value="choice1" >Choice 1</option>
            <option value="choice1">Choice 2</option>
            <option value="choice1">Choice 3</option>
        </select>
<select class="form-control">
            <option value="" disabled="" selected="">Select Choice</option>
            <option value="choice1" >Choice 1</option>
            <option value="choice1">Choice 2</option>
            <option value="choice1">Choice 3</option>
        </select>
<select class="form-control">
            <option value="" disabled="" selected="">Select Choice</option>
            <option value="choice1" >Choice 1</option>
            <option value="choice1">Choice 2</option>
            <option value="choice1">Choice 3</option>
        </select>
<select class="form-control">
            <option value="" disabled="" selected="">Select Choice</option>
            <option value="choice1" >Choice 1</option>
            <option value="choice1">Choice 2</option>
            <option value="choice1">Choice 3</option>
        </select>
<select class="form-control">
            <option value="" disabled="" selected="">Select Choice</option>
            <option value="choice1" >Choice 1</option>
            <option value="choice1">Choice 2</option>
            <option value="choice1">Choice 3</option>
        </select>

Solution

  • Your answer is what @u_mulder sugggested in the first place: array_count_values on your request subset.

    However you first must give names to your selects e.g.

    <select name='select[]' class="form-control">
            <option value="" disabled="" selected="">Select Choice</option>
            <option value="choice1" >Choice 1</option>
            <option value="choice1">Choice 2</option>
            <option value="choice1">Choice 3</option>
        </select>
    <select name='select[]' class="form-control">
            <option value="" disabled="" selected="">Select Choice</option>
            <option value="choice1" >Choice 1</option>
            <option value="choice1">Choice 2</option>
            <option value="choice1">Choice 3</option>
        </select>
    <select name='select[]' class="form-control">
            <option value="" disabled="" selected="">Select Choice</option>
            <option value="choice1" >Choice 1</option>
            <option value="choice1">Choice 2</option>
            <option value="choice1">Choice 3</option>
        </select>
    <select name='select[]' class="form-control">
            <option value="" disabled="" selected="">Select Choice</option>
            <option value="choice1" >Choice 1</option>
            <option value="choice1">Choice 2</option>
            <option value="choice1">Choice 3</option>
        </select>
    <select name='select[]' class="form-control">
            <option value="" disabled="" selected="">Select Choice</option>
            <option value="choice1" >Choice 1</option>
            <option value="choice1">Choice 2</option>
            <option value="choice1">Choice 3</option>
        </select>
    <select name='select[]' class="form-control">
            <option value="" disabled="" selected="">Select Choice</option>
            <option value="choice1" >Choice 1</option>
            <option value="choice1">Choice 2</option>
            <option value="choice1">Choice 3</option>
        </select>
    

    (This feels like it could have been done in a loop)

    Collect the list of the names e.g.

    Check how many times each was selected:

    if (max(array_count_values($_GET['select'])) >= 3) { 
      /* 3 of the same value selected */
    }