| Author |
Message |
Desbrina Jadeite

Joined: 11 Jun 2005 Posts: 2958 Location: Earth
|
Posted: Sat Nov 17, 2007 10:21 am Post subject: checking value of fields |
|
|
I know how to do this the extremely long way, but i'm wondering if theres a simpler solution.
I have 6 text fields, all named num1 - num6
I need to make sure that the values in one text fields does not equal the value of another text field
IE: User enters
1 2 3 4 5 6
in the text fields, the values are different, so it does an sql query
if they enter
1 2 3 4 4 5
in them, it comes back saying that you cant have duplicate values and then does nothing else
The only way i know is by using a lot of if statements, but there would be a lot of them doing it that way _________________ Midnight Tempest - A Sailor Moon TCG
Balanced Force - A Star Wars TCG |
|
| Back to top |
|
| |
Scott tutorialtoday.com

Joined: 24 Mar 2005 Posts: 2582 Location: Mississauga, Ontario
|
Posted: Sat Nov 17, 2007 10:37 am Post subject: |
|
|
The easiest way I can think of is something like this:
| Code: | $values = array($_POST['num1'], $_POST['num2'], $_POST['num3'], $_POST['num4'], $_POST['num5'], $_POST['num6']);
$dupes = array_count_values($values)
foreach($dupes as $x) {
if($x > 1) {
echo 'There is a duplicate....';
}
} |
_________________ Tutorial Management Script - Version 1.3 Released
TutorialToday - Up and running, submit your tutorials!
Linux Tutorials - Coming Soon |
|
| Back to top |
|
| |
Desbrina Jadeite

Joined: 11 Jun 2005 Posts: 2958 Location: Earth
|
|
| Back to top |
|
| |
marinaroz Grey Scaled

Joined: 04 Mar 2004 Posts: 2765 Location: Israel
|
Posted: Sat Nov 17, 2007 3:47 pm Post subject: |
|
|
If this is for some kind of interaction with a user, you could write a javascript script to do the testing instead of going to the server. This way you can serve the user an appropriate error message without having the page refreshed. _________________ Tarakana NET |
|
| Back to top |
|
| |
Desbrina Jadeite

Joined: 11 Jun 2005 Posts: 2958 Location: Earth
|
Posted: Sat Nov 17, 2007 3:50 pm Post subject: |
|
|
you, its part of a script where there are 6 text fields, where a user has to enter a value between 1 and 20 in each, but not have the same number in any of them
With the example Scott posted, i can seem to work out where to put the sql statement _________________ Midnight Tempest - A Sailor Moon TCG
Balanced Force - A Star Wars TCG |
|
| Back to top |
|
| |
krt ...

Joined: 11 Jan 2005 Posts: 4607 Location: Australia
|
Posted: Sat Nov 17, 2007 4:50 pm Post subject: |
|
|
Easier way:
Box 1: <input type="text" name="num[1]" />
Box 2: <input type="text" name="num[2]" />
Box 3: <input type="text" name="num[3]" />
...
PHP:
| Code: | $array = $_POST['num'];
if (count($array) != count(array_unique($array))) {
// Duplicate found
} |
|
|
| Back to top |
|
| |
|
|
|