SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checking fields from arrays

    Hello,

    I have a form that, after submitting checks to see if certain fields were filled out. The problem I am having is that it either checks and redisplays the entire form for each missed field in the array, or it doesnt check at all. Here is a sample. (abbreviated code array inplace of the form page)

    <?
    $field[0] = '--';
    $field[1] = '1';
    $field[2] = '2';
    $field[3] = '3';
    $field[4] = '4';
    $field[5] = '5';

    $i=0;
    foreach($field as $f){

    if($f == '--'){

    echo "Redisplay form here with message";
    }else{

    echo "UPDATE DATABASE and dispaly results";
    }
    $i++;
    }

    ?>

    I hope that was clear. I basically need to say, without looping, if any of the values = "--". Then show the error, if not than update and show the results. Using this method above error checks correctly but displays the the looped. How do I get the the looped results to show in one IF statment for checking?

    Thanks.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Try something along the lines of...

    PHP Code:
    <?php
    $aInvalidFields 
    = array();
    foreach(
    $aFields as $sName => $sValue)
    {
        if(
    '--' == $sValue)
        {
            
    array_push($aInvalidFields$sName);
        }
    }
    if(
    count($aInvalidFields) > 0)
    {
        
    printf(
            
    'The following field(s) have invalid values:- %s',
            
    implode(','$aInvalidFields)
        );
        
    #show form
    }
    else
    {
        
    #save
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Try something along the lines of...

    PHP Code:
    <?php
    $aInvalidFields 
    = array();
    foreach(
    $aFields as $sName => $sValue)
    {
        if(
    '--' == $sValue)
        {
            
    array_push($aInvalidFields$sName);
        }
    }
    if(
    count($aInvalidFields) > 0)
    {
        
    printf(
            
    'The following field(s) have invalid values:- %s',
            
    implode(','$aInvalidFields)
        );
        
    #show form
    }
    else
    {
        
    #save
    }
    ?>
    I will try this. Thanks.


Bookmarks

Posting Permissions

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