2013-10-20 81 views
0

我想問一下如何通過3個下拉列表單獨過濾我的結果。例如,我將通過1個下拉列表過濾它,然後當我選擇另一個下拉列表時,它將精煉結果與我選擇第三個時相同。使用AJAX,PHP和MySql進行過濾

這裏是我的代碼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(); 


?> 
+0

那麼問題在哪裏呢?你有什麼問題?這不工作嗎? – geomagas

+0

**危險**:您正在使用[一個**過時的**數據庫API](http://stackoverflow.com/q/12859942/19068),並應使用[現代替換](http:// php。淨/手動/ EN/mysqlinfo.api.choosing.php)。你也**易受[SQL注入攻擊](http://bobby-tables.com/)**,現代的API會使[防禦]更容易(http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)自己從。 – Quentin

+0

這實際上是有效的,但是您必須選擇所有3個下拉列表才能過濾結果。即使我選擇了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(); 
?> 
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).""; 

} 
+0

感謝您的幫助!爲了研究特定的代碼或編程課程,你介意給我關鍵字在Google中進行搜索嗎?儘管我在編程方面很新穎。謝謝! –

+0

PDO?看看http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers – Tom