0

I have an HTML form with a number of statements which the user can rank as 1, 2, 3 or 4. I group the statements together in clusters of 4 and the ranks should only be used once per cluster (for example 4,3,2,1 not 1,1,2,3 etc.).

I need some PHP code that will check each cluster of 4 answers and display an error message if it finds any duplicates.

This is the 1st cluster in my form (written for Contact Form 7, hence the square brackets)

1. I like pasta
[select* one-01 first_as_label class:cluster1 "--" "1" "2" "3" "4"]

2. I like potato
[select* one-02 first_as_label class:cluster1 "--" "1" "2" "3" "4"]

3. I like rice
[select* one-03 first_as_label class:cluster1 "--" "1" "2" "3" "4"]

4. I like fries
[select* one-04 first_as_label class:cluster1 "--" "1" "2" "3" "4"]

HTML:

1. I like pasta
<select name="one-01" class="cluster1" ><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>

2. I like potato
<select name="one-02" class="cluster1"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>
    
3. I like rice
<select name="one-03" class="cluster1"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>

4. I like fries
<select name="one-04" class="cluster1"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>

I'm very new to PHP and I don't know the best way to treat the answers of a cluster of values as an array. (This is the best I could do. This is added to my child theme functions.php)

// Function to test for duplicate form entries
if(count(array_unique($cluster1))<count($cluster1))
{
    // Array has duplicates
    echo "You must only use each score once per cluster";
}
else
{
    // Array does not have duplicates
}

I think this php check array value for duplicate is the closest to what I'm trying to do, but I don't know how to treat all values of the same class as an array. I think there many be other errors in my implementation too.

All advice will be gratefully received by this novice!

Thanks

4
  • 1
    Id must be unique. Commented Feb 9, 2022 at 12:55
  • Can you post html too? Commented Feb 9, 2022 at 12:55
  • The easiest way to get your values grouped together, is by using a different field names in the first place. stackoverflow.com/questions/4543500/… Commented Feb 9, 2022 at 13:32
  • I have edited my code to remove id and replace it with class. I have also included the HTML Commented Feb 9, 2022 at 14:03

0

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.