使用AJAX,PHP和MySql进行过滤

时间:2013-10-20 17:04:45

标签: php mysql ajax

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

这是我的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();


?>

2 个答案:

答案 0 :(得分: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)."";

}

答案 1 :(得分: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();
?>