Knowledgebase

Hard time with if else for six conditions

Posted by cannibal, 01-14-2013, 05:43 AM
Hi, I am having hard time figuring out the (if else) for 6 conditions My need is very simple but hard to apply at least for me Please help to achieve the following

Posted by mike86, 01-14-2013, 06:06 AM
Try that out. It uses a concatenated var $invalidmessage to concatenate all reasons for the invalid username instead of giving them multiple tries to enter an invalid one. My PHP isn't great, so you may have to fix some syntax.

Posted by cannibal, 01-14-2013, 06:28 AM
Thank man, but I done the following code and it works great but I couldn't know where to put the DB query since I don't want to query DB until the ($num > 0) condition is required to be tested. Please help how to do that

Posted by mike86, 01-14-2013, 06:33 AM
You should get the data for that "if" first, before anything happens so that you can use it in the "if". Or you could just create a function which returns the value for your $num variable and use it directly in the "if", like so:

Posted by TwineDev, 01-14-2013, 06:49 AM
Note, I got distracted while writing this, so by the time I submitted this, there were other replies to the thread, so this reply is only in reference to original post] There were a few issue in your code: 1. In the first IF statement, you have mismatched parenthesis, so you either need to get rid the first one, or add another one at the end. 2. I would suggest if (!is_numeric($username)) as once you get more used to coding this is more readable as "if not is numeric" instead of "if is number is false", (personal preference) 3. For the checking of the first character of the username, I'm assuming that somehow you are making an array ($string) of the characters in $username, if this is the case, you can just directly access the first character of the $username with $username{0} 4. Again, just my preference, but I like creating an array of error messages, so that when it is time to check, you can just do if (count($aryErr) > 0) to see if there are errors. As you have your code, you need to do the following to check if you had any errors: Now if you change any validation, either adding or removing a $result# you have to also adjust this if statement. Also from my code below, you can see one line outputs an unordered list of all errors. Another method I use if I need to show errors by the actual input instead of all in a group is to assign a key to the $aryErr, ex. $aryErr['email']='Invalid email address'; then I can specifically check for this to display it at the email input, yet still just check the count of $aryErr to see if there are any errors at all. (and alternately, you can do a foreach($aryErr as $msg) { echo $msg."
\n"; } or something similar to display differently) 5. You are checking if username is less than 8 characters, yet the message you gave was that it needs to be 8 characters. I changed the error message to match what you were actually checking. If instead you needed to actually be exactly 8 characters, you need to do (strlen($username) != 8) Lastly, depending on how fancy you want to get, you can combine all the $username checking into the following singe regex check: The way this regex works is the following: / is the "boundary" characters to define the actual expression (think of them as quotes surrounding text) ^ says it must match the start of the string you are checking 7 is a literal character you are matching, that 7 must be first [0-9] is a class of characters, in this case the characters 0 - 9 {7,} says to match the previous item (the class of any digit) between 7 and unlimited times (7 here plus the first match of the number 7 gives you 8 or more total) If you meant to say username needed to be exactly 8 characters, just use {7} here $ says to match the end of the string. / is another boundary character that has to match the opening one. (technically you can then have modifiers after this) (The reason you have the ^ and $ characters is that without them, 'a71234567z' would have matched, as the 7 + at least 7 digits can be found between the letters) Last edited by TwineDev; 01-14-2013 at 06:56 AM. Reason: Added in the note at the top

Posted by TwineDev, 01-14-2013, 06:57 AM
note if you tried this, I noticed I had an error in the line that output the errors, it is fixed now.



Was this answer helpful?

Add to Favourites Add to Favourites

Print this Article Print this Article

Also Read
Name servers (Views: 590)
Where to request? (Views: 580)
UKServers down (Views: 631)
burst slow ! (Views: 654)


Language:

Client Login

Email

Password

Remember Me

Search