2012-07-16 109 views
0

我正在使用datatables插件,由此我正在寻找一个名为“特权”的字段。特权值为“superadmin”和“admin”。对于通配符搜索,只要我通过“管理员”进行搜索,管理员和超级管理员都会显示记录。如何解决这个问题。我的代码是这样的:datatables搜索选项

function fnFilterColumn (i) 
{ 
    $('#example').dataTable().fnFilter( 
    $("#col"+(i+1)+"_filter").val(),i,true,false); 
} 
$(document).ready(function(){ 
var oTable = $('#example').dataTable({ 
    "bProcessing": true, 
    "sAjaxSource": "datatabledb.php", 
    "bJQueryUI": true, 
    "sPaginationType": "full_numbers", 
    "sDom": 'T<"clear">lfrtip', 
    "oTableTools": { 
    "aButtons": [ 
    { 
    "sExtends": "csv", 
    "sButtonText": "Save to CSV" 
    } 
    ] 
    }, 
    "oLanguage": { 
     "sSearch": "Search all columns:" 
    }, 
    "aoColumns": [ 
     null, 
       { "bSortable": false }, // disable the sorting property for checkbox header 
     null,null,null,null,null,null,null,null 
      ] 
}); 

在datatabledb.php文件,我写下了我的DB代码,如:

<?php 
include('library/function.php'); 

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * Easy set variables 
    */ 

    /* Array of database columns which should be read and sent back to DataTables. Use a space where 
    * you want to insert a non-database field (for example a counter or static image) 
    */ 
    $aColumns = array('admin_name','admin_photo','username','email','age','location','contact_no','role','creation_date','status'); 

    /* Indexed column (used for fast and accurate table cardinality) */ 
    $sIndexColumn = "admin_id"; 

    /* DB table to use */ 
    $sTable = "admin_details"; 

    /* Database connection information */ 
    $gaSql['user']  = "root"; 
    $gaSql['password'] = ""; 
    $gaSql['db']   = "alert"; 
    $gaSql['server']  = "localhost"; 

    /* REMOVE THIS LINE (it just includes my SQL connection user/pass) */ 
    //include($_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php"); 


    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * If you just want to use the basic configuration for DataTables with PHP server-side, there is 
    * no need to edit below this line 
    */ 

    /* 
    * MySQL connection 
    */ 
    $gaSql['link'] = mysql_pconnect($gaSql['server'], $gaSql['user'], $gaSql['password'] ) or 
     die('Could not open connection to server'); 

    mysql_select_db($gaSql['db'], $gaSql['link']) or 
     die('Could not select database '. $gaSql['db']); 


    /* 
    * Paging 
    */ 
    $sLimit = ""; 

    if (isset($_GET['iDisplayStart']) && $_GET['iDisplayLength'] != '-1') 
    { 
     $sLimit = "LIMIT ".mysql_real_escape_string($_GET['iDisplayStart']).", ". 
      mysql_real_escape_string($_GET['iDisplayLength']); 
    } 


    /* 
    * Ordering 
    */ 
    $sOrder = ""; 
    if (isset($_GET['iSortCol_0'])) 
    { 
     $sOrder = "ORDER BY "; 
     for ($i=0 ; $i<intval($_GET['iSortingCols']) ; $i++) 
     { 
      if ($_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true") 
      { 
       $sOrder .= "`".$aColumns[ intval($_GET['iSortCol_'.$i]) ]."` ". 
        mysql_real_escape_string($_GET['sSortDir_'.$i]) .", "; 
      } 
     } 

     $sOrder = substr_replace($sOrder, "", -2); 
     if ($sOrder == "ORDER BY") 
     { 
      $sOrder = ""; 
     } 
    } 


    /* 
    * Filtering 
    * NOTE this does not match the built-in DataTables filtering which does it 
    * word by word on any field. It's possible to do here, but concerned about efficiency 
    * on very large tables, and MySQL's regex functionality is very limited 
    */ 
    $sWhere = ""; 
    if (isset($_GET['sSearch']) && $_GET['sSearch'] != "") 
    { 
     $sWhere = "WHERE ("; 
     for ($i=0 ; $i<count($aColumns) ; $i++) 
     { 
      $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch'])."%' OR "; 
     } 

     $sWhere = substr_replace($sWhere, "", -3); 
     $sWhere .= ')'; 

    } 


    /* Individual column filtering */ 
    for ($i=0 ; $i<count($aColumns) ; $i++) 
    { 
     if (isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '') 
     { 
      if ($sWhere == "") 
      { 
       $sWhere = "WHERE "; 
      } 
      else 
      { 
       $sWhere .= " AND "; 
      } 
      $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' "; 
     } 

    } 


    /* 
    * SQL queries 
    * Get data to display 
    */ 

    $sQuery = " 
     SELECT SQL_CALC_FOUND_ROWS `".str_replace(" , ", " ", implode("`, `", $aColumns))."` 
     FROM $sTable 
     $sWhere 
     $sOrder 
     $sLimit 
     "; 


    $rResult = mysql_query($sQuery, $gaSql['link']) or die(mysql_error()); 
    /* Data set length after filtering */ 
    $sQuery = " 
     SELECT FOUND_ROWS() 
    "; 
    $rResultFilterTotal = mysql_query($sQuery, $gaSql['link']) or die(mysql_error()); 
    $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal); 
    $iFilteredTotal = $aResultFilterTotal[0]; 

    /* Total data set length */ 
    $sQuery = " 
     SELECT COUNT(`".$sIndexColumn."`) 
     FROM $sTable 
    "; 
    $rResultTotal = mysql_query($sQuery, $gaSql['link']) or die(mysql_error()); 
    $aResultTotal = mysql_fetch_array($rResultTotal); 
    $iTotal = $aResultTotal[0]; 


    /* 
    * Output 
    */ 
    $output = array(
     "sEcho" => intval($_GET['sEcho']), 
     "iTotalRecords" => $iTotal, 
     "iTotalDisplayRecords" => $iFilteredTotal, 
     "aaData" => array() 
    ); 

    while ($aRow = mysql_fetch_array($rResult)) 
    { 
     $row = array(); 
     if($aRow['admin_name'] != ''){ 
      $row[] = wordwrap($aRow['admin_name'],15,"<br />\n",TRUE); 

     } 
     if($aRow['admin_photo'] == ''){ 
       $row[] = "<img src='http://fgtpl.com/fugenx1/public_html/alertR/admins/upload/no-pic.jpg' width='50' height='50'>"; 
     } 
     else if($aRow['admin_photo'] != ''){ 
      $row[] ="<img src='http://fgtpl.com/fugenx1/public_html/alertR/admins/upload/".$aRow['admin_photo']."' width='50' height='50'>" ; 
     } 
     if($aRow['username'] != ''){ 
      $row[] = $aRow['username']; 
     } 
     if($aRow['email'] != ''){ 
      $row[] = wordwrap($aRow['email'],15,"<br />\n",TRUE); 
     } 
     else if($aRow['email'] == ''){ 
      $row[] = "N/A"; 
     } 
     if($aRow['age'] != 0){ 
      $row[] = $aRow['age']; 
     } 
     else if($aRow['age'] == 0){ 
      $row[] = "N/A"; 
     } 
     if($aRow['location'] != ''){ 
      $row[] = wordwrap($aRow['location'],20,"<br />\n",TRUE); 
     } 
     else if($aRow['location'] == ''){ 
      $row[] = "N/A"; 
     } 
     if($aRow['contact_no'] != ''){ 
      $row[] = $aRow['contact_no']; 
     } 
     else if($aRow['contact_no'] == ''){ 
      $row[] = "N/A"; 
     } 
     if($aRow['role'] != ''){ 
      $row[] = get_role_name_by_id($aRow['role']); 
     } 
     if($aRow['creation_date'] != ''){ 
      $joiningDate = date("d-m-Y h:i:s", strtotime($aRow['creation_date'])); 
      $row[] = substr($joiningDate,0,10); 
     } 
     if($aRow['status'] != ''){ 
      $row[] = ($aRow['status'] == 1)?"Enable":"Disable"; 
     } 





     $output['aaData'][] = $row; 
    } 


    echo json_encode($output); 
?> 
+0

太多的代码的详细信息,并在其目前的形式,而不是我可以重现的东西来测试答案。甚至不知道你在哪里指定通配符。但一般的答案可能是“不要使用通配符搜索”。 (“Doc,当我这样做时会很痛苦。”) – ghoti 2012-07-16 11:13:26

+0

是的,我只是想排除特定搜索字段的通配符搜索而不是全部。请帮帮我。 – Bappa 2012-07-16 11:16:27

+0

我没有在你的PHP中看到通配符搜索发生在admin_detail表的“特权”列上......偶尔没有在“过滤”部分或代码中的其他任何地方提及。 – ghoti 2012-07-16 11:24:43

回答

1

我不知道为什么是这样解答了这么久。你只需要改变

$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch'])."%' OR "; 

$sWhere .= "`".$aColumns[$i]."` LIKE '".mysql_real_escape_string($_GET['sSearch'])."' OR "; 

删除通配符
您也可以为单个列做到这一点,让我知道如果你想对