如何使用复选框显示php中的表格数据?

时间:2015-03-18 10:56:53

标签: php database

我的数据库中有多个主题,我的查询是通过在php中选择check boxes来获取这些主题的常用信息? 假设有四个主题'a', 'b', 'c' and 'd'我有单独的表也包含公共数据,即a intersect b, a intersect c and a intersect d表等等。

现在我尝试过的是第一个用户必须从下拉框中选择主题形式,然后使用复选框选择多个比较主题。当我检查两个主题时,它只显示最后一个主题的数据。

<form method="GET" action="abc.php" name="form">
   <font size = "4px", color = "#008C9A", align = "left"><b> Select Theme >> </b></font>    
   <select name ="table[]">
     <option value="ab">a</option>
     <option value="bc">b</option>
     <option value="cd">c</option>
   <br/>
   <font size="4px" color="#008C9A" align="centre">
    <b> Show Common Genes of Selected Theme with </b>
   </font> <br/> <br/>
   <input type="checkbox" name="search" value = "a"/> 
    <font color = "#2F4F4F"><b>a </b></font>                         
   <input type="checkbox" name="search" value = "a"/>
    <font color = "#2F4F4F"><b>b</b></font>                       
   <input type="checkbox" name="search"/ value = "c"> 
    <font color = "#2F4F4F"><b>c</b></font>                         
   <input type= "submit" value= "Search" name="sub" /> 
</form>

任何人都可以建议我应该做什么动作.php?

我正在尝试这个,但它没有给出任何结果:

    <?php
    require("config.php");

    $table=$_POST['table'];
    $search=$_POST['search'];

  echo"<table border='0', width='1200', align='center'>
<tr>
<th bgcolor = '#2F4F4F' align = center><font color = '#fff'>Theme</font></th>
<th bgcolor = '#2F4F4F' align = center><font color = '#fff'>Id</font></th>
<th bgcolor = '#2F4F4F' align = center><font color = '#fff'>Symbol</font></th>
<th bgcolor = '#2F4F4F' align = center><font color = '#fff'>Name</font></th>
<th bgcolor = '#2F4F4F' align = center><font color = '#fff'>Synonyms</font></th>
<th bgcolor = '#2F4F4F' align = center><font color = '#fff'>Chromosome</font></th>
<th bgcolor = '#2F4F4F' align = center><font color = '#fff'>New_Id</font></th>

</tr>";

        foreach($search as $key)
        {

switch($key)
            {
      case"cancer":         
   $sql = mysql_query("select * from $table where theme = 'cancer'", $con);
    $num_row=mysql_num_rows($sql);


    for($i=0;$i<$num_row;$i++)
{

$result=mysql_fetch_assoc($sql);


  echo"<tr bgcolor = '#EBDDE2'>";
  echo"<td align=center><font color = 'purple'><b>" . $result['theme'] ."</b></font></td>";

    $strName2 = $result["Locus_id"];
   echo"<td align=center><font color = 'purple'><b>" . $result['id'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['symbol'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['name'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Synonyms'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Chromosome'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['new_id'] ."</b></font></td>";
  echo"</tr>";
}
    break;          

    case"diabetes":         
   $sql = mysql_query("select * from $table where theme = 'diabetes'", $con);
    $num_row=mysql_num_rows($sql);


    for($i=0;$i<$num_row;$i++)
{

$result=mysql_fetch_assoc($sql);

echo"<tr bgcolor = '#EBDDE2'>";
  echo"<td align=center><font color = 'purple'><b>" . $result['theme'] ."</b></font></td>";

    $strName2 = $result["Locus_id"];
   echo"<td align=center><font color = 'purple'><b>" . $result['id'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['symbol'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['name'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Synonyms'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Chromosome'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['new_id'] ."</b></font></td>";
  echo"</tr>";
}
}
    break;          

            case"tubersulosis":         
   $sql = mysql_query("select * from $table where theme = 'tuberculosis'", $con);
    $num_row=mysql_num_rows($sql);


    for($i=0;$i<$num_row;$i++)
{

$result=mysql_fetch_assoc($sql);

echo"<tr bgcolor = '#EBDDE2'>";
  echo"<td align=center><font color = 'purple'><b>" . $result['theme'] ."</b></font></td>";

    $strName2 = $result["Locus_id"];
   echo"<td align=center><font color = 'purple'><b>" . $result['id'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['symbol'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['name'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Synonyms'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Chromosome'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['new_id'] ."</b></font></td>";
  echo"</tr>";
}
}
    break;
            case"hiv":          
   $sql = mysql_query("select * from $table where theme = 'HIV'", $con);
    $num_row=mysql_num_rows($sql);


    for($i=0;$i<$num_row;$i++)
{

$result=mysql_fetch_assoc($sql);

echo"<tr bgcolor = '#EBDDE2'>";
  echo"<td align=center><font color = 'purple'><b>" . $result['theme'] ."</b></font></td>";

    $strName2 = $result["Locus_id"];
   echo"<td align=center><font color = 'purple'><b>" . $result['id'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['symbol'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['name'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Synonyms'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Chromosome'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['new_id'] ."</b></font></td>";
  echo"</tr>";
}
}
    break;
        }
            }

mysql_close($con);
    ?>

2 个答案:

答案 0 :(得分:1)

根据@manoj的建议,将名称更改为search[]而不是search。并将表单方法设为POST

现在在abc.php中,您可以通过

获取复选框值
$search = $_POST['search'];//this will be an array

其余我认为你可以做到。对吗?

答案 1 :(得分:0)

您的表单代码应为:

<form method="POST" action="abc.php" name="form"> <!-- You can keep GET as well -->
   <font size="4px" color="#008C9A" align="left"><!-- no impact but extra , removed-->
    <b> Select Theme >> </b>
   </font>    
   <select name="table"> <!-- why using [] if multiple option can't be selected -->
    <option value="ab">a</option>
    <option value="bc">b</option>
    <option value="cd">c</option>
   </select> <!-- Important: missing close select tag -->
   <br/>
   <font size="4px" color="#008C9A" align="center">
    <b> Show Common Genes of Selected Theme with </b>
   </font> <br/> <br/>
   <input type="checkbox" name="search[]" value="a" /> <!-- name should use [] notation if multiple values -->
    <font color = "#2F4F4F"><b>a </b></font>                         
   <input type="checkbox" name="search[]" value="b" /> <!-- Note value change here -->
    <font color="#2F4F4F"><b>b</b></font>                       
   <input type="checkbox" name="search[]" value="c" /> <!-- extra / removed -->
    <font color="#2F4F4F"><b>c</b></font>                         
   <input type="submit" value="Search" name="sub" /> 
</form>

在PHP文件中,您可以访问值:

$search = $_POST['search']; // checkbox values
print_r($search);

$table = $_POST['table']; // select value
echo $table;