2011-02-15 60 views
2

我對此很陌生,搜索了一段時間後,我想我會問。使用MySQL,PHP,jQuery和Ajax創建搜索功能時遇到困難

我創建了一個非常基本的搜索函數,它通過Ajax/jQuery調用MySQL數據庫中涵蓋的主題的標題名稱。

我想現在創建一個鏈接,以便如果你想了解更多關於這個主題,那麼你可以點擊並轉到詳細的MySQL數據庫記錄有更多的信息。

現在我的search.php有以下幾點:

if (mysql_num_rows($result) > 0){ 
     while($row = mysql_fetch_object($result)){ 
     $string .= "<b>".$row->title."</b>"; 
     $string .= "<br/>\n"; 

該數據庫具有以下字段:標題,subtitle01,subtext01,subtitle02,subtext02和POST_ID

我想補充另一個字符串它會識別標題所基於的post_id,然後在新的php頁面上創建一個鏈接來查看詳細信息(這將顯示所有字段)。有關如何做到這一點的任何想法?

+1

您是否考慮過轉向MVC?您正在討論基本上創建數據庫查詢的動態視圖(即視圖視圖)。數據,業務邏輯和表示的分離可以極大地幫助保持/使您的代碼可讀和可維護。 http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller – 2011-02-15 00:11:42

+0

試試本教程http://webdesignergeeks.com/coding/ajax-js/create-ajax-search-using -php-mysql-and-jquery/ – 2011-11-22 12:53:18

回答

2

This?

if(mysql_num_rows($result) > 0){ 
    while($row = mysql_fetch_object($result)){ 
     $string .= "<a href='detail.php?id=".$row->post_ID."'>".$row->title."</a><br />\n"; 
    } 
} 

編輯:固定錯字

0

如果你真的想創建一個jQuery的Ajax和PHP的搜索功能,mysql.I已經在我的網站上創建了這個腳本,這裏的JavaScript代碼示例用於搜索功能。

$(document).ready(function(){ 

    // initilize varibales 
    var htmlData=''; 

    var ajaxUrl='search.php'; 
    $("#searchText").keyup(function(){ 
     $searchText=$(this).val(); 
     if($searchText.length>=1){ 
      get_search_data($searchText,ajaxUrl,htmlData); 
     }else{ 
      $(".search_result").html('').css("padding","0px").fadeOut(); 
     } 
    }); 
}); 

function get_search_data($searchText,ajaxUrl,htmlData){ 
    $.ajax({ 
     url:ajaxUrl, 
     type:"post", 
     dataType:"json", 
     data:{'action':'get_result','search_text':$searchText}, 
     success:function(response){ 
      console.log(response); 
      for(var i=0; i<response.totalResult; i++){ 
       htmlData+='<p>'+response.allData[i].email+'</p>'; 
       $(".search_result").html(htmlData).css("padding","10px").fadeIn(); 
      } 
     } 
    }); 
    // return htmlData; 
} 

如果你想下載或學習完整的源代碼,那麼你可以點擊鏈接。

Search With Jquery-Ajax, Php and Mysql