0

我想问一下如何通过 3 个下拉列表独立过滤我的结果。例如,我将按 1 个下拉列表对其进行过滤,然后当我选择另一个下拉列表时,它会细化与我选择第 3 个下拉列表时相同的结果。

这是我的filtering.php代码:

<?php
$q=$_GET['q'];
$a=$_GET['a'];
$b=$_GET['b'];

$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error($con));
}

mysql_select_db("ooh", $con);
$strSQL="SELECT * FROM files WHERE type = '".$q."' AND price = '".$a."' AND location = '".$b."'";
$rs = mysql_query($strSQL,$con);

while($info = mysql_fetch_array($rs)) {

Print "<div id='filtername' class='fluid'>"; 
Print "<img src='images/ad_mock4.jpg'  alt=''/>";
Print "<div class='box'>"; 
Print "<h2>".$info['title']. "<h2>"; 
Print "<p>".$info['shortdescription']. "</p>"; 
Print "<p class='cat'><strong>Price:</strong>".$info['price'] . "</p>";
Print "<p class='cat'><strong>Duration:</strong>".$info['duration'] . "</p>"; 
Print "<p class='cat'><strong>Material:</strong>".$info['material'] . "</p>";
Print "<p class='cat'><strong>Type:</strong>".$info['type'] . "</p>"; 
Print "<p class='cat'><strong>Location:</strong>".$info['location'] . "</p>";
Print "<p class='cat'><strong>Size:</strong>".$info['size'] . "</p>";
Print "</div>";
</div>
Print "<div align='center'><a href='landingpage.php?id=".$info['id']."' class='cssbutton2'>VIEW ITEM</a></div>"; 
}
mysql_close();


?>
4

2 回答 2

0

但是,您应该继续使用 PDO 进行数据库查询 - 这里是关于如何解决您的目标的提示

$sql = array();

foreach($_GET as $k => $v){

  if(get_magic_quotes_gpc()){
    $v = stripslashes($v);
  } 

  $v = addslashes(htmlspecialchars($v, ENT_QUOTES)); // or whatever

  switch($k){

    case 'q':

    $sql[] = "type = '".$v."'";

    break;
    case 'a':

    $sql[] = "price = '".$v."'";

    break;  
    case 'b':

    $sql[] = "location = '".$v."'";

    break;          
    default:

  }

}

// query part
if(!empty($sql)){

  $strSQL = "SELECT * FROM files WHERE ".implode(' AND ', $sql)."";

}
于 2013-10-20T17:30:05.070 回答
0

我在这里发布了我的问题的答案,我想通了。我希望这对面临同样问题的其他人有所帮助。顺便感谢写评论和回答我问题的人!

<?php
$q=$_GET['q'];
$a=$_GET['a'];
$b=$_GET['b'];

$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error($con));
}

mysql_select_db("ooh", $con);

if($q && $a && $b!='')
{
$strSQL = "SELECT * FROM files WHERE type = '".$q."' AND price = '".$a."' AND location = '".$b."' ORDER BY id DESC";
}

else if($q && $b && $a!='')
{
$strSQL = "SELECT * FROM files WHERE type = '".$q."' AND location = '".$b."' AND price = '".$a."' ORDER BY id DESC";
}

else if($a && $q && $b!='')
{
$strSQL = "SELECT * FROM files WHERE price = '".$a."' AND type = '".$q."' AND location = '".$b."' ORDER BY id DESC";
}

else if($a && $b && $q!='')
{
$strSQL = "SELECT * FROM files WHERE price = '".$a."' AND location = '".$b."' AND type = '".$q."' ORDER BY id DESC";
}

else if($b && $q && $a!='')
{
$strSQL = "SELECT * FROM files WHERE location = '".$b."' AND type = '".$q."' AND price = '".$a."' ORDER BY id DESC";
}

else if($b && $a && $q!='')
{
$strSQL = "SELECT * FROM files WHERE location = '".$b."' AND price = '".$a."' AND type = '".$q."' ORDER BY id DESC";
}

else if($q && $a!='')
{
$strSQL = "SELECT * FROM files WHERE type = '".$q."' AND price = '".$a."' ORDER BY id DESC";
}

else if($q && $b!='')
{
$strSQL = "SELECT * FROM files WHERE type = '".$q."' AND location = '".$b."' ORDER BY id DESC";
}

else if($a && $q!='')
{
$strSQL = "SELECT * FROM files WHERE price = '".$a."' AND type = '".$q."' ORDER BY id DESC";
}

else if($a && $b!='')
{
$strSQL = "SELECT * FROM files WHERE price = '".$a."' AND location = '".$b."' ORDER BY id DESC";
}

else if($b && $q!='')
{
$strSQL = "SELECT * FROM files WHERE location = '".$b."' AND type = '".$q."' ORDER BY id DESC";
}

else if($b && $a!='')
{
$strSQL = "SELECT * FROM files WHERE location = '".$b."' AND price = '".$a."' ORDER BY id DESC";
}

else if($q!='')
{
$strSQL = "SELECT * FROM files WHERE type = '".$q."' ORDER BY id DESC";
}

else if($a!='')
{
$strSQL = "SELECT * FROM files WHERE price = '".$a."' ORDER BY id DESC";
}

else if($b!='')
{
$strSQL= "SELECT * FROM files WHERE location = '".$b."' ORDER BY id DESC";
}

else
{
$strSQL =  "SELECT * from files ORDER BY id DESC";
}

$rs = mysql_query($strSQL,$con);

while($info = mysql_fetch_array($rs)) {
Print "<div id='filtername' class='fluid'>"; 
Print "<img src='images/".$info['file']."' />";
Print "<div class='box'>"; 
Print "<h2>".$info['title']. "<h2>"; 
Print "<p>".$info['shortdescription']. "</p>"; 
Print "<p class='cat'><strong>Price:</strong>".$info['price'] . "</p>";
Print "<p class='cat'><strong>Duration:</strong>".$info['duration'] . "</p>"; 
Print "<p class='cat'><strong>Material:</strong>".$info['material'] . "</p>";
Print "<p class='cat'><strong>Type:</strong>".$info['type'] . "</p>"; 
Print "<p class='cat'><strong>Location:</strong>".$info['location'] . "</p>";
Print "<p class='cat'><strong>Size:</strong>".$info['size'] . "</p>";
Print "</div>";
Print "<div align='center' id='button' class='cssbutton2'><a href='landingpage.php?id=".$info['id']."'>VIEW ITEM</a></div>"; 
}

mysql_close();
?>
于 2013-10-23T04:55:35.110 回答