Jquery 자동완성 검색기능 추가하기 (Autocomplete) 많이 검색한 순서대로 정렬

Jquery 자동완성 검색기능 추가하기 (Autocomplete) 많이 검색한 순서대로 정렬

QA

Jquery 자동완성 검색기능 추가하기 (Autocomplete) 많이 검색한 순서대로 정렬

답변 1

본문

아래가 소스인데요.

        order by
            pp_word asc limit 10 ";

이 부분 같습니다.

검색이 많은 순서대로 정렬하려면 어떻게 해야 하죠?

예를 들어 배트맨을 검색하고 자동완성은 배트맨로빈 검색이 많다면 배트맨로빈을 가장 위에 뛰우는 겁니다. 가능할까요?

 

<script>
$(document).ready(function() {

    // 오토컴플리트 (검색어 자동완성)
    $("#thema_wrapper").on("keyup", "#stx", function(){
        var stx = $(this).val(); /* 입력한 검색어 */
        $(this).autocomplete({
            source:function(request, response) {
                $.getJSON(g5_url+"/_search_popular.php", {
                    /* _search_popular.php 파일로 넘길 변수값을 이곳에 작성하시면 됩니다. GET 으로 넘어갑니다. */
                    /* 콤마로 구분하시면 되요 ex) sfl:"wr_subject", stx:stx, ........ */
                    stx : stx
                }, response);
            },
            minLength:2, /*최소 검색 글자수*/
            delay: 150,  /* 검색어 입력후 표시되는 시간 - 숫자가 클수록 느리게 출력 */
            focus:function(event, ui) {
                /* 검색을 통하여 넘어온 값을 여기서 처리 */
                console.log(ui.item.value); /* 콘솔 확인용이므로 삭제하거나 주석처리 하여도 됩니다. */
            },
            close:function(event, ui) {

            }
        })
    });
    // 오토컴플리트 종료

});
</script>

 

<?php
include_once('./_common.php');
$stx = $_GET['stx'];
$sql = "select
            distinct(pp_word)
        from
            g5_popular
        where
            pp_word like '%".$stx."%'
        group by
            pp_word
        order by
            pp_word asc limit 10 ";
$result = sql_query($sql,true);
// value : 검색된 단어이며, input 값으로 넘어갑니다.
// label : 특정검색어를 입력시 자동완성 리스트로 표시됩니다. (다양하게 응용가능)
while($row=sql_fetch_array($result)) {
    $arr[] = array(
                "value"    => $row['pp_word'],
                "label"    => $row['pp_word']
                );
}
echo json_encode($arr);
?>

이 질문에 댓글 쓰기 :

답변 1

select 문에 count 로 잡으시고  order by 에서 정렬하시면 됩니다 

 

$sql = "select
            pp_word, count(*) as cnt 
        from
            g5_popular
        where
            pp_word like '%".$stx."%'
        group by
            pp_word
        order by
            cnt desc  limit 10 ";

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 38
© SIRSOFT
현재 페이지 제일 처음으로