0

我有一个类,其中有一个用户搜索功能:

PHP:

private function searchUsers() {
    if (isset($_REQUEST['term'])) {
        $params = array( ':searchQ' => $_REQUEST['term'] . '%' );
        $sql = "SELECT distinct username as suggest, user_id
                FROM login_users
                WHERE username LIKE :searchQ
                OR name LIKE :searchQ
                OR user_id LIKE :searchQ
                ORDER BY username
                LIMIT 0, 5";
        $stmt = parent::query($sql, $params);
        if ( $stmt->rowCount() > 0 ) {
            while($suggest = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $data[] = array(
                    'label' => $suggest['name'] . '(' . $suggest['user_id'] . ')',
                    'value' => $suggest['user_id']
                );
            }
            echo json_encode($data);
            flush();
            exit();
        }
    }
}

在单独的页面上:

JS:

<script>
$(function () {
    jQuery.ajaxSetup({
      beforeSend: function() {
         $('#loader').show();
      },
      complete: function(){
         $('#loader').hide();
      },
      success: function() {}
    });
    $('#search-input').autocomplete({
        source:'classes/add_user.class.php',
        minLength:3,
        select: function(event, ui) {
            AutoCompleteSelectHandler(event, ui)
        }
    });
    function AutoCompleteSelectHandler(event, ui) {               
        var selectedObj = ui.item; 
        window.location.href = 'users.php?uid=' +  selectedObj.value;            
    }
});
/* Disable autocomplete */
var flag = 1;
function disAutoComplete(obj){
    if(flag){
    obj.setAttribute("autocomplete","off");
        flag = 0;
  }
    obj.focus();
}
</script>

HTML:

<form method="post" id="search-users-form" action="classes/add_user.class.php" class="pull-right">
    <div class="control-group">
      <div class="controls">
        <div class="input-prepend">
          <button id="add_new_user_btn" class="btn"><?php _e('Add new user'); ?></button>
          <input type="number" class="input-mini" min="0" id="showUsers" name="showUsers" placeholder="<?php _e('Show'); ?>" value="<?php echo !empty($_SESSION['jigowatt']['users_page_limit']) ? $_SESSION['jigowatt']['users_page_limit'] : 10; ?>">
          <span class="add-on">
            <label for="search-input"><a href="#" data-rel="tooltip-bottom" title="<?php _e('Search by Username, Name, or ID!'); ?>"><i class="icon-search"></i></a></label>
          </span>
          <input name="search-input" type="text" style="margin:0" class="span2" id="search-input" onclick="disAutoComplete(this);" placeholder="<?php _e('User search'); ?>">
          <!--
          <input class="span2" style="margin:0" id="username-search" type="text" name="searchUsers" placeholder="<?php _e('User search'); ?>">
          -->
        </div>
      </div>
    </div>
</form>

通常我会在控制器 php 页面上有 php 逻辑,然后在 isset 上获取。但是,我必须将该函数放入一个类中才能访问该parent::query函数。使用 jquery autcomplete,我如何访问此功能,和/或我必须进行哪些修改才能使该术语起作用?

4

1 回答 1

1

add_user.class.php你应该喜欢callfunction

if (isset($_REQUEST['term'])) {
   $classObj->searchUsers($_REQUEST['term']);//$classObj of your class
}


// your class
private function searchUsers($term='') {
   $params = array( ':searchQ' => $term . '%' );
   $sql = "SELECT distinct username as suggest, user_id
             FROM login_users
             WHERE username LIKE :searchQ
             OR name LIKE :searchQ
             OR user_id LIKE :searchQ
             ORDER BY username
             LIMIT 0, 5";
   $stmt = parent::query($sql, $params);
   if ( $stmt->rowCount() > 0 ) {
       while($suggest = $stmt->fetch(PDO::FETCH_ASSOC)) {
          $data[] = array(
              'label' => $suggest['name'] . '(' . $suggest['user_id'] . ')',
              'value' => $suggest['user_id']
           );
       }
       echo json_encode($data);
       flush();
       exit();
    }    
}
于 2013-08-09T16:02:01.810 回答