Jump to content

I need help for a function


sakisdem

Recommended Posts

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;
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. ;)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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. ;)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.