วันพุธที่ 3 ตุลาคม พ.ศ. 2555

การเรียงข้อมูลจากฐานข้อมูล ตามคอลัมน์ที่กำหนด

       หลายคนคงรู้จักวิธีเรียงข้อมูลในฐานข้อมูล (Database) เราจะใช้คำสั่ง ORDER BY ตามด้วยชื่อฟิลด์ที่ต้องการจะเรียง และตามด้วยรูปแบบของการเรียง ASC (น้อยไปมาก) หรือ DESC (มากไปหาน้อย)

เช่น SELECT  * FROM yourTable ORDER BY yourId DESC (เรียงIDจากมากไปน้อย)

หรือ SELECT  * FROM yourTable ORDER BY yourId DESC,yourName ASC,yourAge DESC

หรือ SELECT * FROM yourTable ORDER BY yourId
แต่ไม่มี ASC หรือ DESC ต่อท้าย มันจะเรียงฟิลด์ yourId ให้เป็นแบบ ASC อัตโนมัติ

หรือหากใช้คำสั่ง
SELECT  * FROM yourTable 
เฉยๆ ไม่มีคำสั่ง ORDER BY มันจะเรียงจาก ID ที่เป็น Primary Key ให้เป็นแบบ ASC อัตโนมัติ

*คำสั่งที่ผมใช้ อ้างอิงตามคำสั่งของ MySQL นะครับ ^^

ผมขออธิบายหลักการเรียงข้อมูลคร่าวๆแค่นี้ก่อนครับ เพื่อให้เข้าใจกันตรงกัน

      แต่ที่ผมจะสอนจริงๆ ผมจะเอาคำสั่งข้างบนนี้แหละ มาประยุกต์ใช้ในการจัดเรียงข้อมูลแบบที่เรากำหนดเองครับ โดยผมใช้ฐานข้อมูล MySQL และใช้ภาษา PHP นะครับ

มาดูโค๊ดกันเลยครับ ^^
<?php
session_start();
$connect = mysql_connect('localhost', 'root', 'root');//เชื่อมต่อกับฐานข้อมูล
mysql_select_db('db_exshop2');//เลือกฐานข้อมูล
mysql_query('SET NAMES UTF8');//เซตภาษา

function sortCustom($od_array, $od_no) {//ฟังก์ชั่นเรียงข้อมูล 
// $od_array คือ ตัวแปรเก็บชื่อฟิลด์ทีต้องการเรียงโดยจะเก็บแบบ array ให้สังเกตุคำสั่ง
// $od_array = array('1' => 'pd_id', '2' => 'pd_name', '3' => 'pd_amount', '4' => 'pd_price');
// $od_no ตัวแปรเก็บค่าการเรียง ซึ่งจะมาจากลิงค์ที่เราสร้าง ให้สังเกตุ  <a href="orderProduct.php?od_no=1">
    global $sql_product;//เก็บ คำสั่งSQL

    if ($_SESSION['ss_od_no'] != $od_no) {//หากคลิกคอลัมน์อื่น ไม่ได้คลิกคอลัมน์เดิมซ้ำ ให้จัดเรียงแบบ ASC เสมอ
        $sql_product.='ORDER BY ' . $od_array[$od_no] . ' ASC ';
        $_SESSION['ss_od_no'] = $od_no;//เก็บค่าการเรียงเอาไว้ในตัวแปรแบบSession ชื่อ ss_od_no
        $_SESSION['ss_set_order'] = 1;//เก็บรูปแบบการเรียงไว้ตัวแปรแบบSession ชื่อ ss_set_order ผมให้ 1 แทน ASC ,2 แทน DESC
    } else {//คลิกคอลัมน์เดิมซ้ำอีกรอบ
        if ($_SESSION['ss_set_order'] == 1) {//หากค่าเดิมเป็น 1 หมายถึง ASC
            $sql_product.='ORDER BY ' . $od_array[$od_no] . ' DESC ';//ให้เรียงตรงกันข้ามนั่นก็คือ DESC
            $_SESSION['ss_set_order'] = 2;//เซตรูปแบบการเรียงให้เป็น 2 (DESC)
        } else {//หากค่าเดิมเป็น 2 หมายถึง DESC
            $sql_product.='ORDER BY ' . $od_array[$od_no] . ' ASC ';//ให้เรียงตรงกันข้าม คือ ASC
            $_SESSION['ss_set_order'] = 1;//เซตรูปแบบการเรียงให้เป็น 1 (ASC)
        }
    }
}

