Daney11 Posted March 18, 2009 Share Posted March 18, 2009 Hey guys. I have a form and when it is submitted it gives each user a number from a drop down menu. However two users are not allowed the same number so im using <?php if (eregi ('^[[:digit:]]{1,2}$', stripslashes(trim($_POST['squad_number'][$i])))) { if ($_POST['squad_number'][$i] != $_POST['squad_number'][$i]) { $squad_number = $_POST['squad_number'][$i]; } else { $squad_number = FALSE; $errors[] = 'Please enter a correct team number.'; } } else { $squad_number = FALSE; $errors[] = 'Two players can not have the same number.'; } ?> However im getting the error. "Please enter a correct team number." Even though each number is different. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted March 18, 2009 Share Posted March 18, 2009 isnt this line: if ($_POST['squad_number'][$i] != $_POST['squad_number'][$i]) { always going to be false. because it is checking it against itself so it is always going to equal itself. Quote Link to comment Share on other sites More sharing options...
Daney11 Posted March 18, 2009 Author Share Posted March 18, 2009 Hmm. How would i structure this then? I want it so that two numbers can not equal each other in the array. <select name='squad_number[<?php $i; ?>]' class="input"> <?php for ($a=0; $a<=99; $a++) { if($a<10) { $val="$a"; } else { $val=$a; } echo "<option value='$val'"; if ($val==$Squad_PlayerRow['squad_number']) { echo " selected"; } echo ">$val</option>"; } ?> </select> Thanks Quote Link to comment Share on other sites More sharing options...
Daney11 Posted March 18, 2009 Author Share Posted March 18, 2009 Ive got with this aswell if ($_POST['squad_number'][$i] == $_POST['squad_number'][$i]) { $squad_number = FALSE; $errors[] = 'Two players can not have the same number.'; } else { $squad_number = $_POST['squad_number'][$i]; } But it throws up the two players can not error. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted March 18, 2009 Share Posted March 18, 2009 Can you give me a little better of an explanation of what you were originally trying to do? Maybe if I better understand I can help you better. Quote Link to comment Share on other sites More sharing options...
Daney11 Posted March 18, 2009 Author Share Posted March 18, 2009 Basically i have a form with numbers from 0-99. Which is below. <select name='squad_number[<?php $i; ?>]' class="input"> <?php for ($a=0; $a<=99; $a++) { if($a<10) { $val="$a"; } else { $val=$a; } echo "<option value='$val'"; if ($val==$Squad_PlayerRow['squad_number']) { echo " selected"; } echo ">$val</option>"; } ?> </select> And when i click submit it goes to update.php and updates fine without any error checking. However if there are two numbers that are the same in value squad_number[$i] then i want error checking to say that you cannot choose the same number twice. if (eregi ('^[[:digit:]]{1,2}$', stripslashes(trim($_POST['squad_number'][$i])))) { $squad_number = $_POST['squad_number'][$i]; } else { $squad_number = FALSE; $errors[] = 'Please enter a correct team number.'; } if ($_POST['squad_number'][$i] == $_POST['squad_number'][$i]) { $squad_number = FALSE; $errors[] = 'Two players can not have the same number.'; } else { $squad_number = $_POST['squad_number'][$i]; } Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted March 18, 2009 Share Posted March 18, 2009 The following line is missing an echo: <select name='squad_number[<?php $i; ?>]' class="input"> should be: <select name='squad_number[<?php echo $i; ?>]' class="input"> So you are having multiple select boxes? Where does it get $i from? Can you show the full code? Quote Link to comment Share on other sites More sharing options...
Daney11 Posted March 18, 2009 Author Share Posted March 18, 2009 There is 1 select box per line in the array. Which is squad_number[$i]. My code works perfectly without error checking. Im just wanting to find a way to check that in the array of squad_number[$i], a same number hasnt been chosen twice... Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted March 18, 2009 Share Posted March 18, 2009 Are you trying to check if the same user has chose it twice or if any user has selected it and another one tries to? That is what I am trying to figure out. Quote Link to comment Share on other sites More sharing options...
Daney11 Posted March 18, 2009 Author Share Posted March 18, 2009 If the user.. i.e. Myself, has chosen the same number twice from: <select name='squad_number[<?php $i; ?>]' class="input"> <?php for ($a=0; $a<=99; $a++) { if($a<10) { $val="$a"; } else { $val=$a; } echo "<option value='$val'"; if ($val==$Squad_PlayerRow['squad_number']) { echo " selected"; } echo ">$val</option>"; } ?> </select> Num: Name: 1 Dane 2 Milk 3 Cow 1 Pig ^^ Number 1 has been chosen twice, so its wrong. Num: Name: 1 Dane 2 Milk 3 Cow 4 Pig ^^ This would return fine. Quote Link to comment Share on other sites More sharing options...
syed Posted March 18, 2009 Share Posted March 18, 2009 use in_array() function to check if a value is already in the array. Quote Link to comment Share on other sites More sharing options...
Daney11 Posted March 18, 2009 Author Share Posted March 18, 2009 Not possible? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.