为查询结果的每一行都设置单选按钮

时间:2014-12-06 20:01:57

标签: php html button radio-button

我遇到为每个查询结果创建单选按钮的问题。对于返回的每一行,我想要一个由2个单选按钮组成的额外列。用户应该只能选择这两个按钮中的一个。现在我有单选按钮列,但用户只能选择每个单选按钮1,而不是每个查询结果1。我希望这是有道理的。

我应该使用单选按钮吗?或者我应该使用复选框?

<!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>

2 个答案:

答案 0 :(得分:0)

所有单选按钮上的输入名称都是相同的,因此Web浏览器假设它们都回答相同的问题(主页或Away表示所有记录),而不是一组问题(Home或Away for EACH记录)。您需要区分每行的每对单选按钮的名称。

$row=0;
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>"
        ."<label>Home <input type='radio' name='picks[$row]' value='home'></label>"
        ."<label>Away <input type='radio' name='picks[$row]' value='away'></label>"
        ."</td>";

    echo "\t</tr>\n";
    $row++;
}

在服务器端,它会将提交解释为数组。上面的$ row变量可以是每行的唯一索引或ID,而不仅仅是我演示的计数器。

答案 1 :(得分:0)

无法同时选择具有相同名称的单选按钮。每个查询的单选按钮应具有唯一的名称,但同一查询的两个按钮的名称应具有相同的名称。

代码应修改为: -

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