Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help/tips on how I should code this...

    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

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    1 way
    PHP Code:
    <?
    $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 );
    }
    ?>
    Last edited by firepages; 05-16-2004 at 04:35 AM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages
    1 way
    PHP Code:
    <?
    $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
    Last edited by DiarYofaMadmaN; 05-16-2004 at 06:17 PM.

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    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() 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.

    PHP Code:
    <? 
    /*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>
    Last edited by firepages; 05-16-2004 at 06:26 PM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages
    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() 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.

    PHP Code:
    <? 
    /*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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •