tags:

views:

29

answers:

3

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! :)

+1  A: 

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
}
Keeper
Yes, simple. Not sure how I didn't get to this. Although for the sake of validation it's the `}else{` condition that I need to generate my error messages
DavidYell
A: 

You can try something like this:

function getFeaturedProducts() {
  $featuredProducts = array();
  foreach (array('featured1', 'featured2', 'featured3') as $key) {
    $value = intval($_POST[$key]);
    if (in_array($value, $featuredProducts)) {
      // throw validation error!
      return false;
    }
    if ($value) $featuredProducts[$key] = $value;
  }
  return $featuredProducts;
}

$products = getFeaturedProducts();
if ($products === false) {
  echo "You can't select the same product twice!";
} else {
  // $products will have the same keys as $_POST, but will only contain ones 
  // we want to update, i.e. if feature1 was 0, it will not be present at this point
  foreach ($products as $key => $value) {
    // sample update
    mysql_query("UPDATE featured SET product_id=$value WHERE key=$key");
  }
}
gnarf
A: 

If you want to ensure you have unique items in your array (for every item with a value above 0) you could do the following.

$selects = array(rand(0,2),rand(0,2),rand(0,2));

echo implode(",",$selects) . "\n";

function removeUnSelected($var) { return $var != 0; }
$selects = array_filter($selects,"removeUnSelected");

echo implode(",",$selects) . "\n";

if($selects == array_unique($selects))
{
    echo "true";
}
Brooke