จัดเรียงใน MySQL มันจะทำให้คิวรี่ช้า มาจัดเรียงภาษาไทยด้วย PHP กันดีกว่า
การจัดเรียงปกติใน MySQL เราจะใช้คำสั่ง SQL ที่สั่งให้จัดเรียงคือ ORDER BY
แต่ภาษาไทยจะมีปัญหากับสระที่นำหน้าพยัญชนะไทย เช่น เ, ไ, โ ฯลฯ
ดังนั้นเวลาจัดเรียงก็ต้องเรียกฟังก์ชั่น CONVERT เสียก่อน
ORDER BY CONVERT( `name` USING tis620 )
แต่ถ้าเราอยากได้ข้อมูลจาก MySQL เร็วๆ ก็ตัด ORDER BY ออกไปเลย จะทำให้คิวรี่ได้เร็วขึ้น แล้วก็นำมาจัดเรียง Array ใน PHP แทนดังโค้ดต่อไปนี้
[PHP Code]
<?php
function th_strcoll($stringA, $stringB)
{
$regex = '(^[เแโใไ]*)((.)(.*))';
$matchesA = $matchesB = null;
mb_ereg($regex, $stringA, $matchesA);
mb_ereg($regex, $stringB, $matchesB);
if ($matchesA[1] !== $matchesB[1] && $matchesA[3] === $matchesB[3]) {
if ($matchesA[1] === false) {
return -1;
} else if ($matchesB[1] === false) {
return 1;
} else {
return strcoll($matchesA[1], $matchesB[1]);
}
}
return strcoll($matchesA[2], $matchesB[2]);
}
$a = ['โค','เคย','ไก่','การบ้าน','ข้าว', 'กัน','ไข่','กิ่ง'];
usort($a, "th_strcoll");
print_r($a);
?>
Array ( [0] => กัน [1] => การบ้าน [2] => กิ่ง [3] => ไก่ [4] => ข้าว [5] => ไข่ [6] => เคย [7] => โค )
เพียงเท่านี้เราก็สามารถช่วยลดภาระของ MySQL ได้แล้ว (ประหยัดทั้ง Memory และ CPU) ถ้ายังไงก็ลองเอาไปใช้กันดูนะครับ เผื่อจะช่วยให้การทำงานเร็วขึ้นได้
หลังจากที่สามารถเรียงอาร์เรย์ได้แล้ว ผู้เขียนได้ลองค้นหาเทคนิคการเรียงคำภาษาไทย โดยเปรียบเทียบข้อมูล Array เป็นข้อมูลที่ดึงมาจาก PHP แบบ JSON โดยผลลัพธ์ที่ได้ดังนี้
[ JavaScript Code ]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>เรียงภาษาไทยโดย JavaScript</title>
</head>
<body>
<h1>เรียงภาษาไทยโดย JavaScript</h1>
<p id="demo">Some text...</p>
<table border="1" >
<thead>
<tr>
<td>คำที่ต้องการตรวจสอบ</td><td>คำแรก มากกว่า</td><td>คำที่ 2 มากกว่า</td><td>เทียบคำเต็ม</td>
</tr>
</thead>
<tbody id="test">
</tbody>
</table>
<script>
document.getElementById("demo").innerHTML = '';
var data = [
{"name": "ขวด","val": 5},
{"name": "คา","val": 15},
{"name": "ก๋ง","val": 1},
{"name": "ไก่","val": 5},
{"name": "แห่","val": 12},
{"name": "จน","val": 9},
{"name": "กิ่ง","val": 5},
{"name": "เคย","val": 2},
{"name": "ไข่","val": 3},
{"name": "จริง","val": 9},
{"name": "การบ้าน","val": 7},
{"name": "กัน","val": 15},
{"name": "โค","val": 13},
{"name": "กก","val": 16},
{"name": "แห","val": 12},
{"name": "ข้าว","val": 12},
{"name": "จะ","val": 18}
];
data.sort(function(a,b) {
str1 = a.name.replace(/่/gi, "");
str1 = str1.replace(/้/gi, "");
str1 = str1.replace(/๊/gi, "");
str1 = str1.replace(/๋/gi, "");
str1 = str1.replace(/็/gi, "");
str2 = b.name.replace(/่/gi, "");
str2 = str2.replace(/้/gi, "");
str2 = str2.replace(/๊/gi, "");
str2 = str2.replace(/๋/gi, "");
str2 = str2.replace(/็/gi, "");
text1 = str1.substring(0, 1);
text2 = str2.substring(0, 1);
if(text1 == 'เ' || text1 == 'แ' || text1 == 'ไ' || text1 == 'ใ' || text1 == 'โ'){
//text1 = a.name.substring(1,2) + text1 + a.name.substring(2);
text1 = str1.substring(1,2) + str1;
}else{
text1 = str1;
}
if(text2 == 'เ' || text2 == 'แ' || text2 == 'ไ' || text2 == 'ใ' || text2 == 'โ'){
//text2 = b.name.substring(1,2) + text2 + b.name.substring(2);
text2 = str2.substring(1,2) + str2;
}else{
text2 = str2;
}
//alert();
test = document.getElementById("test").innerHTML;
test += '<tr><td>' + a.name + ' , ' + b.name + '</td>';
test += '<td>' + text1 + ' > ' + text2 + ' :: ' + (text1 > text2) + '</td>';
test += '<td>' + text1 + ' < ' + text2 + ' :: ' + (text1 < text2) + '</td>';
test += '<td>' + text1 + ' = ' + text2 + ' :: ' + (text1 == text2) + '</td>';
test += '<td>' + a.name + ' > ' + b.name + ' :: ' + (a.name > b.name) + '</td>';
test += '</tr>';
document.getElementById("test").innerHTML = test;
if(text1 < text2){
return -1;
}else if(text1 > text2){
return 1;
}else{
return (a.name < b.name) ? -1 : (a.name > b.name) ? 1 : 0;
}
});
//alert(JSON.stringify(data));
document.getElementById("demo").innerHTML = JSON.stringify(data).replace(/},{/g, '},<br/>{');
</script>
</body>
</html>
PHP CI MANIA - PHP Code Generator
โปรแกรมช่วยสร้างโค้ด ลดเวลาการเขียนโปรแกรม เขียนโปรแกรมง่ายและสะดวกขึ้น
สนใจสั่งซื้อราคาสุดคุ้ม >> http://fastcoding.phpcodemania.com/
:: อ้างอิง ::
ความคิดเห็น
แสดงความคิดเห็น