Tuesday, April 11, 2017

MySQL lỗi sql_mode=only_full_group_by

Một ngày đẹp trời bạn nâng cấp Mysql hoặc thì bị dính lỗi sql_mode=only_full_group_by
Nguyên nhân là từ MySQL 5.7.5+ mặc định nó xài thêm mode ONLY_FULL_GROUP_BY. Nên chúng ta phải khai báo lại... k xài mode đó nữa.

Mở file /etc/mysql/conf.d/mysql.cnf, thêm vào:

mysqld]
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

Khởi động lại mysql: sudo service mysql restart

Saturday, October 10, 2015

jQuery âm lịch (jQuery.amlich)

jQuery Plugin âm lịch


Sử dụng:
$(element).amLich({
  type: 'calendar', // month hoặc year hoặc text
  tableWidth: '200px' // Chiều rộng lịch
});

Download: https://github.com/ichuot/jquery-amlich

Tuesday, September 29, 2015

Hàm PHP đọc số tiền thành chữ

Hàm PHP đọc số thành chữ (tiền)
function convert_number_to_words($number)
{
  $hyphen = ' ';
  $conjunction = '  ';
  $separator = ' ';
  $negative = 'âm ';
  $decimal = ' phẩy ';
  $dictionary = array(
    0 => 'không',
    1 => 'một',
    2 => 'hai',
    3 => 'ba',
    4 => 'bốn',
    5 => 'năm',
    6 => 'sáu',
    7 => 'bảy',
    8 => 'tám',
    9 => 'chín',
    10 => 'mười',
    11 => 'mười một',
    12 => 'mười hai',
    13 => 'mười ba',
    14 => 'mười bốn',
    15 => 'mười năm',
    16 => 'mười sáu',
    17 => 'mười bảy',
    18 => 'mười tám',
    19 => 'mười chín',
    20 => 'hai mươi',
    30 => 'ba mươi',
    40 => 'bốn mươi',
    50 => 'năm mươi',
    60 => 'sáu mươi',
    70 => 'bảy mươi',
    80 => 'tám mươi',
    90 => 'chín mươi',
    100 => 'trăm',
    1000 => 'ngàn',
    1000000 => 'triệu',
    1000000000 => 'tỷ',
    1000000000000 => 'nghìn tỷ',
    1000000000000000 => 'ngàn triệu triệu',
    1000000000000000000 => 'tỷ tỷ'
  );
  if (!is_numeric($number)) {
    return false;
  }

  if (($number >= 0 && (int)$number < 0) || (int)$number < 0 - PHP_INT_MAX) {

    // overflow

    trigger_error('convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX, E_USER_WARNING);
    return false;
  }

  if ($number < 0) {
    return $negative . convert_number_to_words(abs($number));
  }

  $string = $fraction = null;
  if (strpos($number, '.') !== false) {
    list($number, $fraction) = explode('.', $number);
  }

  switch (true) {
  case $number < 21:
    $string = $dictionary[$number];
    break;

  case $number < 100:
    $tens = ((int)($number / 10)) * 10;
    $units = $number % 10;
    $string = $dictionary[$tens];
    if ($units) {
      $string.= $hyphen . $dictionary[$units];
    }

    break;

  case $number < 1000:
    $hundreds = $number / 100;
    $remainder = $number % 100;
    $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
    if ($remainder) {
      $string.= $conjunction . convert_number_to_words($remainder);
    }

    break;

  default:
    $baseUnit = pow(1000, floor(log($number, 1000)));
    $numBaseUnits = (int)($number / $baseUnit);
    $remainder = $number % $baseUnit;
    $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
    if ($remainder) {
      $string.= $remainder < 100 ? $conjunction : $separator;
      $string.= convert_number_to_words($remainder);
    }

    break;
  }

  if (null !== $fraction && is_numeric($fraction)) {
    $string.= $decimal;
    $words = array();
    foreach(str_split((string)$fraction) as $number) {
      $words[] = $dictionary[$number];
    }

    $string.= implode(' ', $words);
  }

  return $string;
}

Monday, September 28, 2015

Hàm javascript đọc số thành chữ

/**
* Đọc số thành chữ (Vietnamese)
* DOCSO.doc( 123 );
*/
var DOCSO=function(){var t=["không","một","hai","ba","bốn","năm","sáu","bảy","tám","chín"],r=function(r,n){var o="",a=Math.floor(r/10),e=r%10;return a>1?(o=" "+t[a]+" mươi",1==e&&(o+=" mốt")):1==a?(o=" mười",1==e&&(o+=" một")):n&&e>0&&(o=" lẻ"),5==e&&a>=1?o+=" lăm":4==e&&a>=1?o+=" tư":(e>1||1==e&&0==a)&&(o+=" "+t[e]),o},n=function(n,o){var a="",e=Math.floor(n/100),n=n%100;return o||e>0?(a=" "+t[e]+" trăm",a+=r(n,!0)):a=r(n,!1),a},o=function(t,r){var o="",a=Math.floor(t/1e6),t=t%1e6;a>0&&(o=n(a,r)+" triệu",r=!0);var e=Math.floor(t/1e3),t=t%1e3;return e>0&&(o+=n(e,r)+" ngàn",r=!0),t>0&&(o+=n(t,r)),o};return{doc:function(r){if(0==r)return t[0];var n="",a="";do ty=r%1e9,r=Math.floor(r/1e9),n=r>0?o(ty,!0)+a+n:o(ty,!1)+a+n,a=" tỷ";while(r>0);return n.trim()}}}();

Cách dùng

alert( DOCSO.doc(123000) );

Friday, September 11, 2015

Vô hiệu Xem trước In (Print review) của chrome

Tạo 1 shortcut của Chrome mới với Target: "C:\Program Files\Google\Chrome\Application\chrome.exe" --disable-print-preview

Tham số --disable-print-preview sẽ vô hiệu hóa xem trước khi in. Bấm in sẽ bật ra giao diện chọn máy in để in.

Nếu thay bằng tham số --kiosk-printing sẽ in luôn mà không cần hỏi chọn máy in nào. Máy in mặc định được in sẽ là máy in trước đó bạn chọn. Tham số rất hữu ích để triển khai hệ thống POS bán hàng... k phải mất thời gian xác nhận máy in.

Và tham số --kiosk sẽ cho phép Chrome chạy ở trạnh thái Full Screen.

Chú ý:
- Thoát Chrome hoàn toàn mà mở lại bằng shortcut mới thì tham số mới có hiệu lực.
- Đường dẫn đến ứng dụng Chrome thay đổi tùy theo máy tính của bạn.

Sunday, August 23, 2015

Nâng cấp PHP cho AppServ

Còn rất nhiều người dùng Windows XP và... cũng như là AppServ (appserv-win32-2.5.10.exe). Phiên bản 2.5.10 đã rất lâu rồi không được update và hình như project đã bị loại bỏ cập nhật luôn thì phải. Có một điều tuyệt vời của AppServ là nó rất nhẹ. Nó đã loại bỏ hết những thành phần không cần thiết của 1 LAMPP. Chỉ duy nhất có Apache 2.2 với PHP 5.2.6. Đối với Apache 2.2 có cũ thì cũ nhưng vấn đề không quan trọng lắm với 1 coding vì người ta hay dùng để developing là chính nhưng với PHP 5.2.6 cũ kỹ thì việc coding sẽ là vấn đề khó khăn vì những đặc tính mới của PHP bị thiếu. Vào thời điểm 2015 hiện tại thì hầu hết web hay opensource đều yêu cầu 5.3 trở lên.

Mình sẽ thực hiện việc nâng cấp lên 5.3
- Tải 5.3 ở đây: http://windows.php.net/downloads/releases/archives/ (mình chọn version cuối cùng của 5.3 http://windows.php.net/downloads/releases/archives/php-5.3.9-Win32-VC9-x86.zip)

- Mình tải bản ZIP và thực hiện cài đặt thủ công. Mình giải nén ZIP đó ra thư mục C:/AppServ/php53

- Tiếp mở C:\AppServ\Apache2.2\conf\httpd.conf tìm và thay dòng LoadModule php5_module C:/AppServ\php5\php5apache2_2.dll thành 2 dòng:
LoadModule php5_module C:/AppServ/php53/php5apache2_2.dll
PHPIniDir C:/AppServ/php53/php.ini

- Đổi tên file C:\AppServ\php53\php.ini-development thành C:\AppServ\php53\php.ini

- Mở C:\AppServ\php53\php.ini.
+ Tìm ;extension_dir = "ext" và thay bằng extension_dir = "C:/AppServ/php53/ext"
+ Gở bỏ dấu ; trước các dòng:
extension=php_curl.dll
extension=php_gd2.dll
extension=php_mbstring.dll
extension=php_exif.dll
extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_pdo_mysql.dll
extension=php_pdo_sqlite.dll
extension=php_sockets.dll
extension=php_sqlite.dll
extension=php_sqlite3.dll
(đây là những extension cơ bản)
+ Tìm và thay thế ;date.timezone = thành date.timezone = "Asia/Saigon"

- Tiếp theo là restart lại Apache: Ấn tổ hợp phím Windows + R, gõ vào services.msc chạy và tìm Apache2.2, bấm phải chọn Restart. Nếu không có lỗi gì thì ok rồi.

- Bạn mở C:\AppServ\www\phpinfo.php sửa <? thành <?php, lưu lại rồi dùng trình duyệt kiểm tra http://localhost/phpinfo.php xem có phải php 5.3.9 không.

Sunday, July 5, 2015

Thiết lập wildcard subdomain dùng BIND trên Ubuntu

Ví dụ ta code PHP trên ubuntu. Tạo 1 project dev.com (localhost) và muốn mọi subdomain của nó đều trỏ về 127.0.0.1 (Hoặc bất kỳ IP nào). Nhầm mục đích lập trình, test code.

Cài đặt bind
sudo apt-get install bind9

Thiết lập BIND như là 1 caching server. Có nghĩa là card mạng dùng DNS của BIND dùng DNS của public của DNS nào đó nếu muốn internet

Mở:
/etc/bind/named.conf.options

Thiết lập:
forwarders {
  # Phía dưới dùng google DNS. Bạn có thể dùng DNS khác
  8.8.8.8;
  8.8.4.4;
};

Tạo 1 ZONE

Mở:
/etc/bind/named.conf.local

Thêm 1 zone:
zone "dev.com" {
  type master;
  file "/etc/bind/db.dev.com";
};

Tạo:
/etc/bind/db.dev.com

Thêm vào:
;
; BIND data file for local loopback interface
;
$TTL 14400
@ IN  SOA dev.com. root.dev.com. (
            2012091901   ; Serial
                 86400   ; Refresh
                  7200   ; Retry
               3600000   ; Expire
                 86400 ) ; Negative Cache TTL
;
@ IN  NS  dev.com.
@ IN  A 127.0.0.1
* IN  A 127.0.0.1
@ IN  AAAA  ::1

Khởi động lại BIND

sudo service bind9 restart

Bước tiếp theo là thiết lập card mạng chúng ta dùng DNS 127.0.0.1
Please google!

Kết tiếp là thiết lập Apache (tạo virtual host) cho tên miền dev.com
<Virtualhost *:80>
  ServerName dev.com
  ServerAlias *.dev.com
  DocumentRoot /home/user/www/dev.com
  <directory "/home/user/www/dev.com">
    AllowOverride All
    Options All
    Order allow,deny
    Allow from all
  </Directory>
  ErrorLog /var/log/apache2/dev.com.error.log
  CustomLog /var/log/apache2/dev.com.access.log combined
</VirtualHost>