寻找我的数组以表格格式显示结果

时间:2013-11-21 16:47:18

标签: php html mysql sql

我希望能够点击一个我的复选框,然后从我的数据库中的表中返回一个结果。我想我的一切都是正确的,除了我的查询以提取数据并以表格格式显示结果。

This is my index.php file that makes the checkboxes.

<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Search</title>
    </head>
    <body>
    <form action="filter.php" method="post">
    <INPUT TYPE=CHECKBOX NAME="option[]" VALUE="PhoneName" id="r1">PhoneName
    <INPUT TYPE=CHECKBOX NAME="option[]" VALUE="8GB" id="b1">8GB 
    <INPUT TYPE=CHECKBOX NAME="option[]" VALUE="16GB" id="g1">16GB
    <INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Android" id="g1">Android
    <INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Apple" id="g1">Apple
    <input type="submit" name="formSubmit" value="Filter" />

    </form>

    </body>
    </html>

这是我的filter.php,其中PHP代码开始

    <?php

         mysql_connect("localhost", "root", "") or die (mysql_error());
        mysql_select_db("James") or die (mysql_error());
        $sql = 'SELECT * FROM hardware';
    $query=array();
    if (isset($_POST['option'])){
       foreach ($_POST['option'] as $opt){
          switch ($opt){
             case 'PhoneName':  $query[] = "PhoneName='yes'";
             case '8GB': $query[] = "8GB='yes'"; 
             case '16GB': $query[] = "16GB='yes'"; 
             case 'Android': $query[] = "Android='yes'"; 
             case 'Apple': $query[] = "Apple='yes'";
    }

          }
        }

    if (count($query)>0){
      $sql .= 'WHERE '.implode(' IN ', $query);
      print_r($_POST['option']);
    }

任何帮助都会感激不尽,因为我花了好几天制作各种不同的代码,但这是我现在可以得到的。我几天前才开始学习php,所以我意识到它可能不太好

2 个答案:

答案 0 :(得分:0)

在切换案例之后

使用break也会将implode(' IN ', $query)替换为implode(' OR ', $query)implode(' AND ', $query)

$sql = 'SELECT * FROM hardware';
    $query=array();
    if (isset($_POST['option'])){
       foreach ($_POST['option'] as $opt){
          switch ($opt){
             case 'PhoneName':  $query[] = "PhoneName='yes'";
              break;
             case '8GB': $query[] = "8GB='yes'"; 
              break;
             case '16GB': $query[] = "16GB='yes'"; 
              break;
             case 'Android': $query[] = "Android='yes'"; 
              break;
             case 'Apple': $query[] = "Apple='yes'";
         }

      }
  }

    if (count($query)>0){
      $sql .= ' WHERE '.implode(' OR ', $query);
    }

    echo $sql;

请参阅phpfiddle

答案 1 :(得分:0)

mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db("James") or die (mysql_error());
$sql = 'SELECT * FROM hardware';
$query=array();
if (isset($_POST['option'])){
foreach ($_POST['option'] as $opt){
    if($opt=='PhoneName')
    $query[] = "PhoneName='yes'";        
    if($opt=='8GB')
    $query[] = "8GB='yes'"; 
    if($opt=='16GB')
    $query[] = "16GB='yes'"; 
    if($opt=='Android')
    $query[] = "Android='yes'"; 
    if($opt=='Apple')
    $query[] = "Apple='yes'";


    }
}

if (count($query)>0){
$sql .= ' WHERE '.implode(' AND ', $query);
print_r($_POST['option']);
echo $sql;
}