如何在PHP中创建单独的单选按钮组

时间:2014-12-06 18:04:24

标签: php html radio-button

我的输出中出现了单选按钮问题。我想为返回的查询的每一行提供2个单选按钮。我可以正确显示单选按钮,但所有单选按钮似乎都在同一组中,因此我只能从所有单选按钮中选择1个,而不是每个查询1个。我希望这是有道理的。

我的节目是一个足球选秀页面。所以它列出了主队/客队,然后它会给你2个单选按钮。一个是主队,一个是客队。然后你只需选择你认为会赢得哪一个。我真的需要帮助这些单选按钮。

这是我现在的代码

<!DOCTYPE html>
 <html>
 <head>
<title>games</title>
</head>
<body>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>">
<select name="weekNo">
<option value="1">week 1</option>
<option value="2">week 2</option>
<option value="3">week 3</option>
<option value="4">week 4</option>
<option value="5">week 5</option>
<option value="6">week 6</option>
<option value="7">week 7</option>
<option value="8">week 8</option>
<option value="9">week 9</option>
<option value="10">week 10</option>
<option value="11">week 11</option>
<option value="12">week 12</option>
<option value="13">week 13</option>
<option value="14">week 14</option>
</select>
<input type="submit" name="submit" value="Get Games" />
</form>
<br>
<hr>
<?php

 $conn = 
 or die('Could not connect: ' . pg_last_error());
 if(isset($_POST['submit'])) //submit button pressed
{
$query=NULL; //prevent compile error

$weekNum = $_POST['weekNo'];

    $query = "SELECT a.game_no AS game_number, a.home AS home_team,
            homeTeam.wins AS home_wins, homeTeam.losses AS home_losses,
            a.away AS away_team, awayTeam.wins AS away_wins,
            awayTeam.losses AS away_losses, a.spread AS spread
            FROM weekly_stats AS a
            INNER JOIN team AS homeTeam ON a.home = homeTeam.name
            INNER JOIN team AS awayTeam ON a.away = awayTeam.name
            WHERE a.week_no = $weekNum";
 $result = pg_query($query) or die ('Query failed: ' .pg_last_error());

    $query2 = "SELECT week_no, game_no FROM weekly_stats";
    $result2 = pg_query($query2) or die ('Query failed: ' . pg_last_error());

 // Printing results in HTML
 echo "<br>There are " . pg_num_rows($result) . " records found.\n<p></p>\n";
 echo "<table border=1>\n\t<tr>\n";

 for($i=0; $i<pg_num_fields($result); $i++)
 {
 echo "\t\t<th>" . pg_field_name($result, $i) . "</th>\n";
 }
 echo "\t\t<th>Picks</th>\n";

  echo "\t</tr>\n";

 while ($line = pg_fetch_array($result, null, PGSQL_ASSOC))
 {

    echo "\t<tr>\n";

    foreach ($line as $col_value)
    {
    echo "\t\t<td>$col_value</td>\n";
    }

    echo "<td><input type=\"radio\" name=\"picks\" value=\"home\">Home
  <input type=\"radio\" name=\"picks\" value=\"away\">Away</td>";


 echo "\t</tr>\n";
 }

 echo "</table>\n";
 // Free resultset
 pg_free_result($result);
 }
 // Closing connection
 pg_close($conn);

 ?> 
 </body>
 </html>

1 个答案:

答案 0 :(得分:0)

由于您要求用户根据他们认为会赢的人选择主队或客队,我想您应该唯一地引用每一行的单选按钮。像这样的东西

     echo "<br>There are " . pg_num_rows($result) . " records found.\n<p></p>\n";
     echo "<table border=1>\n\t<tr>\n";
     echo "<form method='POST'>";
     for($i=0; $i<pg_num_fields($result); $i++)
     {
        echo "\t\t<th>" . pg_field_name($result, $i) . "</th>\n";
     }
     echo "\t\t<th>Picks</th>\n";    
     echo "\t</tr>\n";    
     while ($line = pg_fetch_array($result, null, PGSQL_ASSOC))
     {    
        echo "\t<tr>\n";    
        foreach ($line as $col_value)
        {
           echo "\t\t<td>$col_value</td>\n";
        }
        $pick_val = "picks_".$line['id']; // based on the row id of the db table
        echo "<td><input type=\"radio\" name='$pick_val' value=\"home\">Home
        <input type=\"radio\" name='$pick_val' value=\"away\">Away</td>";
        echo "\t</tr>\n";
     }    
     echo "</table>\n";
     echo "</form>";
     // Free resultset
     pg_free_result($result);
     }
     // Closing connection
     pg_close($conn);    
?>

这将有助于您明智地选择主队或客队。