  | 
              
	              
	                
  
    
	 
        การค้นหาคำภาษาไทยใน record ของ mysql ผ่านตัวแปล PHP     | 
   
  
    |   | 
   
 
 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 มาแบบนี้ก็บอกได้อย่างเดียว ค้นหาเอาเองเลยครับ ตัวอย่างการเขียนโค๊ดค้นหาคำมีเยอะ ก็ลองไปหาอ่านศีกษาดูครับ 
 
ถ้าอยากได้คำแนะนำมากกว่านี้ก็เอาโค๊ดที่เขียนมาแสดงด้วย จะได้ช่วยแนะนำจากโค๊ดที่เขียนไว้ได้                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-03-15 23:05:41 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 อันนี้หน้า list ครับ 
 
Code (PHP) 
<?php
include("database_connect.php");
?>
<style>
h1.intro {
	margin-top: 25px;
	border-radius: 10px;
	background-color: #a6a6a6;
	color:white;
	padding-left: 30px;
}
body {
	padding-top: 0px;
	padding-right:  100px;
	padding-left: 100px;
	zoom: 80%;
	background-color: #f2f2f2;
}
input[type=text], select {
		width: 300;
		padding: 5px 23px;
		margin: 3px ;
		display: inline-block;
		border: 1px solid #ccc;
		border-radius: 4px;
		box-sizing: border-box;
	}
input[type=submit] {
	width: 120;
	background-color: #76a0ef;
	color: white;
	padding: 14px 20px;
	margin: 8px 0;
	border: none;
	border-radius: 10px;
	cursor: pointer;
	font-size:13;
}
input[type=submit]:hover {
	background-color: #8db0f2;
}
</style>
<header>
	<h1 class = "intro">Receipt</h1>
	<meta charset="utf-8">
</header><title>All receipts</title>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<form method="post" action = "listreceipt.php">
	<b>Search:<input type="text"  name="searchtext" value= "<?php if (isset($_POST['searchtext'])) echo $_POST['searchtext']; ?>"  placeholder="Type here to search" > 
		<input type="submit" name="searchbutton" src="searchicon.png" value = 'Search' width="30" height="30"  alt="addbutton" formaction = "listreceipt.php" />
		<!--<a href="listreceipt.php" name ="searchbutton"><img src="searchicon.png" width = "30" height ="30" ></a>-->
		<input type = 'submit'  value = 'Add receipt' formaction = "fillreceipt.php"  style="float: right;">
	</form>
	<?php
	if(isset($_POST['searchbutton'])) {
		$result = mysqli_query($dbc, "SELECT * FROM receipt WHERE Date LIKE '%".$_REQUEST['searchtext']."%' OR CustomerName LIKE '%".$_REQUEST['searchtext']."%' OR PaymentType LIKE '%".$_REQUEST['searchtext']."%' OR SaleName LIKE '%".$_REQUEST['searchtext']."%' ORDER BY STR_TO_DATE(Date, '%d-%M-%Y') DESC");
	echo "<table class='w3-table-all'> <thead> <tr class='w3-blue'>";
	echo "<td><bgcolor ='#A2D75C'><center>"."Date"."</td>"."<td><center>"."Customer Name"."</td>"."<td><center>"."Description"."</td>"."<td><center>"."Amount"."</td>"."<td><center>"."Total"."</td>"."<td><center>"."VAT"."</td>"."<td><center>"."Grand Total"."</td>"."<td><center>"."Payment Type"."</td>"."<td><center>"."Sale Name"."</td>"."<td><center>View PDF</td>";
	while ($row=mysqli_fetch_array($result)){
		$id = $row['ReceiptID'];
		echo "<tr>";
		echo "<td><center>".$row['Date']."</td>"."<td><center>".$row['CustomerName']."</td>"."<td><center><pre>".$row['Description']."</pre></td>"."<td><center>".$row['Amount']."</td>"."<td><center>".$row['Total']."</td>"."<td><center>".$row['VAT']."</td>"."<td><center>".$row['GrandTotal']."</td>"."<td><center>".$row['PaymentType']."</td>"."<td><center>".$row['SaleName']."</td>";
		echo "<td><center>"."<a href='testpdf.php?id=$id'><img border='0' src='pdficon.png' width='30' height='30'>"."</center></td>";
	}
		?>
		<a href = "listreceipt.php"> << Back</a>
		<?php
		exit();
	}
	$result = mysqli_query($dbc, "SELECT * FROM receipt ORDER BY STR_TO_DATE(Date, '%d-%M-%Y') DESC");
	echo "<table class='w3-table-all'> <thead> <tr class='w3-blue'>";
	echo "<td><bgcolor ='#A2D75C'><center>"."Date"."</td>"."<td><center>"."Customer Name"."</td>"."<td><center>"."Description"."</td>"."<td><center>"."Amount"."</td>"."<td><center>"."Total"."</td>"."<td><center>"."VAT"."</td>"."<td><center>"."Grand Total"."</td>"."<td><center>"."Payment Type"."</td>"."<td><center>"."Sale Name"."</td>"."<td><center>View PDF</td>";
	while ($row=mysqli_fetch_array($result)){
		$id = $row['ReceiptID'];
		echo "<tr>";
		echo "<td><center>".$row['Date']."</td>"."<td><center>".$row['CustomerName']."</td>"."<td><center><pre>".$row['Description']."</pre></td>"."<td><center>".$row['Amount']."</td>"."<td><center>".$row['Total']."</td>"."<td><center>".$row['VAT']."</td>"."<td><center>".$row['GrandTotal']."</td>"."<td><center>".$row['PaymentType']."</td>"."<td><center>".$row['SaleName']."</td>";
		echo "<td><center>"."<a href='testpdf.php?id=$id'><img border='0' src='pdficon.png' width='30' height='30'>"."</center></td>";
	}
	?>
 
 
