如何将输入的值分配给我的数组?

时间:2017-01-03 18:09:24

标签: php arrays for-loop input

用户可以输入他们想要的团队数量,并在屏幕上显示输入数量,以便从用户输入。我做到了。但是我希望将我的输入值分配给我的阵列元素,比如$ teams [$ i] = $ _ GET [' team'。$ i。'&#39 ;] for for循环。这样,我可以看到用户输入的数组元素。我尝试用for循环在我的代码中分配它们。但我看不到我想要的结果。有任何语法错误,但我相信,我做了逻辑错误。任何人都可以帮忙吗?提前感谢您的帮助。

        <form method="get" name="teamNumber" style="margin:auto;">
        <label>Competition Name:&nbsp;
                <input type="textbox" name="competitionName"/></br></br>
            </label>
            <label>How many teams you want to add:&nbsp;
                <input type="textbox" name="teamnumber"/></br></br>
            </label>
            <input type="submit" name="sub" value="Create Teams!"/></br></br>
        </form>

<?php

    $competitionName=$_GET['competitionName'];
    $teamnumber=$_GET['teamnumber'];
    $submit=$_GET['sub'];
    $draw=$_GET['draw'];
    $teams=array();



            if(isset($submit))
            {
                if ( $teamnumber==1||$teamnumber==3||$teamnumber==5||$teamnumber==6||$teamnumber==7||$teamnumber==9||$teamnumber==11||$teamnumber==13||$teamnumber==14||
                     $teamnumber==15||$teamnumber==17||$teamnumber==18||$teamnumber==19||$teamnumber==20||$teamnumber==21||$teamnumber==22||
                     $teamnumber==23||$teamnumber==24||$teamnumber==25||$teamnumber==26||$teamnumber==27||$teamnumber==28||$teamnumber==29||
                     $teamnumber==30||$teamnumber==31  /* && $teamnumber>32 */ ) 
                    {
                        echo '<p>'.'You typed invalid team number'.'</p>' ; 
                    }

                    else{       
                            echo '<form method="get" name="addTeam">';

                            for($i=1;$i<=$teamnumber;$i++)
                            {
                                echo '<label> <input type="textbox" name="team'.$i.'" value=" Team'.$i.'"/> </label> <br> <br>' ;
                            }


                        switch($teamnumber)
                        {
                        case $teamnumber==2:
                            echo '<label> <input type="submit" value="Draw Final Round" name="draw"> </label>'. '</form>';
                            break;
                        case $teamnumber>3 && $teamnumber<=4:
                            echo '<label> <input type="submit" value="Draw Semi-Finals Round" name="draw"> </label>'. '</form>';
                            break;
                        case $teamnumber>4 && $teamnumber<=8:   
                            echo '<label> <input type="submit" value="Draw Quarter-Finals Round" name="draw"> </label>'. '</form>';
                            break;
                        case $teamnumber>8 && $teamnumber<=16:  
                            echo '<label> <input type="submit" value="Draw 16 Round" name="draw"> </label>'. '</form>';
                            break;
                        case $teamnumber>16 && $teamnumber<=32: 
                            echo '<label> <input type="submit" value="Draw 32 Round" name="draw"> </label>'. '</form>';
                            break;
                        }


                         }//end of else

                }// end of if

                if(isset($draw))
                {
                    echo '<p>'.$competitionName.'</p>' ;

                    for($j=1;$j<=$teamnumber;$j++)
                    {
                        $teams[$j]=$_GET['team'.$j.'']; 
                        echo '<p> Team'.$j.':'.$teams[$j].'</p>' ;
                    }


                }


 ?>

1 个答案:

答案 0 :(得分:0)

您正在将GET值直接传递给变量,而不检查它是否已设置。这会导致错误。

尝试这样的事情。

if ( isset( $_GET['sub'] ) ) {
   if( !isset( $_GET['competitionName'] ) || !isset( $_GET['competitionName'] ) ) {
     echo 'Your Error Message';
    }
    else {
      $competitionName=$_GET['competitionName'];
      $teamnumber=$_GET['teamnumber'];
      $submit=$_GET['sub'];
      //$draw=$_GET['draw'];
      $teams=array();

      if( $teamnumber % 2 == 0 ) {
        //Code to be executed. switch statement and form in your case. 
      }
      else {
       //warn user when input value is odd. 1,3,5,7 etc 
       echo '<p>'.'You typed invalid team number'.'</p>' ;
      }
    }
  // Code  to be executed here. in your case for loop
}