|
 |
|
พี่ๆค่ะ รบกวนดูโค้ดให้หน่อยนะค่ะ คือตอนนี้ หนูทำการค้นหาข้อมูลจากฐานข้อมูลค่ะ แล้วหนูนับคำที่ค้นเจอด้วยค่ะ |
|
 |
|
|
 |
 |
|
พี่ๆค่ะ รบกวนดูโค้ดให้หน่อยนะค่ะ
คือตอนนี้ หนูทำการค้นหาข้อมูลจากฐานข้อมูลค่ะ แล้วหนูนับคำที่ค้นเจอด้วยค่ะ ว่าในแต่ละเรคคอร์ดเจอกี่คำ
ปัญหาที่อยากรบกวนคือ หนูอยากทราบว่า ถ้าต้องการเรียงลำดับการค้นหาจากมากไปน้อย ต้องทำอย่างไรค่ะ?
รบกวนพี่ๆ ผู้รู้ด้วยนะค่ะ
Code (PHP)
<meta charset="utf-8" />
<?php
require("connect.php");
$search = 'ปวดท้อง ตำลึง ยาสาระบาด';
//$search = $_POST['searchtxt'];
$inputlist = explode(' ', trim($search));
echo "<h3>...Inputlist..."; print_r($inputlist); echo "</h3><br>";
$conditionarr = array();
foreach ($inputlist as $val) {
array_push($conditionarr, "isan like '%{$val}%'");
array_push($conditionarr, "thai like '%{$val}%'");
}
//echo '$conditionarr.......'; print_r($conditionarr); echo '<br><hr>';
if (count($conditionarr) > 0) {
$condition = " where " . implode(" or ", $conditionarr);
//echo "<br>condition..."; print_r($condition); echo "<br><hr>";
}
$sql = "SELECT * FROM dictionary $condition";
echo "select dict...".$sql."<br><hr>";
$data = mysql_query($sql);
$dictISArr = array();
$dictTHArr = array();
while ($row = mysql_fetch_array($data)){
$dictISArr[] = $row[1];
$dictTHArr[] = $row[2];
// echo '<br>Isan...'; print_r($dictISArr); echo '<hr>';
// print_r($dictTHArr); echo '<hr>';
}
echo 'Inputlist...2'; print_r($inputlist); echo '<hr>';
$mergeArray_d = array_merge($dictISArr, $dictTHArr, $inputlist);
echo 'merg_d....'; print_r( $mergeArray_d ); echo '<br><hr>';
$resultxx_d = array_unique($mergeArray_d);
echo 'resultxx_d....'; print_r( $resultxx_d ); echo '<br>';
$replace = str_replace($dictTHArr, ' ', $resultxx_d);
echo 'REPLACE --> '; print_r($replace); echo '<hr>';
$conditionarrM = array();
foreach ($resultxx_d as $val2) {
array_push($conditionarrM, "keyword like '%{$val2}%'");
array_push($conditionarrM, "treat like '%{$val2}%'");
}
//echo '$conM...'; print_r($conditionarrM); echo '<br><hr>';
if (count($conditionarrM) > 0) {
$conditionM = " where " . implode(" or ", $conditionarrM);
//echo "<br>...conditionM..."; print_r($conditionM); echo "<br><hr>";
}
$resultM = array();
$strsql = "select * from med $conditionM";
echo "SELECT_MED =>...".$strsql."<br><br>";
$datamed = mysql_query($strsql);
$num = mysql_num_rows($datamed);
echo $num.'<br>';
while ($rowmed = mysql_fetch_assoc($datamed)) {
$resultM[] = $rowmed;
$wordarr = explode(' ', $rowmed['keyword']);
$wordarr2 = explode(' ', $rowmed['treat']);
$mergeArray=array_merge($wordarr, $wordarr2);
$resultxx = array_unique($mergeArray);
// echo '<hr>resultxx...'; print_r($resultxx); echo '<br><br>';
$keyword = $inputlist;
$countkeysearch = array();
foreach ($replace as $valuesearch){
// echo 'value '; print_r($valuesearch); echo '-----';
$count = 0;
foreach ($resultxx as $value){
// echo 'value '; print_r($value); echo '</br>';
if (@strpos($value, $valuesearch) !== false){
$count++;
}
}
$countkeysearch[$valuesearch] = $count;
//echo "..........Count...".$countkeysearch[$valuesearch]; echo ' '.$valuesearch.'<br>';// จำนวนที่ได้แต่ละค่า
}
$sum = array_sum($countkeysearch);
//echo 'รหัสตำรายา...'.$rowmed['medID'].'<br>คำค้น...'.$rowmed['keyword'].'<br>การรักษา...'.$rowmed['treat'].'<br>เนื้อหาตำรายา...'.$rowmed['detail'].' พบทั้งหมด '.$sum.' คำ '; echo '<hr>';
//echo '<br>Count..'; print_r($countkeysearch); echo '<br>';
$key_med = $rowmed['keyword'];
$treat_med = $rowmed['treat'];
foreach ($resultxx_d AS $w){
$key_med = str_replace($w, '<b>' . $w . '</b>', $key_med);
$treat_med = str_replace($w, '<b>' . $w . '</b>', $treat_med);
}
// echo 'รหัสตำรายา...'.$rowmed['medID'].'<br>คำค้น...'.$key_med.'<br>การรักษา...'.$treat_med.'<br>เนื้อหาตำรายา...'.$rowmed['detail'].' พบทั้งหมด '.$sum.' คำ '; echo '<hr>';
// echo 'รหัสตำรายา...'.$rowmed['medID'].' '.'พบทั้งหมด '.$sum.' คำ '; echo '<hr>';
// echo $sum;
}//echo '<br>Count..'; print_r($countkeysearch); echo '<br>';
?>
<!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>Untitled Document</title>
</head>
<body>
</body>
</html>
Tag : PHP, MySQL

