Archive for December, 2016

Query ringan mysql

Posted: December 5 ,2016 in Uncategorized

Mencari duplikasi data

SELECT payment_journal.`reference_no`
FROM payment_journal
INNER JOIN (SELECT reference_no
FROM payment_journal
GROUP BY payment_journal.`reference_no`
HAVING COUNT(id) > 1) dup
ON payment_journal.`reference_no` = dup.`reference_no`;

 
Mencari duplikasi data

create indexing

CREATE UNIQUE INDEX payment_code_unique
ON ng_invoice_gateway (payment_code);

show all index
SHOW INDEX FROM ng_invoice_gateway;

ADD index
mysql> ALTER TABLE testalter_tbl ADD INDEX (c);

REMOVE index

mysql> ALTER TABLE testalter_tbl DROP INDEX (c);

Fungsi untuk menghitung umur 

function age($date1, $date2) {
$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365 * 60 * 60 * 24));
$months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
//printf("%d Tahun, %d Bulan, %d Hari", $years, $months, $days);
printf("%d Tahun, %d Bulan", $years, $months);
}

Terbilang untuk penulisan kuitansi

 

function terbilang($x) {
    $x = abs($x);
    $angka = array("", "satu", "dua", "tiga", "empat", "lima",
    "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
    $temp = "";
    if ($x <12) {
        $temp = " ". $angka[$x];
    } else if ($x <20) {
        $temp = terbilang($x - 10). " belas";
    } else if ($x <100) {
        $temp = terbilang($x/10)." puluh". terbilang($x % 10);
    } else if ($x <200) {
        $temp = " seratus" . terbilang($x - 100);
    } else if ($x <1000) {
        $temp = terbilang($x/100) . " ratus" . terbilang($x % 100);
    } else if ($x <2000) {
        $temp = " seribu" . terbilang($x - 1000);
    } else if ($x <1000000) {
        $temp = terbilang($x/1000) . " ribu" . terbilang($x % 1000);
    } else if ($x <1000000000) {
        $temp = terbilang($x/1000000) . " juta" . terbilang($x % 1000000);
    } else if ($x <1000000000000) {
        $temp = terbilang($x/1000000000) . " milyar" . terbilang(fmod($x,1000000000));
    } else if ($x <1000000000000000) {
        $temp = terbilang($x/1000000000000) . " trilyun" . terbilang(fmod($x,1000000000000));
    }
    return $temp;
}

Untuk check email valid

public function is_valid_email($email){
 if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
 return true;
 }else{
 return false;
 }
 } 

Untuk check no telp hp

public function is_valid_cellphone($phone){
 /*
 * 
 telkomsel.regex = ^(\\+62|\\+0|0|62)8(1[123]|52|53|21|22|23)[0-9]{5,9}$ 
 simpati.regex = ^(\\+62|\\+0|0|62)8(1[123]|2[12])[0-9]{5,9}$
 as.regex = ^(\\+62|\\+0|0|62)8(52|53|23)[0-9]{5,9}$ 
 indosat.regex= ^(\\+62815|0815|62815|\\+0815|\\+62816|0816|62816|\\+0816|\\+62858|0858|62858|\\+0814|\\+62814|0814|62814|\\+0814)[0-9]{5,9}$
 im3.regex = ^(\\+62855|0855|62855|\\+0855|\\+62856|0856|62856|\\+0856|\\+62857|0857|62857|\\+0857)[0-9]{5,9}$
 xl.regex = ^(\\+62817|0817|62817|\\+0817|\\+62818|0818|62818|\\+0818|\\+62819|0819|62819|\\+0819|\\+62859|0859|62859|\\+0859|\\+0878|\\+62878|0878|62878|\\+0877|\\+62877|0877|62877)[0-9]{5,9}$ 
 smart.regex = ^(\\+62|\\+0|0|62)8(81|87)[0-9]{5,9}$
 fren.regex = ^(\\+62|\\+0|0|62)8(88|89)[0-9]{5,9}$
 tri.regex = ^(\\+62|\\+0|0|62)8(98|99)[0-9]{5,9}$
 BYRU.regex = ^(\\+62|\\+0|0|62)8(68)[0-9]{5,9}$
 
 */
 
 if (preg_match("/^(\\+62|\\+0|0|62)8(1[123]|52|53|21|22|23)[0-9]{5,9}$/", $phone)){
 return true;
 }else if (preg_match("/^(\\+62|\\+0|0|62)8(1[123]|2[12])[0-9]{5,9}$/", $phone)){
 return true;
 }else if (preg_match("/^(\\+62|\\+0|0|62)8(52|53|23)[0-9]{5,9}$/", $phone)){
 return true;
 }else if (preg_match("/^(\\+62815|0815|62815|\\+0815|\\+62816|0816|62816|\\+0816|\\+62858|0858|62858|\\+0814|\\+62814|0814|62814|\\+0814)[0-9]{5,9}$/", $phone)){
 return true;
 }else if (preg_match("/^(\\+62855|0855|62855|\\+0855|\\+62856|0856|62856|\\+0856|\\+62857|0857|62857|\\+0857)[0-9]{5,9}$/", $phone)){
 return true;
 }else if (preg_match("/^(\\+62817|0817|62817|\\+0817|\\+62818|0818|62818|\\+0818|\\+62819|0819|62819|\\+0819|\\+62859|0859|62859|\\+0859|\\+0878|\\+62878|0878|62878|\\+0877|\\+62877|0877|62877)[0-9]{5,9}$/", $phone)){
 return true;
 }else if (preg_match("/^(\\+62|\\+0|0|62)8(81|87)[0-9]{5,9}$/", $phone)){
 return true;
 }else if (preg_match("/^(\\+62|\\+0|0|62)8(88|89)[0-9]{5,9}$/", $phone)){
 return true;
 }
 else if (preg_match("/^(\\+62|\\+0|0|62)8(98|99)[0-9]{5,9}$/", $phone)){
 return true;
 }
 else if (preg_match("/^(\\+62|\\+0|0|62)8(68)[0-9]{5,9}$/", $phone)){
 return true;
 }else{
 return false;
 }
 } 

Untuk check apakan komputer terkoneksi dengan internet

 function is_connected()
 {
 $connected = @fsockopen("www.google.com", 80); 
 //website, port (try 80 or 443)
 if ($connected){
 $is_conn = true; //action when connected
 fclose($connected);
 }else{
 $is_conn = false; //action in connection failure
 }
 return $is_conn;

 }

 

Untuk Format Rupiah

    public function rupiahFormat($price) {
        return "Rp " . number_format($price, 2);
    }

Keyboard arah dengan javascript

function checkKey(e) {
    var event = window.event ? window.event : e;
    console.log(event.keyCode)
}

arrow keys are only triggered by onkeydown, not onkeypress

keycodes are:

  • left = 37
  • up = 38
  • right = 39
  • down = 40
 $("input").not($(":button")).keypress(function (evt) {
            if (evt.keyCode == 13) {
                iname = $(this).val();
                if (iname !== 'Submit') {
                    var fields = $(this).parents('form:eq(0),body').find('button, input, textarea, select');
                    var index = fields.index(this);
                    if (index > -1 && (index + 1) < fields.length) {
                        fields.eq(index + 1).focus();
                    }
                    return false;
                }
            }
        });