Jump to content

validating and displaying errors from multi-dimensional array


thara

Recommended Posts

To get famous sports from counties, I created this form.

 

$sports = array (
           'Australia' =>  array (
                           1 => 'Cricket',
                           2 => 'Foot Ball',
                           3 => 'Net Ball',
                           4 => 'Kabadi',
                           5 => 'Ragby',
                           6 => 'Basket Ball',
                           7 => 'Volley Ball',
                       ),          
         'New Zealand' =>  array (
                           1 => 'Cricket',
                           2 => 'Foot Ball',
                           3 => 'Net Ball',
                           4 => 'Ragby',
                           5 => 'Basket Ball',                         
                       ),        
             'England' =>  array (
                           1 => 'Cricket',
                           2 => 'Foot Ball',
                           3 => 'Net Ball',
                           4 => 'Ragby',
                           5 => 'Karom',                           
                           6 => 'Basket Ball',                         
                           7 => 'Table Tennis',                            
                           8 => 'Tennis',                          
                       ), 
               );

echo '<br><form action="" method="post">';
   foreach ( $sports AS $country => $sport ) { 
       echo "<h3>{$country}</h3\n";    
       foreach ($sport AS $k => $v) { 
           echo "<br /><input type='checkbox'  name='country-sport[{$country}][]' value='{$k}' />{$v}\n";
       } 
   }
echo "\n<br><input type='submit' value='go' />\n</form>";

 

My problem is When I am going to validate this. Here I need to check some conditions with this form validation.

  • country-subject array is completely empty or not
  • at least 1 or upto 3 sports for each countries have selected or not

these conditions not met need to display error message.

I tried something like this.. with this code I can get 1st error message which is if whole array is empty..

this is my validation code so far..

if ( isset($_POST['country-sport']) && is_array( $_POST['country-sport'])) {

   foreach ( $_POST['country-sport'] AS $country => $sport) {      

       if ( count( $sport ) >= 1 && count( $sport ) <= 3 ) { 


       } else {
           echo "select at leat 1 or upto 3 sports for each country";          
       }   
   }
} else {    
   echo 'You have not selected sports for any country!';
}

 

 

I am expecting someone can help me out...

Thank you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.