อันนี้หน้า database_connect 
Code (PHP) 
<?php
$host= "localhost";
$user="root";
//$password = "";
$dbc=mysqli_connect($host,$user);
mysqli_select_db($dbc, 'xfitness');
mysqli_query($dbc, "SET NAMES UTF8");
if (mysqli_connect($host, $user)) {
	//echo "Connected Database!!!!";
} else {
	echo "Error!!";
}
?>
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-03-15 23:13:00 | 
                        By :
                            nookseal | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 utf-8 collation เป็นอะไรครับ ที่ใช้ได้คือ utf8_general_ci หรือ utf8_unicode_ci แนะนำให้ใช้ utf8_unicode_ci 
 
สำหรับการ คิวรี่  
where concat( field1, '_',field2,'_',field_3) like '%search_text%' จะสั้นกว่าและเร็วกว่า 
อย่าแยกหาทีละ field 
 
และรูปแบบของการเขียนโค๊ด ที่จะทำให้เข้าใจง่ายของ php html javascript 
ให้แยกส่วนของ server (php) และ client( html, css, javascript) ออกจากกันให้ได้มากที่สุด 
คำนวณ php ไว้ส่วนบน เอาตัวแปรที่จำเป็นไปใช้ในส่วนของ html 
Code (PHP) 
<?php
  $variable_php = "...... code php ......";
?><!doctype html>
<html>
<head>
<link .... global css ..../>
<style>
   /* .... only this page css .... */
</style>
<script src="global javascript" ></script>
<script>
   /* .... only this page javascript */
</script>
</head>
<body>
   <div>content  <?=$variable_php?></div>
</body>
</html>
  
 
สำหรับ ไฟล์ include database_connect.php 
Code (PHP) 
<?php
$host= "localhost";
$user='[email protected]'; // เปลี่ยนอย่าใช้ root privilage ใช้ user เฉพาะ domain
$password = '??????';
$dbname = 'xfitness';
$dbc=new mysqli($host,$user, $password, $dbname);
if($dbc->connect_errno){ die('Error<br>' . $dbc->connect_error); }
$dbc->set_charset("UTF-8");
 
 
และการใช้ตัวแปร global ให้ผ่าน function filter_input จะปลอดภัยมากกว่า 
หัดเขียน mysqli เป็น oop เต็มรูปแบบจะเขียนได้สั้นกว่า 
Code (PHP) 
  $searchtext = filter_input(INPUT_POST, 'searchtext', FILTER_SANITIZE_STRING, array("options" => array("default" => '')));
  $result = $dbc->query("SELECT * FROM receipt 
    WHERE concat( Date,'_', CustomerName , '_' , PaymentType , '_' , SaleName) LIKE '%$searchtext%'
    ORDER BY STR_TO_DATE(Date, '%d-%M-%Y') DESC" );
  while( $ro=$result->fetch_object()){
     echo $ro->Date, '-', $ro->CustomerName;
  }
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-03-16 03:13:54 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ลองใช้ DataTables ดูครับ ถ้าชอบใจไม่ต้องเสียเวลาคิวรี่หาทุกฟิลด์แบบนี้ง่ายเลย                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-03-16 08:49:55 | 
                        By :
                            apisitp | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ขอบคุณมากครับ 
 
คือมันติดนิสัยมาตั้งแต่ตอนแรกน่ะครับสำหรับรูปแบบการเขียนโค็ด 
 
ตอนฝึกงาน ก็พยายามเปลี่ยนเป็นแบบ clean code ให้มากกว่านี้อยู่ครับ 
 
ขอบคุณครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-03-16 09:33:45 | 
                        By :
                            nookseal | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |