PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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.
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
Last edited by Addos on Thu Jul 24, 2008 5:47 am, edited 1 time in total.
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...
$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;
$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>';
}
}
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
$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>';
}
$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>';
}
$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...