doudang8824 2015-04-16 20:17
浏览 33
已采纳

PHP循环中的多个选择框

I am busy working on a university assignment which must do the following:

  • Show list of sporting events for week
  • User Submits the teams they think will win
  • At end of week, calculate which users had the most correct picks

In working on above I have created a form like this, which lets the user select who they think will win.

enter image description here

The form data is collected from a database called events which looks like this:

enter image description here

My form is generate with the following code

   //create form
    echo'<form name="" action="" method="post">';
    echo'<fieldset>';
    echo'<legend>Make Your Picks</legend>';

//create query
$sql = "Select * FROM events";
$result = mysql_query($sql);

while($row=mysql_fetch_array($result)){
    $team1 = $row['team1'];
    $team2 = $row['team2'];

    //dislay teams
    echo'<h3>'.$team1.' VS '.$team2.'</h3>';
    echo'<select name="'.$row['event_id'].'">';
            echo'<option value="'.$row['team1'].'">'.$team1.'</option>';
            echo'<option value="'.$row['team2'].'">'.$team2.'</option>';
            echo'</select>';    
}//while

echo'</legend></fieldset>';
echo'<input type="submit" class="buttono" value="Submit" name="submit" />';
echo'</form>'

I would like to get a more experienced users opinion on my logic and perhaps a tip if I am on the correct path and what I could be doing better, in terms of implementation,since I have ZERO confidence in my current approach.

  1. I am creating multiple selectboxes inside the while loop, each select box has a unique name of the event_id value from db which will get inserted into the picks database (see below), this doesn't seem very efficient is there an alternative way I can approach this?

enter image description here

  1. Am I correct in assigning the name attribute of the selctbox the value of the fixture_id?

  2. Is there perhaps a more efficient way I can approach this problem that you can suggest?

Just thought it would be interesting getting an experienced users view on this

  • 写回答

1条回答 默认 最新

  • dtvfshi5248 2015-04-16 20:21
    关注

    use an array for your name variable like name="event[7]" which would look like

    echo'<select name="event['.$row['event_id'].']">';
    

    then when you are gathering data you run a loop like

    foreach($_POST['event'] as $event_id => $winner){ 
    // do something here 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