Page 1 of 1
[Solved] Best way to do this.
Posted: Mon Jul 21, 2008 4:08 am
by Addos
Hi,
I’m wondering what might be the best way to do the following. I have about 5 from fields in a form and I want to check to make sure that the same numeric values are not entered into any of the fields.
I know to use something like this
Code: Select all
if ($_POST['v_number_1'] = = $_POST['v_number_2'])
{
echo '<br>'. 'You already have used that Number ' .'<br>';
}
But to do that with each and every field seems a whole lot of code for example
Code: Select all
if ($_POST['v_number_1'] = = $_POST['v_number_2'])
if ($_POST['v_number_1'] = = $_POST['v_number_3'])
if ($_POST['v_number_1'] = = $_POST['v_number_4'])
if ($_POST['v_number_2'] = = $_POST['v_number_2'])
if ($_POST['v_number_2'] = = $_POST['v_number_4'])
and so on which seems a convoluted way of doing this.
Any pointers as to how I can do this?
Thanks
Re: Best way to do this.
Posted: Mon Jul 21, 2008 4:17 am
by dhrosti
set the name of the inputs to name[number] like this...
Code: Select all
<input type="text" name="v_number[1]" />
Then when it gets POSTed, you will have an array which you can then use in_array() with...
Code: Select all
foreach ($_POST['v_number'] as $index=>$number) :
if (in_array($number, $_POST['v_number']) :
echo '<br>'. 'You already have used that Number ' .'<br>';
endif;
endforeach;
Edit: Sorry, that's a stupid thing to do, of course it's going to be in the array once! give me a minute to think of something better...
Re: Best way to do this.
Posted: Mon Jul 21, 2008 4:37 am
by dhrosti
Right, I think we need to use
array_count_values()...
Code: Select all
$numValues = array_count_values($_POST['v_number']);
foreach ($numValues as $value=>$occurrences) :
if ($occurrences > 1) :
echo '<br>You already have used the number '.$value.'<br>';
endif;
endforeach;
Re: Best way to do this.
Posted: Mon Jul 21, 2008 4:39 am
by padduweb
you can also search for more
Free PHP Code in this site
Re: Best way to do this.
Posted: Mon Jul 21, 2008 4:48 pm
by Addos
Thanks a mil for this help.
I had to tweak it a little but all is good now.
Code: Select all
$array = array($_POST['v_number_1'], $_POST['v_number_2'], $_POST['v_number_3']);
$results=(array_count_values($array));
foreach ($results as $value=>$occurrences){
if ($occurrences > 1) {
$duplicate_error = '<br>You already have used the number '.$value.'<br>';
}
}
Re: Best way to do this.
Posted: Mon Jul 21, 2008 6:56 pm
by Addos
Just had a little problem with this code. It runs fine and checks for a duplicate entry in one of three fields I have however if nothing is entered into two of the fields it’s reading this as a duplicate (0) or occurrence of 2 null values and is stopping the form processing.
For example if I submit the form with only one of the 3 fields filed in it prints
Array ( [789] => 1 [] => 2 )
Is it possible to run something else along with this to have it ignore empty form fields?
Thanks again
Code: Select all
$array = array($_POST['v_number_1'], $_POST['v_number_2'], $_POST['v_number_3']);
$results=(array_count_values($array));
foreach ($results as $value=>$occurrences){
if ($occurrences > 1) {
$duplicate_error = '<br>You already have used the number '.$value.'<br>';
}
Re: Best way to do this.
Posted: Mon Jul 21, 2008 8:10 pm
by manixrock
just add a check to see if it's empty:
Code: Select all
$array = array($_POST['v_number_1'], $_POST['v_number_2'], $_POST['v_number_3']);
$results=(array_count_values($array));
foreach ($results as $value=>$occurrences){
if ($value && $occurrences > 1) { // here you have the simple check
$duplicate_error = '<br>You already have used the number '.$value.'<br>';
}
Re: Best way to do this.
Posted: Tue Jul 22, 2008 12:50 am
by gmapsuser
Try the following code.
// Stores only the names starting with v_number_ in $v_array variable.
foreach ($_POST as $key=>$value) {
if (strpos($key, "v_number_") !== false) {
// Stores only the values that are not null.
if ($value != '') {
$v_array[$key] = $value;
}
}
}
// If count of $v_array is greater than zero.
if (isset($v_array) && count($v_array)>0) {
// Stores the occurences of values in $c_array variable.
$c_array = array_count_values($v_array);
}
// If count of $c_array is greater than zero.
if (isset($c_array) && count($c_array)>0) {
// Loop throught the $c_array variable.
foreach ($c_array as $value=>$occurences) {
// If any of the values are repeated set your custom message.
if ($occurences > 1) {
echo "YOUR CUSTOM MESSAGE";
}
}
}
Re: Best way to do this.
Posted: Tue Jul 22, 2008 3:00 am
by dhrosti
Addos wrote:Thanks a mil for this help.
I had to tweak it a little but all is good now.
Code: Select all
$array = array($_POST['v_number_1'], $_POST['v_number_2'], $_POST['v_number_3']);
$results=(array_count_values($array));
foreach ($results as $value=>$occurrences){
if ($occurrences > 1) {
$duplicate_error = '<br>You already have used the number '.$value.'<br>';
}
}
you can avoid having to create an array (line 1) by setting the names of the input like below...
Code: Select all
<input type="text" name="v_number[1]" />
Then in the future, if you decide to add another field, you don't need to edit the php code.
Re: Best way to do this.
Posted: Thu Jul 24, 2008 5:46 am
by Addos
Thanks a mil for all this help. Once I studied it and learned how all this fitted togther it works a treat.