|
|
 |
 |
 |
 |
Date :
2014-02-15 22:30:00 |
By :
stampbnp |
View :
762 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ่านไม่ออกขอ อนุญาติวางให้ใหม่น่ะครับCode (PHP)
<meta charset="utf-8" />
<?php
require("connect.php");
$search = 'ปวดท้อง ตำลึง ยาสาระบาด';
//$search = $_POST['searchtxt'];
$inputlist = explode(' ', trim($search));
echo "<h3>...Inputlist..."; print_r($inputlist); echo "</h3><br>";
$conditionarr = array();
foreach ($inputlist as $val) {
array_push($conditionarr, "isan like '%{$val}%'");
array_push($conditionarr, "thai like '%{$val}%'");
}
//echo '$conditionarr.......'; print_r($conditionarr); echo '<br><hr>';
if (count($conditionarr) > 0) {
$condition = " where " . implode(" or ", $conditionarr);
//echo "<br>condition..."; print_r($condition); echo "<br><hr>";
}
$sql = "SELECT * FROM dictionary $condition";
echo "select dict...".$sql."<br><hr>";
$data = mysql_query($sql);
$dictISArr = array();
$dictTHArr = array();
while ($row = mysql_fetch_array($data)){
$dictISArr[] = $row[1];
$dictTHArr[] = $row[2];
// echo '<br>Isan...'; print_r($dictISArr); echo '<hr>';
// print_r($dictTHArr); echo '<hr>';
}
echo 'Inputlist...2'; print_r($inputlist); echo '<hr>';
$mergeArray_d = array_merge($dictISArr, $dictTHArr, $inputlist);
echo 'merg_d....'; print_r( $mergeArray_d ); echo '<br><hr>';
$resultxx_d = array_unique($mergeArray_d);
echo 'resultxx_d....'; print_r( $resultxx_d ); echo '<br>';
$replace = str_replace($dictTHArr, ' ', $resultxx_d);
echo 'REPLACE --> '; print_r($replace); echo '<hr>';
$conditionarrM = array();
foreach ($resultxx_d as $val2) {
array_push($conditionarrM, "keyword like '%{$val2}%'");
array_push($conditionarrM, "treat like '%{$val2}%'");
}
//echo '$conM...'; print_r($conditionarrM); echo '<br><hr>';
if (count($conditionarrM) > 0) {
$conditionM = " where " . implode(" or ", $conditionarrM);
//echo "<br>...conditionM..."; print_r($conditionM); echo "<br><hr>";
}
$resultM = array();
$strsql = "select * from med $conditionM";
echo "SELECT_MED =>...".$strsql."<br><br>";
$datamed = mysql_query($strsql);
$num = mysql_num_rows($datamed);
echo $num.'<br>';
while ($rowmed = mysql_fetch_assoc($datamed)) {
$resultM[] = $rowmed;
$wordarr = explode(' ', $rowmed['keyword']);
$wordarr2 = explode(' ', $rowmed['treat']);
$mergeArray=array_merge($wordarr, $wordarr2);
$resultxx = array_unique($mergeArray);
// echo '<hr>resultxx...'; print_r($resultxx); echo '<br><br>';
$keyword = $inputlist;
$countkeysearch = array();
foreach ($replace as $valuesearch){
// echo 'value '; print_r($valuesearch); echo '-----';
$count = 0;
foreach ($resultxx as $value){
// echo 'value '; print_r($value); echo '</br>';
if (@strpos($value, $valuesearch) !== false){
$count++;
}
}
$countkeysearch[$valuesearch] = $count;
//echo "..........Count...".$countkeysearch[$valuesearch]; echo ' '.$valuesearch.'<br>';// จำนวนที่ได้แต่ละค่า
}
$sum = array_sum($countkeysearch);
//echo 'รหัสตำรายา...'.$rowmed['medID'].'<br>คำค้น...'.$rowmed['keyword'].'<br>การรักษา...'.$rowmed['treat'].'<br>เนื้อหาตำรายา...'.$rowmed['detail'].' พบทั้งหมด '.$sum.' คำ '; echo '<hr>';
//echo '<br>Count..'; print_r($countkeysearch); echo '<br>';
$key_med = $rowmed['keyword'];
$treat_med = $rowmed['treat'];
foreach ($resultxx_d AS $w){
$key_med = str_replace($w, '<b>' . $w . '</b>', $key_med);
$treat_med = str_replace($w, '<b>' . $w . '</b>', $treat_med);
}
// echo 'รหัสตำรายา...'.$rowmed['medID'].'<br>คำค้น...'.$key_med.'<br>การรักษา...'.$treat_med.'<br>เนื้อหาตำรายา...'.$rowmed['detail'].' พบทั้งหมด '.$sum.' คำ '; echo '<hr>';
// echo 'รหัสตำรายา...'.$rowmed['medID'].' '.'พบทั้งหมด '.$sum.' คำ '; echo '<hr>';
// echo $sum;
}//echo '<br>Count..'; print_r($countkeysearch); echo '<br>';
?>
<!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>Untitled Document</title>
</head>
<body>
</body>
</html>
|
 |
 |
 |
 |
Date :
2014-02-15 23:04:15 |
By :
dekkuza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตรงที่ผมใส่ desc ,xxx คือชื่อ column
Code (SQL)
$sql = "SELECT * FROM dictionary $condition order by xxxx desc";
|
ประวัติการแก้ไข 2014-02-15 23:08:53 2014-02-15 23:10:16
 |
 |
 |
 |
Date :
2014-02-15 23:07:21 |
By :
dekkuza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
การค้นหาของโค้ดนี้ จะอยู่ใน คำสั่งนี้ ค่ะ $strsql = "select * from med $conditionM";
คือมัน จะไป ค้นหาในฟิลด์ 2 ฟิลด์ค่ะ แล้วตอนนี้ นับผลรวมการค้นหาในแต่ละ ตัวได้แล้ว เก็บไว้ในตัวแปร $sum ค่ะ แต่ว่า ทำการค้นหาจากมาก ไปน้อยไม่เป็น ค่ะพี่ รบกวนด้วยนะค่ะ
|
 |
 |
 |
 |
Date :
2014-02-16 02:36:57 |
By :
stampbnp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (SQL)
select * from xxtable order by x1,x2,x2 desc
|
 |
 |
 |
 |
Date :
2014-02-16 22:08:59 |
By :
dekkuza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
select * from med $conditionM order by (ฟิวที่ต้องการเรียงจ๊า)
http://www.w3schools.com/php/php_mysql_order_by.asp
|
 |
 |
 |
 |
Date :
2014-02-17 11:59:08 |
By :
nut_ch31 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|