2014-02-11 16 views
1

有沒有人知道任何教程如何完善過濾後的搜索已完成的PHP/MySQL搜索?我搜索了谷歌和每一個可能的搜索引擎,但沒有。教程改進或過濾一個PHP搜索

預先感謝您

<?php 
if(isset ($_POST['searchString'])){ 

    $searchStr = $_POST['searchString']; 

    function sanitizeString($searchStr){ 
     $searchStr = htmlentities($searchStr); 
     $searchStr = strip_tags($searchStr); 
     $searchStr = htmlspecialchars($searchStr); 
     return $searchStr; 
    } 

    $searchStr = sanitizeString($searchStr); 
    $searchStr = str_replace("%", "", $searchStr); 
    require_once('Connections/conn.php'); 


$sql = "SELECT * FROM happyhours 
WHERE city LIKE :keyword OR zipcode LIKE :keyword"; 



    if (!empty($searchStr)){ 

     $stmt=$conn->prepare($sql); 
     $stmt->bindParam(':keyword',$searchStr, PDO::PARAM_STR); 

      try{ 
       $stmt->execute(); 
       } 
      catch(PDOException $ex){ 
       echo $ex->getMessage(); 
       } 
    } 
     $count = $stmt->rowCount(); 
     $each = $stmt->fetch(PDO::FETCH_ASSOC); 
} 

?> 

以下輸出結果

<?php do { ?> 

<table id="results" align="left" width="700px" border="0"> 
     <tr> 
     <td rowspan="5" width="130" id="photo"><a class="lightbox" href='<?php echo $each['imageURL']; ?>'><p><?php echo $each['name']; ?></p><img id="hh-image" src='<?php echo $each['imageURL']; ?>' width="80" height="80" /></a></td> 

     <tr><td id="hh-name" style="word-wrap:break-word; font-size:16px; font-family:'Myriad Pro'; font-weight:500" width="560" height="20"><?php echo $each['name']; ?></td></tr> 

     <tr><td> <a href='<?php echo $each['googleMap']; ?>' target="new" style="font-size:14px"><?php echo $each['address']; ?></a></td> 
     </tr> 
     <tr> 
     <td style="word-wrap:break-word; font-size:14px; font-family:'Myriad Pro'" height="20"><?php echo $each['phone']; ?></td> 
     </tr> 
     <tr> 
     <td style="word-wrap:break-word; font-size:14px; font-family:'Myriad Pro'" height="20"><?php echo $each['dayOfTheWeek']; ?>&nbsp;&nbsp;(<?php echo $each['hours']; ?>)</td> 
     </tr> 
    </table> 
    <br/> 


<?php } while($each = $stmt->fetch(PDO::FETCH_ASSOC)); ?> 



<?php } ?> 
+0

是您是否要求進一步篩選已經完成的* query *結果? – useSticks

+0

http://datatables.net/ – Mattt

+0

你是否正在使用一個[開發框架](http://codegeekz.com/best-php-frameworks-for-developers/)插件呢?也許你應該是? – tadman

回答

0

This blog,提供有關過濾從mysqli的回報概要的部分。有關更多詳細信息,我建議檢查MYSQLiPDO文檔。

(當然,我真的建議被檢查出的許多框架的一個PHP - !我看到tadman已經在發表了一個鏈接到他們的一個很好的列表)