Re: Re: if elseif elseif elseif....

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 06 Mar 2009 10:16:47 +1100, dmagick@xxxxxxxxx (Chris) wrote:

>
>> Rather than encoding all the variable names into one long unwieldy set of statements, I
>> would put them all into an array, and then use a loop to process the array. This way all
>> the variable names are together, and the next time you want to enter another set of
>> variables you can use the same code, but read from a different array. 
>> 
>> For example:
>> 
>>    $oblig_fields = array('TitleN', 'first_nameN', '....');
>>    $ok = true; 
>>    $i = 0; while ($i < count($oblig_fields && $ok))
>> 	{
>> 	if (!isset($_POST[$oblig_fields[$i]]) || ($_POST[$oblig_fields[$i]] == ''))
>> 		{
>> 		$ok = false;
>> 		}
>> 	else
>> 		{
>> 		$results[$oblig_fields[$i]] = $_POST[$oblig_fields[$i]];
>> 		}
>> 	++$i;
>> 	}
>>    if (!$ok) { echo 'Scream!!!'; }
>
>The problem with that is if I miss 5 fields, I have 5 page refreshes to 
>get to the end & actually successfully submit the form. If you keep an 
>array of broken entries (per my previous suggestion) you get all the 
>form fields missed in one go.

Easily fixed. Just put: 

	$missing = false; 

at the start of the loop, and then:

	$missing[$oblig_fields[$i]] = true; 

each time you find a missing field. Then the array keys for $missing will tell you which
fields you have to fetch again.

>I'd just do a foreach instead of a while, count, and counter.
>
>foreach ($oblig_fields as $field_name) {
>   if (!isset($_POST[$field_name]) || empty($_POST[$field_name])) {
>     ....
>   }
>}
>
>> I am sure you could use a "break" statement in place of the "$ok = false;" above, but I
>> have never trusted the break statement. To my mind it is like a GOTO without a target.
>
>Must've been a bad experience ;) Always works for me.

Don't take me too seriously on this. But it riles me that the same peoplewho threw out the
GOTO as being too dangerous could then introduce the break statement which, as I said,
it's like a GOTO without a target. As a long-time assembly programmer, I found the
judicious use of GOTO's made for far clearer code than the mess of nested braces I am
forced to use in PHP.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux