Variable can't be empty
Moderator: General Moderators
- iknownothing
- Forum Contributor
- Posts: 337
- Joined: Sun Dec 17, 2006 11:53 pm
- Location: Sunshine Coast, Australia
Variable can't be empty
Hey Guys,
I'm struggling for a way to write a clean piece of code, that if a certain variable is not empty, then a certain group of variables cant be empty, otherwise it is an error.
Can anyone help?
Thanks
I'm struggling for a way to write a clean piece of code, that if a certain variable is not empty, then a certain group of variables cant be empty, otherwise it is an error.
Can anyone help?
Thanks
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Start with empty(). After you write some code we can review, post it so we can dive a little deeper.
- iknownothing
- Forum Contributor
- Posts: 337
- Joined: Sun Dec 17, 2006 11:53 pm
- Location: Sunshine Coast, Australia
so far i have this, and it looks messy..
Code: Select all
$allgood = 0;
if (!empty($domaincompany)){
if(!empty($domainexp)){
if(!empty($domainuser)){
if(!empty($domainpass)){
if(!empty($domainregkey)){
$allgood = $allgood + 1;
}else {
$save_result = "<b>Please fill in ALL Domain categories</b>";}
}else {
$save_result = "<b>Please fill in ALL Domain categories</b>";}
}else {
$save_result = "<b>Please fill in ALL Domain categories</b>";}
}else {
$save_result = "<b>Please fill in ALL Domain categories</b>";}
}else {
$save_result = "<b>Please fill in ALL Domain categories</b>";}insteadiknownothing wrote:Code: Select all
$allgood = 0; if (!empty($domaincompany)){ if(!empty($domainexp)){ if(!empty($domainuser)){ if(!empty($domainpass)){ if(!empty($domainregkey)){ $allgood = $allgood + 1; }else { $save_result = "<b>Please fill in ALL Domain categories</b>";} }else { $save_result = "<b>Please fill in ALL Domain categories</b>";} }else { $save_result = "<b>Please fill in ALL Domain categories</b>";} }else { $save_result = "<b>Please fill in ALL Domain categories</b>";} }else { $save_result = "<b>Please fill in ALL Domain categories</b>";}
Code: Select all
$allgood = 0;
if((!empty($domaincompany)&&(!empty($domainexp)&&(!empty($domainuser)&&(!empty($domainpass)&&(!empty($domainregkey)){
$allgood = $allgood + 1;
}else{
$save_result = "<b>Please fill in ALL Domain categories</b>";
}Code: Select all
$allgood = 0;
((!empty($domaincompany)&&(!empty($domainexp)&&(!empty($domainuser)&&(!empty($domainpass)&&(!empty($domainregkey))? $allgood++ :$save_result = "<b>Please fill in ALL Domain Categories</b>" ;- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
an array would be beneficial.. besides which, chances are this is for a form, so the variables will be in $_POST/$_GET anyway.. thusly:
but explicitly checking indexes would be better.
Code: Select all
if (in_array("", $_POST)) $save_result = "<b>Please fill in ALL Domain Categories</b>";- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
I don't like that code. It's only testing if the field is completed, it's not actually validating it at all.
The reason for putting errors into an associative array is so that in the form you can have something like:
Code: Select all
$errors = array();
$strErrorMessage = "";
if(empty($domaincompany)) { $errors['domaincompany'] = "Domain Company"; }
if(empty($domainexp)) { $errors['domainexp'] = "Domain Expiry Date"; }
if(empty($domainuser)) { $errors['domainuser'] = "Domain User"; }
if(empty($domainpass)) { $errors['domainpass'] = "Domain Password"; }
if(empty($domainregkey)) { $errors['domainregkey'] = "Domain Reg Key"; }
if (count($errors)>0) {
$strErrorMessage = "You must complete the following fields: <br>".implode("<br>",$errors)."<br>";
}
if (!checkValidDate($domainexp)) { $strErrorMessage .= "Domain Expiry Date is not a valid date.<br>"; }
if (!checkValidUser($domainuser)) { $strErrorMessage .= "Domain User is not valid.<br>"; }
if (!checkValidRegKey($domainregkey)) { $strErrorMessage .= "Domain Reg Key is not valid.<br>"; }
if (empty($strErrorMessage)) {
$allgood = $allgood + 1;
} else {
$save_result = $strErrorMessage;
}
Code: Select all
echo "<label class=\"labelClass".(array_key_exists('domaincompany',$errors)?" errorClass":"")."\">Domain Company</label>";A mix with Jenk's code makes the code better
So is there any others code better than this??
Code: Select all
$allgood=0;
$list=array($domaincompany,$domainexp,$domainuser,$domainpass,$domainregkey);
if (in_array("", $list)) $save_result = "<b>Please fill in ALL Domain Categories</b>";
$allgood = $allgood + 1;