Stop Form Values Being Equal

ChrDav

New Member
Messages
15
Reaction score
0
Points
0
Hi everyone,

I am trying to stop form values being equal to each other. My form contains about 30 different SELECT/OPTION combinations. I want none of the select fields to have the same option selected when Submit is pressed. These values (assuming none are equal) are then stored in a SQL database.

My only thought so far is to have repeated 'if' combinations checking every combination but I am sure there must be a better solution.

Thanks,
Chris.
 

sybregunne

Member
Messages
54
Reaction score
0
Points
6
A suggestion would be to use javascript create an array. every time an option is selected shortlist the array you may use an index, mark off those array elements that have been selected.

Of course you may use checkboxes to make sure each selection is unique.
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
well, there's a couple of ways.

one would be making an array storing all the field names, the other would be having field values being the same, just with a number in brackets at the end form[1], form[2]. as far as html goes, it's 2 field names, but php can handle those names easily.

as for the nitty gritty, i'm not gonna get into it now, because it's late and i have to get up early for work tomorrow, and have been having a hard time doing so. and i'm working outside all day (all week long so far) and it's a little cold in delaware ;)
 

dickey

New Member
Messages
128
Reaction score
0
Points
0
xplozion could you care to elaborate on that second method. I want to apply this on my site too. but I was thinking more of using javascript but when you suggest php I think that would be better.

Thanks...
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
ok, it's been a while, but the forum software i use (fluxbb) uses the same method, but not for comparing data. give me a bit and i'll try to work on a method (also designing new site), since i've never actually did this, but with arrays and either foreach'es or for's, it shouldn't be too hard.

here's my brainstorm though:

since the way the input name's are written test[1], test[2], etc... it's already built an array. from there, use the foreach to go through one by one for the main string defined by foreach, and if there's a match, return 0, making sure to make it not catch the same form and return on that (test[1] == test[1] return 0) ;). if there's no match, return 1 (which will make it easy for if(check($blah)) {

PHP:
function form_check($name)
{
  foreach ($name AS $k => $v)
  {
    foreach($name AS $n => $c)
    {
      if($v == $c) && ($k != $n)
      {
        return 0;
      }
    }
  }
  return 1;
}

//usage

if (form_check($_POST['test']))
{
  //no matches
}
else
{
  //matches
}
might be some errors, but i didn't test this, just wrote it out on here :)

-xP
 
Last edited:

ChrDav

New Member
Messages
15
Reaction score
0
Points
0
Ah yes, foreach loops, of course.

I'll look at it in a couple of days when I get some free time, thanks xPlozion
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
np, just let me know if there's any major problems that you cant fix yourself :)

if it works, please give rep (that goes for you too dickey) ;)
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Just an FYI, if you're expecting string values then you should use === for comparison. However in this case, array_unique() would be faster than making a function:

PHP:
if (count(array_unique($_POST['values'])) == count($_POST['values'])) {
    //no matches
}
else {
    //matches
}
But if you're expecting numeric data, then you'll have to use a function like the one xPlozion provided. This is because array_unique() checks if the values are *identical* as strings. So "1.0" wouldn't match "1".
 

ChrDav

New Member
Messages
15
Reaction score
0
Points
0
I just spent some time playing around with the code. xPlozion, thanks heaps! I've modified your code slightly to suit my uses better but the foreach loops was the key. There was just one little error:
PHP:
      if($v == $c) && ($k != $n)
It should read:
PHP:
      if(($v == $c) && ($k != $n))
It was only reading the ($v == $c) as the if statement and ignoring the second part.

Thanks,
Chris.
 
Top