Skip to content
Advertisement

Distributing sequential array items as mixed

I have an ordered list from 1 to 15. I put these list items in a form and assigned a checkbox to each. An user can choose whatever he wishes from the items in this form.

When the user fills in and sends the form, I want to separate this list under certain headings.

For example:

<form>
    <input type="checkbox" value="1" name="checklist[]"> Option 1
    <input type="checkbox" value="2" name="checklist[]"> Option 2
    <input type="checkbox" value="3" name="checklist[]"> Option 3 
    <input type="checkbox" value="4" name="checklist[]"> Option 4
    <input type="checkbox" value="5" name="checklist[]"> Option 5
    <input type="checkbox" value="6" name="checklist[]"> Option 6
    <input type="checkbox" value="7" name="checklist[]"> Option 7
    <input type="checkbox" value="8" name="checklist[]"> Option 8
    <input type="checkbox" value="9" name="checklist[]"> Option 9
    <input type="checkbox" value="10" name="checklist[]"> Option 10
    <input type="checkbox" value="11" name="checklist[]"> Option 11
    <input type="checkbox" value="12" name="checklist[]"> Option 12
    <input type="checkbox" value="13" name="checklist[]"> Option 13
    <input type="checkbox" value="14" name="checklist[]"> Option 14
    <input type="checkbox" value="15" name="checklist[]"> Option 15
</form>

According to the results you choose:

Category A
Questions in this category: 1 – 3 – 4 – 10
You selected: 3 – 4 – 10

Category B
Questions in this category: 2 – 8 – 11 – 14 – 15
You selected: 2 – 8 – 11

Category C
Questions in this category: 5 – 6 – 7 – 9 – 12 – 13
You selected: 5 – 7 – 12 – 13

Can you give me an idea of how I can do this?

Advertisement

Answer

I found the answer to the question I asked by searching. For those who need help with this, I add it here.

First, make the options of each category into an array.

$category_A = array (1, 3, 4, 10);
$category_B = array (2, 8, 11, 14, 15);
$category_C = array (5, 6, 7, 9, 12, 13)

Then insert these arrays into the foreach loop and check if the selected options are in this array. An example for category A:

foreach ( $category_A as $value ) {

    if ( in_array( $value, $_POST['check_list'] ) ){

        echo '<span class="selected">'. $value .'</span>';

    } else {

        echo '<span>'. $value .'</span>';

    }

}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement