使用复选框缩小重复区域中的记录范围



让我先说一下,我有点不熟悉php和Mysql。

我正在制作一个包含多个圣日房屋的网站,这些房屋存储在数据库中,我在可重复区域(这是一个包含)的页面上显示它们,结果也被分页。

(目前只有五个条目要测试,但当我准备好时会有几百个)

我试图实现的是访问者可以使用一组复选框(也是一个包含)缩小显示的结果范围,这些复选框代表状态、游泳池、WiFi 等可变选项。

选中这些框的结果应缩小可重复区域中的结果量。

我一直在寻找一天,我真的很卡住!有人可以看看我的代码并向我展示正确的方向或提示我实现我想要实现的目标吗?

结果页上的代码:

<?php require_once('Connections/vakantiewoningentest.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
$currentPage = $_SERVER["PHP_SELF"];
$maxRows_Recordset1 = 4;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;
mysql_select_db($database_vakantiewoningentest, $vakantiewoningentest);
$query_Recordset1 = "SELECT * FROM vw_form_3 ORDER BY provincie ASC";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $vakantiewoningentest) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysql_query($query_Recordset1);
  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;
$queryString_Recordset1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Recordset1") == false && 
        stristr($param, "totalRows_Recordset1") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);
?>
<?php
  if (isset($_POST['submit']))
  {
       if (isset($_POST['interested']))
       {
           $strInterested = implode (",", $_post['interested']);
       }
       else   
       {
           $strInterested = "";
       }
       echo "intersted in: " . $strInterested;
       exit ();   
  }
?>

复选框组代码:

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><table width="140" border="0" cellspacing="0" cellpadding="1" class="td-filter">
  <tr>
    <td colspan="2" align="center" height="15"><h1>Filter resultaten:</h1></td>
  </tr>
  <tr>
    <td colspan="2" align="center" height="15"><hr /></td>
  </tr>
  <tr>
    <th colspan="2">Per provincie:</th>
  </tr>
  <tr>
    <td width="103" class="">Ancona</td>
    <td width="33"><input name="interested[]" type="checkbox" id="interested[]" value="Ancona">
      <label for="interested[]"></label></td>
  </tr>
  <tr>
    <td>Ascoli Piceno</td>
    <td><input name="interested[]" type="checkbox" id="interested[]" value="Ascoli Piceno">
      <label for="interested[]"></label></td>
  </tr>
  <tr>
    <td>Fermo</td>
    <td><input name="interested[]" type="checkbox" id="interested[]" value="Fermo">
      <label for="interested[]"></label></td>
  </tr>
  <tr>
    <td>Macerata</td>
    <td><input name="interested[]" type="checkbox" id="interested[]" value="Macerata">
      <label for="interested[]"></label></td>
  </tr>
  <tr>
    <td>Pesaro Urbino</td>
    <td><input name="interested[]" type="checkbox" id="interested[]" value="Pesaro Urbino">
      <label for="interested[]"></label></td>
  </tr>
  <tr>
    <th colspan="2" height="15"><hr /></th>
  </tr>
  <tr>
    <th colspan="2">Op accomodatie:</th>
  </tr>
  <tr>
    <td>Agriturismo</td>
    <td><input name="interested[]" type="checkbox" id="interested[]" value="agriturismo">
      <label for="interested[]"></label></td>
  </tr>
  <tr>
    <td>Vakantiewoning</td>
    <td><input name="interested[]" type="checkbox" id="interested[]" value="Vakantiewoning">
      <label for="interested[]"></label></td>
  </tr>
  <tr>
    <td>Appartement</td>
    <td><input name="interested[]" type="checkbox" id="interested[]" value="Appartement">
      <label for="interested[]"></label></td>
  </tr>
  <tr>
    <td>B&amp;B</td>
    <td><input name="interested[]" type="checkbox" id="interested[]" value="B and B">
      <label for="interested[]"></label></td>
  </tr>
  <tr>
    <td>Camping</td>
    <td><input name="interested[]" type="checkbox" id="interested[]" value="Camping">
      <label for="interested[]"></label></td>
  </tr>
  <tr>
    <th colspan="2" height="15"><hr /></th>
  </tr>
  <tr>
    <th colspan="2">Opties:</th>
  </tr>
  <tr>
    <td>Zwembad</td>
    <td><input name="interested[]" type="checkbox" id="interested[]" value="Zwembad">
      <label for="interested[]"></label></td>
  </tr>
  <tr>
    <td>Wifi</td>
    <td><input name="interested[]" type="checkbox" id="interested[]" value="Wifi">
      <label for="interested[]"></label></td>
  </tr>
  <tr>
    <td colspan="2" height="15"><hr /></td>
  </tr>
  <tr>
    <td colspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input name="submit" type="submit" value="zoek!"></td>
  </tr>
  <tr>
    <td colspan="2" align="center">&nbsp;</td>
  </tr>
  </table>
</form>

我哪里出错了??如果您想了解更多信息,请告诉我。如果有人能帮助我,我将不胜感激!

