채택완료

쿼리문을 클릭해서 sort 시킬수 있나요?

Copy
<table>
                    <colgroup>
                        <col width="100px">
                        <col width="*">
                    </colgroup>
                    <thead>
                    <tr>
                        <th>이름</th>
                        <th>구매수</th>
                    </tr>
                    </thead>
                <tbody>

<?php

                $sql = "

                    select a.mb_id, b.mb_name, group_concat(wr_12 separator '||') wr_12, count(*) as cnt , sum(a.wr_31) as tot
                      from {$write_table} a
                               left outer join {$g5['member_table']} b on a.mb_id = b.mb_id
                     where wr_29 between '{$stx1}' and '{$stx2}'

                     ORDER BY tot DESC";

 

                $result = sql_query($sql);
                while ($row = sql_fetch_array($result)) {

?>

 

<tr>

..

..

라고 했을때

이름이나 구매수를 클릭해서 ORDER BY 를 변경할 수 있는지 궁금합니다.

|

답변 1개 / 댓글 1개

채택된 답변
+20 포인트

a태그가 있다고 가정을 하겠습니다.

a태그안에 이름, 구매수의 변수를 넘기고

위에 쿼리문에서 해당 변수를 대입하면됩니다.

예로

<a herf="/test.php?order_by=name">이름</a>

<a herf="/test.php?order_by=order">구매수</a>

Copy
<?php
// 클릭한 변수가 없다면 기본 tot로 정렬
if (!$order_by) {
    $order_by = 'tot';
}

$sql = "
    select a.mb_id, b.mb_name, group_concat(wr_12 separator '||') wr_12, count(*) as cnt , sum(a.wr_31) as tot
      from {$write_table} a
               left outer join {$g5['member_table']} b on a.mb_id = b.mb_id
     where wr_29 between '{$stx1}' and '{$stx2}'
     ORDER BY {$order_by} DESC";

$result = sql_query($sql);
while ($row = sql_fetch_array($result)) {

}
?>

완벽한 코드는 아니니 기본 예로 이해만 하시기 바랍니다.

답변에 대한 댓글 1개

감사합니다.

답변을 작성하려면 로그인이 필요합니다.