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 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?