0

郡から有名なスポーツを取得するために、このフォームを作成しました。

$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>";

私の問題は、これをいつ検証するかです。ここで、このフォーム検証でいくつかの条件を確認する必要があります。

  1. country-subject 配列が完全に空かそうでないか
  2. 各国が選択したかどうかについて、少なくとも 1 つまたは最大 3 つのスポーツ

これらの条件が満たされていない場合は、エラー メッセージを表示する必要があります。

私はこのようなことを試しました..このコードで、配列全体が空の場合の最初のエラーメッセージを取得できます..

更新:これはこれまでの私の検証コードです..

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"
  }
}
4

3 に答える 3

1

これを試して:

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!';
}
于 2013-01-18T10:22:13.560 に答える
1

以下のコードを試してください:

<?php
if (isset($_POST['country-sport']) && is_array($_POST['country-sport']))
{      
    foreach ($_POST['country-sport'] AS $country => $sport)
    {
        $varTotal = 0;
        foreach($sport as $k=>$v)
        {
            if($v != '')
            {
                $varTotal += 1;
            }
        }
        if ($varTotal >= 1 && $varTotal <= 3)
        {

        } 
        else
        {
            $arrError[$country] = 'select at least 1 or upto 3 sports for '.$country ;
        }
    }
    print_r($arrError);
} 
else
{
    echo 'You have not selected sports for any country!';
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $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";
            $i=0;
            foreach ($sport AS $k => $v)
            {  
                // This will help to get all the fields name in post 
                echo "<input type='hidden'  name='country-sport[{$country}][]' value='' />";              
                echo "<br /><input type='checkbox'  name='country-sport[{$country}][]' value='{$k}' />{$v}\n";
                $i++;
            }
        }
        echo "\n<br><input type='submit' value='go' />\n</form>";
        ?>
    </body>
</html>
于 2013-01-18T10:18:31.280 に答える
0

それを行う最善の方法は、各国をループして、その国のスポーツがあなたが述べた要件を満たしていることを個別に確認してチャンクに分割することです (空かどうか、および/または少なくとも 1 から 3 のスポーツが選択されています)。 )、次にエラーの結果を別の配列に入れ、検証済みかどうかを示します。

つまり、国を通過すると、有効なものと有効でないものに関して、以下のような構造を持つ配列が得られます。そこから、エラーとその理由を表示できます。すべてを分類したために、正しく入力されていない国を強調表示することもできます。

$result = array(
    'Australia' => array('error' => false, 'reason' => ''),
    'New Zealand' => array('error' => true, 'reason' => 'No countries selected')
);
于 2013-01-18T09:38:54.987 に答える