冒着看起来很愚蠢的风险,我只想指出,您的查询中没有WHERE语句,我没有看到任何类型的 JavaScript 在客户端更改结果。

如果你被卡住了,我要做的第一件事就是修复肖恩在上面的评论中提到的重复ID。但最重要的是你需要一个WHERE语句,它采用复选框的已发布 HTML 数组(仍然命名interested[]只是 ID 不同),并对变量进行WHERE INimplode

希望有帮助。

要完成您需要的内容,您需要构造一个动态查询。我将使用 PDO,因为它更容易构造并且不推荐使用mysql_函数。您可以在此维基中了解有关PDO的更多信息

以下代码使用您的某些窗体。注意 我已经按照lolka_bolka所述删除了 id,因为这些必须是唯一的。

$clause = " WHERE ";//Initial clause
$sql="SELECT * FROM vw_form_3  ";//Query stub
$params = array();//Array to hold parameters
if(isset($_POST['submit'])){
    if(isset($_POST['provincie'])){
        $in ="";
        foreach($_POST['provincie'] as $c){
           if(!empty($c)){
                $in  .= "?,";//Add ? placeholder
                array_push($params,$c);//Add parameter to array
                }
            }
            $in = rtrim($in, ",");
            $sql .= $clause." `provincie` IN(";
            $sql .=$in.")";
            $clause = " AND ";//Change  to AND after 1st WHERE
    }
    if(isset($_POST['accomodatie'])){
        $in ="";
        foreach($_POST['accomodatie'] as $c){
            if(!empty($c)){
                $in  .= "?,";
                array_push($params,$c);
                }
            }
            $in = rtrim($in, ",");
            $sql .= $clause." `accomodatie` IN(";
            $sql .=$in.")";
            $clause = " AND ";//Change  to AND after 1st WHERE
    }

接下来的 3 行用于测试,可以删除。

echo $sql;
echo "<br>";
print_r($params);

现在可以使用"惰性"绑定(在 execute() 中使用数组)执行查询

// Execute the query
$stmt = $db->prepare($sql);
$result = $stmt->execute($params);
}
?>
<form name="form1" method="POST" action="#"><table width="140" border="0" cellspacing="0" cellpadding="1" class="td-filter">
  <tr>
    <td colspan="2" align="center" height="15"><h1>Filter resultaten:</h1></td>
  </tr>
  <tr>
    <td colspan="2" align="center" height="15"><hr /></td>
  </tr>
  <tr>
    <th colspan="2">Per provincie:</th>
  </tr>
  <tr>
    <td width="103" class="">Ancona</td>
    <td width="33"><input name="provincie[]" type="checkbox" value="Ancona">
      <label for="provincie[]"></label></td>
  </tr>
  <tr>
    <td>Ascoli Piceno</td>
    <td><input name="provincie[]" type="checkbox"  value="Ascoli Piceno">
      <label for="provincie[]"></label></td>
  </tr>
  <tr>
    <td>Fermo</td>
    <td><input name="provincie[]" type="checkbox"  value="Fermo">
      <label for="provincie[]"></label></td>
  </tr>
  <tr>
    <td>Macerata</td>
    <td><input name="provincie[]" type="checkbox"  value="Macerata">
      <label for="provincie[]"></label></td>
  </tr>
  <tr>
    <td>Pesaro Urbino</td>
    <td><input name="provincie[]" type="checkbox"  value="Pesaro Urbino">
      <label for="provincie[]"></label></td>
  </tr>
  <tr>
    <th colspan="2" height="15"><hr /></th>
  </tr>
  <tr>
    <th colspan="2">Op accomodatie:</th>
  </tr>
  <tr>
    <td>Agriturismo</td>
    <td><input name="accomodatie[]" type="checkbox"  value="agriturismo">
      <label for="accomodatie[]"></label></td>
  </tr>
  <tr>
    <td>Vakantiewoning</td>
    <td><input name="accomodatie[]" type="checkbox"  value="Vakantiewoning">
      <label for="accomodatie[]"></label></td>
  </tr>
  <tr>
    <td>Appartement</td>
    <td><input name="accomodatie[]" type="checkbox"  value="Appartement">
      <label for="accomodatie[]"></label></td>
  </tr>
  <tr>
    <td>B&amp;B</td>
    <td><input name="accomodatie[]" type="checkbox"  value="B and B">
      <label for="accomodatie[]"></label></td>
  </tr>
  <tr>
    <td>Camping</td>
    <td><input name="accomodatie[]" type="checkbox"  value="Camping">
      <label for="accomodatie[]"></label></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input name="submit" type="submit" value="zoek!"></td>
  </tr>
  <tr>
    <td colspan="2" align="center">&nbsp;</td>
  </tr>
  </table>
</form>

典型结果

SELECT * FROM vw_form_3 WHERE `provincie` IN(?,?) AND `accomodatie` IN(?,?)
array(4) { [0]=> string(6) "Ancona" [1]=> string(13) "Ascoli Piceno" [2]=> string(11) "agriturismo" [3]=> string(14) "Vakantiewoning" } 

最新更新