PDA

View Full Version : need help/tips on how I should code this...



DiarYofaMadmaN
05-16-2004, 01:43 AM
Can anyone help or give me tips on how I should go about coding this?

I have a form with a table. In that Table I have 14 text boxes and 2 listboxes. I need to code a script that checks to see if the fields are not empty. Is this possible to do with out using if statments, because if I use if statements i'm most likely going to have to use an abundance of them

firepages
05-16-2004, 03:32 AM
1 way


<?
$validate = array( 'field1' , 'field2' , 'field3' , 'field4' , 'etc...' ) ;
foreach( $validate as $k=> $v ){
if( empty( $_POST[$k] ) ){
$errs[] = 'Field '.$k.' is required';
}
}

if( isset( $errs ) ){
echo implode( '<br />' , $errs );
}
?>

DiarYofaMadmaN
05-16-2004, 05:00 PM
1 way


<?
$validate = array( 'field1' , 'field2' , 'field3' , 'field4' , 'etc...' ) ;
foreach( $validate as $k=> $v ){
if( empty( $_POST[$k] ) ){
$errs[] = 'Field '.$k.' is required';
}
}

if( isset( $errs ) ){
echo implode( '<br />' , $errs );
}
?>


Thanks, I was thinking about using Arrays but wasn't sure how set the emtpy arrays into a variable and print out that variable with the list of empty arrays, But now I do!

I understand the arrays, foreach and empty, but not exactly sure about the difference between just setting $errs without the [] and $errs[]. Another thing, where did you get $v from? IF you could explain I would greatly appreciate that.

-Rich

firepages
05-16-2004, 05:18 PM
Hi, using $errs[]= makes $errs an array , and each iteration adds to the array , you can only do this with numerically indexed arrays , not with associative ones (where you would actually have to give the $err['key']

This way you can display several error messages at once.

implode() (http://www.php.net/implode) is a quick way of getting the contents of an array into a string.


<edit>
the $k=>$v in the foreach loop gets the array index and the array value , as here we need to know the 'name' of the variable.
So if we had a form field
..name="wombat" value="good"
. $k would equal 'wombat' and $v 'good'

we dont actually need $v here , but its useful to have as the next step may be to also say validate types (as opposed to just existance), you may want to do further calculations on $v, e.g.



<?
/*field 1 & 2 may need to be numeric*/
$int_validate = array( 'field1' , 'field2') ;
foreach( $int_validate as $k=> $v ){
if( empty( $_POST[$k] ) ){
$errs[] = 'Field '.$k.' is required';
}else{
if( !is_numeric( $v ) ){
$errs[] = 'Field '.$k.' should be an numeric value';
}
}
}

if( isset( $errs ) ){
echo implode( '<br />' , $errs );
}
?>

</edit>

DiarYofaMadmaN
05-18-2004, 01:56 AM
Hi, using $errs[]= makes $errs an array , and each iteration adds to the array , you can only do this with numerically indexed arrays , not with associative ones (where you would actually have to give the $err['key']

This way you can display several error messages at once.

implode() (http://www.php.net/implode) is a quick way of getting the contents of an array into a string.


<edit>
the $k=>$v in the foreach loop gets the array index and the array value , as here we need to know the 'name' of the variable.
So if we had a form field
..name="wombat" value="good"
. $k would equal 'wombat' and $v 'good'

we dont actually need $v here , but its useful to have as the next step may be to also say validate types (as opposed to just existance), you may want to do further calculations on $v, e.g.



<?
/*field 1 & 2 may need to be numeric*/
$int_validate = array( 'field1' , 'field2') ;
foreach( $int_validate as $k=> $v ){
if( empty( $_POST[$k] ) ){
$errs[] = 'Field '.$k.' is required';
}else{
if( !is_numeric( $v ) ){
$errs[] = 'Field '.$k.' should be an numeric value';
}
}
}

if( isset( $errs ) ){
echo implode( '<br />' , $errs );
}
?>

</edit>

Thanks for clearing all that up for me i understand it greatly now :-) Just one more questions. is the foreach function like a loop and does it go through each array one after another till it reaches the end, then the if statement gets executed?