sakisdem Posted January 7, 2013 Share Posted January 7, 2013 Hi to all I create a function for my password field but i have one problem. I predefine 4 codes but the function allows me to use only the last, in this case is dddd. Can anyone help me with this? The code I wrote is: function registrationCode($param,$extra=null) { $validCodes = array('aaaa','bbbb','cccc','dddd'); $bul = true; foreach($validCodes as $validCode) if(strtolower($validCode) == strtolower($param)) { $bul = true; if($tmp) $bul = false; //break; } else $bul = false; return $bul; } Quote Link to comment https://forums.phpfreaks.com/topic/272798-i-need-help-for-a-function/ Share on other sites More sharing options...
trq Posted January 7, 2013 Share Posted January 7, 2013 What exactly are you trying to achieve? Quote Link to comment https://forums.phpfreaks.com/topic/272798-i-need-help-for-a-function/#findComment-1403906 Share on other sites More sharing options...
Beeeeney Posted January 7, 2013 Share Posted January 7, 2013 Hi to all I create a function for my password field but i have one problem. I predefine 4 codes but the function allows me to use only the last, in this case is dddd. Can anyone help me with this? The code I wrote is: function registrationCode($param,$extra=null) { $validCodes = array('aaaa','bbbb','cccc','dddd'); $bul = true; foreach($validCodes as $validCode) if(strtolower($validCode) == strtolower($param)) { $bul = true; if($tmp) $bul = false; //break; } else $bul = false; return $bul; } Don't really see what you're trying to do but if you want to use one of the strings in that array for something then just use [0], [1], [2], etc. Quote Link to comment https://forums.phpfreaks.com/topic/272798-i-need-help-for-a-function/#findComment-1403908 Share on other sites More sharing options...
Christian F. Posted January 7, 2013 Share Posted January 7, 2013 The correct manner in which to validate a subset of an array, is to return true the instant you've found a matching value. What you've done above making the situation a lot more complicated than what it needs to be, and thus introducing a higher chance of bugs occurring (which did happen). This is how I'd solve a problem like this: function validateCode ($code) { // Create the array of valid codes, and flip it to enable the use of isset (). $validCodes = array ('aaaa', 'bbbb', 'cccc', 'dddd'); $validCodes = array_flip ($validCodes); // Check if the given code exists in the array. if (isset ($validCodes[$code])) { return true; } // It didn't. return false; } As you can see: Simple, clean and working. Quote Link to comment https://forums.phpfreaks.com/topic/272798-i-need-help-for-a-function/#findComment-1403912 Share on other sites More sharing options...
sakisdem Posted January 7, 2013 Author Share Posted January 7, 2013 Thanks a lot Christian! Actually I try to have some codes for verification in my field of password and I want to use only one time the code. I make this script: function registrationCode($param,$extra=null) { $validCodes = array('AAAA','BBBB','CCCC','DDDD'); $bul = true; foreach($validCodes as $validCode) if(strtolower($validCode) == strtolower($param)) { $bul = true; //verify if this was used before $db = &JFactory::getDBO(); $db->setQuery("SELECT `SubmissionId` FROM #__rsform_submission_values WHERE `FieldValue` = '".$param."' AND `FormId` = '".intval($_POST['form']['formId'])."' LIMIT 1"); $tmp = $db->loadResult(); if($tmp) $bul = false; //break; } else $bul = false; return $bul; } Please tell me how to make it with you code now Quote Link to comment https://forums.phpfreaks.com/topic/272798-i-need-help-for-a-function/#findComment-1403929 Share on other sites More sharing options...
Christian F. Posted January 7, 2013 Share Posted January 7, 2013 The principle is the same, only you don't use an array but check the result of the SELECT query. Also, you'll need a DELETE query before you return true. Remember: Keep it simple. Quote Link to comment https://forums.phpfreaks.com/topic/272798-i-need-help-for-a-function/#findComment-1403933 Share on other sites More sharing options...
sakisdem Posted January 7, 2013 Author Share Posted January 7, 2013 sorry but i am new in this. I make this: function validateCode ($code) { // Create the array of valid codes, and flip it to enable the use of isset (). $validCodes = array ('aaaa', 'bbbb', 'cccc', 'dddd'); $validCodes = array_flip ($validCodes); // Check if the given code exists in the array. if (isset ($validCodes[$code])) { return true; } //verify if this was used before $db = &JFactory::getDBO(); $db->setQuery("SELECT `SubmissionId` FROM #__rsform_submission_values WHERE `FieldValue` = '".$param."' AND `FormId` = '".intval($_POST['form']['formId'])."' LIMIT 1"); $tmp = $db->loadResult(); if($tmp) // It didn't. return false; } Quote Link to comment https://forums.phpfreaks.com/topic/272798-i-need-help-for-a-function/#findComment-1403935 Share on other sites More sharing options...
sakisdem Posted January 7, 2013 Author Share Posted January 7, 2013 can you please show me how it gone? I am new in PHP Thanks Quote Link to comment https://forums.phpfreaks.com/topic/272798-i-need-help-for-a-function/#findComment-1403948 Share on other sites More sharing options...
Christian F. Posted January 7, 2013 Share Posted January 7, 2013 I've already told you how to do it, I'm not going to write the code for you. You need to step back a bit, think about what you need to do in order to get the result you want. Write it down in a very simple step-by-step list, preferably using only one verb and one noun per line, until you've accurately described every single step of the process. Once you've done that, you'll find that actually writing the code will be come trivial, as you've already solved all of the problems. Trying to solve the problems at hand, while at the same time writing the code, only makes things overly complicated. Which only ends up with confusion, bugs and frustration. There is a very good reasoning behind my signature, after all. Quote Link to comment https://forums.phpfreaks.com/topic/272798-i-need-help-for-a-function/#findComment-1403954 Share on other sites More sharing options...
sakisdem Posted January 7, 2013 Author Share Posted January 7, 2013 I don’t want to write the code because I want to copy paste! I want to see how it done! And you said to me that I need to replace the array with the result of the SELECT query but I wand to have predefine codes and the function to putt the only one time. All I know is the code I show you If I know how to do it I am not bother you with sally questions Quote Link to comment https://forums.phpfreaks.com/topic/272798-i-need-help-for-a-function/#findComment-1403961 Share on other sites More sharing options...
scootstah Posted January 7, 2013 Share Posted January 7, 2013 I don’t want to write the code because I want to copy paste! I want to see how it done! Well I hate to burst your bubble, but that's not how it works. If you want code written for you, head on over to the Freelancing section and pay someone to write it. Quote Link to comment https://forums.phpfreaks.com/topic/272798-i-need-help-for-a-function/#findComment-1403962 Share on other sites More sharing options...
Jessica Posted January 7, 2013 Share Posted January 7, 2013 If you try, we will help you. We will not write it for you, that doesn't help you learn. You might think it does, but I promise you, you won't understand it the same way as if you learn how to figure out the problem and solve it. Where are you stuck? Quote Link to comment https://forums.phpfreaks.com/topic/272798-i-need-help-for-a-function/#findComment-1403964 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.