728x90

PHP 와 Query를 이용하여 테이블의 칼럼을 누루면 해당 칼럼 순으로 정렬하는 기능을 구현하고 싶을 때가 있다.

핵심 기능에 필요한 것만 발췌하여 적어둔다.

<?php
ini_set("display_startup_errors"1);
ini_set("display_errors"1);
error_reporting(E_ALL);
 
$link_url = "MemberList.php"// 현재 실행중인 파일명 가져오기
 
$sort = isset($_GET['sortby']) ? $a->XSSFilter($_GET['sortby']): '';
$bidx = isset($_GET['bidx']) ? $a->XSSFilter($_GET['bidx']) :'';
switch($bidx){
    case 1:
        $xorderby"userNM ";
        $sortby = ($sort == "userNMDESC") ? "ASC" : "DESC";
        $xorderby.= $sortby;
        break;
    case 2:
        $xorderby"userID ";
        $sortby = ($sort == "userIDDESC") ? "ASC" : "DESC";
        $xorderby.= $sortby;
        break;
    default:
        $xorderby"idx DESC";
        $sortby = "DESC";
        break;
}
?>
 
<th scope="col" class="bldSort" data-bidx="1" style="cursor:pointer">성명</th>
<th scope="col" class="bldSort" data-bidx="2" style="cursor:pointer">아이디</th>
 
<div class='form-group'>
    <form name="ListForm" class="form-inline" action="<?php echo $link_url;?>">
        <input type="hidden" name="m" value="<?php echo $m;?>" />
        <input type="hidden" name="orderby" id="xorderby" value="<?php echo $xorderby;?>" />
        <!-- 일부 코드 생략 -->
</div>
<div id="urlPath" url-path="<?php echo $link_url;?>"></div>
 
<script>
$('.bldSort').click(function(e) {
    e.preventDefault();
    var bidx = $(this).attr('data-bidx');
    var uri = $('#urlPath').attr('url-path');
    var sort = document.getElementById('xorderby').value.replace(/(\s*)/g,"");
    MemberListTable(where,keyword,curPage,uri,bidx,sort);
});
</script>

 

 

블로그 이미지

Link2Me

,