Page 1 of 2

Variable can't be empty

Posted: Tue Mar 20, 2007 10:29 pm
by iknownothing
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

Posted: Tue Mar 20, 2007 10:32 pm
by RobertGonzalez
Start with empty(). After you write some code we can review, post it so we can dive a little deeper.

Posted: Tue Mar 20, 2007 11:47 pm
by iknownothing
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>";}

Posted: Wed Mar 21, 2007 12:45 am
by dude81
iknownothing 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>";}
instead

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

}
:P Hopefully short. Can there be shorter code than this.... Anybody??

Posted: Wed Mar 21, 2007 1:35 am
by dude81

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>" ;
Alteranate version of above short code, untested , but I see still it can be reduced. Come on guys this again a code challenge for shortest code :drunk: :twisted:

Posted: Wed Mar 21, 2007 1:49 am
by feyd
Shorter, maybe. Readable, not really.

Posted: Wed Mar 21, 2007 2:03 am
by dude81
hmm not readable :roll: I fail at making code readable, I'm getting bigger expressions, who's coming with perfect , shorter, readable code ?? Anybody??

Posted: Wed Mar 21, 2007 5:26 am
by stereofrog
This is ugly because you're using a "group of variables" instead of arrays or objects.

Posted: Wed Mar 21, 2007 5:52 am
by dude81
Come on.. I did with whatever the data available. there are no arrays in the above given code. Moreover assigning all of the variables to an array will make the code big... I'm talking about shorter code sterofrog :wink:

Posted: Wed Mar 21, 2007 6:04 am
by Jenk
an array would be beneficial.. besides which, chances are this is for a form, so the variables will be in $_POST/$_GET anyway.. thusly:

Code: Select all

if (in_array("", $_POST)) $save_result = "<b>Please fill in ALL Domain Categories</b>";
but explicitly checking indexes would be better.

Posted: Wed Mar 21, 2007 6:05 am
by stereofrog
Think about making this code better, not "shorter". Start with decent data structure.

Posted: Wed Mar 21, 2007 6:10 am
by onion2k
I don't like that code. It's only testing if the field is completed, it's not actually validating it at all.

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;
}
The reason for putting errors into an associative array is so that in the form you can have something like:

Code: Select all

echo "<label class=\"labelClass".(array_key_exists('domaincompany',$errors)?" errorClass":"")."\">Domain Company</label>";

Posted: Wed Mar 21, 2007 6:12 am
by dhrosti
Does php not treat empty variables as false? if it does you can strip out all the "!empty()"s

Posted: Wed Mar 21, 2007 6:14 am
by dude81
A mix with Jenk's code makes the code better

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;
So is there any others code better than this??

Posted: Wed Mar 21, 2007 6:21 am
by onion2k
As Feyd said, it's not better, it's just shorter. When you're validating incoming data you need to be as verbose as possible.