duandao2083 2013-01-18 09:28
浏览 13
已采纳

验证并显示来自多维数组的错误

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
";    
        foreach ($sport AS $k => $v) { 
            echo "<br /><input type='checkbox'  name='country-sport[{$country}][]' value='{$k}' />{$v}
";
        } 
    }
echo "
<br><input type='submit' value='go' />
</form>";

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

  1. country-subject array is completely empty or not
  2. 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..

UPDATE : 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) { //checking that it has 3 or more values.
             //process
        } else {
            echo "select at leat 1 or upto 3 sports for {$country} ";          
        }   
    }

} else {    
    echo 'You have not selected sports for any country!';
}

UPDATE : with var_dump($_POST['country-sport']);

array(3) {
  ["Australia"]=>
  array(3) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
    [2]=>
    string(1) "3"
  }
  ["New Zealand"]=>
  array(3) {
    [0]=>
    string(1) "2"
    [1]=>
    string(1) "3"
    [2]=>
    string(1) "4"
  }
  ["England"]=>
  array(3) {
    [0]=>
    string(1) "6"
    [1]=>
    string(1) "7"
    [2]=>
    string(1) "8"
  }
}
  • 写回答

3条回答 默认 最新

  • douyingtai6662 2013-01-18 10:22
    关注

    try this:

    if ( isset($_POST['country-sport']) && !empty( $_POST['country-sport'])) {//using empty
    
        foreach ( $_POST['country-sport'] AS $country => $sport) { 
    
            if ( count($sport) > 2) { //checking that it has 3 or more values.
                 //process
            } else {
                echo "select at leat 1 or upto 3 sports for {$country} ";          
            }   
        }
    } else {    
        echo 'You have not selected sports for any country!';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线