$sql_product = 'select * from tb_product where 1 ';//คำสั่ง SQL เริ่มต้น
if (!empty($_GET['od_no'])) {
    $od_no = $_GET['od_no'];
    $od_array = array('1' => 'pd_id', '2' => 'pd_name', '3' => 'pd_amount', '4' => 'pd_price');
    
    sortCustom($od_array, $od_no);//เรียกใช้ฟังก์ชั่น sortCustom
}

$rs_product = mysql_query($sql_product);//คิวรี่คำสั่ง
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>เรียงข้อมูลตามคอลัมน์ที่เรากำหนด</title>
        <style  text="text/css">
            body{
                font-family:Tahoma, Geneva, sans-serif;
                font-size:14px; 
                color:#333;
            }
            a{
                text-decoration:none;
                color:#06C;
            }
            a:hover{
                text-decoration:underline; 
            }
        </style>
    </head>

    <body>
        <table width="550" border="1" align="center" cellpadding="3" cellspacing="0" style="">
            <tr>
                <td align="center"><strong><a href="orderProduct.php?od_no=1">รหัส<img src="sort_icon.gif" width="14" height="18" border="0" /></a></strong></td>
                <td align="center"><strong><a href="orderProduct.php?od_no=2">ชื่อ<img src="sort_icon.gif" alt="" width="14" height="18" border="0" /></a></strong></td>
                <td align="center"><strong><a href="orderProduct.php?od_no=3">จำนวน<img src="sort_icon.gif" alt="" width="14" height="18" border="0" /></a></strong></td>
                <td align="center"><strong><a href="orderProduct.php?od_no=4">ราคาต่อหน่วย<img src="sort_icon.gif" alt="" width="14" height="18" border="0" /></a></strong></td>
            </tr>
            <?php while ($show_product = mysql_fetch_assoc($rs_product)) { ?>
                <tr>
                    <td><?= $show_product['pd_id'] ?></td>
                    <td><?= $show_product['pd_name'] ?></td>
                    <td><?= $show_product['pd_amount'] ?></td>
                    <td><?= $show_product['pd_price'] ?></td>
                </tr>
            <?php } ?>
        </table>
    </body>
</html>
<?php mysql_close($connect);//ปิดการเชือมต่อ ?>

ให้สังเกตุคำสั่ง
$od_array = array('1' => 'pd_id', '2' => 'pd_name', '3' => 'pd_amount', '4' => 'pd_price'); โดยกำหนดให้ตรงกับ URL ของคอลัมน์ที่ท่านจะเรียงครับ

ซึ่งจากโค๊ดตัวอย่าง
ผมกำหนด URL ชื่อ
orderProduct.php?od_no=1
orderProduct.php?od_no=2
orderProduct.php?od_no=3
orderProduct.php?od_no=4
ค่า od_no=1 จะหมายถึงฟิลด์ pd_id
ค่า od_no=2 จะหมายถึงฟิลด์ pd_name
ค่า od_no=3  จะหมายถึงฟิลด์ pd_amount
ค่า od_no=4  จะหมายถึงฟิลด์ pd_price

1 ความคิดเห็น :

  1. อยากให้ รหัส เรียง จากน้อยไปหา มากต้อง เขียนโค้ดอย่างไรบ้างค่ะ

    ตอบลบ