Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Three item validation comparison

Tags:

validation

php

I have 3 featured product panels on the homepage, and I'm writing a CMS page for it. I'm trying to validate the items.

They are selected via three <select> elements, featured1, featured2 and featured3. The default is <option value="0" selected>Select an element</option>

I need to validate the $_POST to ensure that the user hasn't selected the same product for more than one of the panels.

I have worked out that each $_POST needs to be $_POST['featuredN'] > 0 but I can't seem to find a logical way of processing the 7 potential outcomes. Using a logic table, where 1 is a set value.

1  2  3
-------
0  0  0
1  1  1
1  0  0
0  1  0
0  0  1
1  1  0
0  1  1

If an item is 0, then I will not update it, but I want the user to be able to update a single item if needs be.

I cannot find a logical way to see if the item is not 0, and then compare it to another item if that also is not 0.

So far my colleague suggested, adding up the values. Which works to see if condition 1 0 0 0 is not met.

I have a vague feeling that some form of recursive function might be in order, but I can't quite get my brain to help me on this one! So to the collective brain! :)

like image 737
David Yell Avatar asked Dec 01 '25 04:12

David Yell


1 Answers

Why not using some simple ifs?

if($_POST['featured1'] != 0 && $_POST['featured1'] != $_POST['featured2'] && $_POST['featured1'] != $_POST['featured3']) {
    // do something with featured1
}
if($_POST['featured2'] != 0 && $_POST['featured2'] != $_POST['featured1'] && $_POST['featured2'] != $_POST['featured3']) {
    // do something with featured2
}
if($_POST['featured3'] != 0 && $_POST['featured3'] != $_POST['featured1'] && $_POST['featured3'] != $_POST['featured2']) {
    // do something with featured3
}
like image 52
Keeper Avatar answered Dec 02 '25 19:12

Keeper