Cara Mengirim Data Antar Intent

Setelah sekian minggu hiatus sekarang saya kembali dengan tutorial baru. Masih seputar android. Tutorial kali ini akan menunjukkan bagaimana caranya mengirim dan menerima parameter antar Activity yang dilakukan melalui mekanisme Intent.

Continue reading “Cara Mengirim Data Antar Intent”

Share

Kemarin tanggal berapa ya?

Kerja hari ini saya dihadapkan pada problem tanggal. Ceritanya, ada script yang harus diperbaiki. Karena metode yang digunakan berbeda dengan sebelumnya, akhirnya saya harus bermain-main dengan tanggal, atau lebih spesifik lagi, tanggal kemarin. Dari dulu, yang ada dikepala saya, kalau mengambil tanggal kemarin di PHP itu adalah mengambil dari fungsi mktime() yang dikombinasikan dengan date():

<?php $yesterday = date("Y-m-d", mktime(0,0,0,date("m"), date("d")-1, date("Y"))); ?>

Tapi, ada tapi-nya nih… Hari ini pikiran saya susah sekali untuk diajak kompromi, jadi saya sama sekali lupa pada metode simple diatas dan re-invent the whell. Sampai akhirnya saya menemukan bahwa untuk mendapatkan tanggal kemarin bisa dengan kode seperti berikut:

<?php $yesterday = date('Y-m-d', (strtotime($today) - (24*60*60))); ?>

Yang dioptimize menjadi:

<?php $yesterday = date('Y-m-d', (strtotime($today) - (86400))); ?>

. Tapi aneh, sampai disini saya tiba2 ingat metode pertama yang menimbulkan niat untuk menguji performa diantara metode tersebut, mana yang paling cepat.

Kemudian saya membuat script benchmark sederhana:

<?php
$total = 100000;
$today = date('Y-m-d');
$start1 = microtime(true);
for($i = 1; $i <= $total; $i++){
        $yesterday = date("Y-m-d", mktime(0,0,0,date("m"), date("d")-1, date("Y")));
        //$yesterday = date('Y-m-d', strtotime('-1 day', strtotime($today)));
}
$end1 = microtime(true);
//echo "$start1 - $end1\n";
$time1 = $end1 - $start1;

$start2 = microtime(true);
for($i = 1; $i <= $total; $i++){
        $yesterday = date('Y-m-d', (strtotime($today) - (86400)));
}
$end2 = microtime(true);
//echo "$start2 - $end2\n";
$time2 = $end2 - $start2;

echo "Benchmark $total times.\n";
echo "Method 1: $time1\n";
echo "Method 2: $time2\n";
//echo "\n";
//echo "\n";
?>

Hasil yang didapat cukup membuat saya kaget. Ternyata ada gunanya juga sedikit pikun :)). Sebab, metode kedua mampu meningkatkan performa berkali lipat. Berikut output setelah 3 kali menjalankan script benchmark:

Benchmark 100000 times.
Method 1: 27.5039460659
Method 2: 9.91087913513

Benchmark 100000 times.
Method 1: 25.8903188705
Method 2: 9.55417895317

Benchmark 100000 times.
Method 1: 36.927713871
Method 2: 14.1359829903

Dari sini bisa dilihatkan kalau metode kedua ternyata lebih cepat. Jadi, kalau lain waktu butuh metode untuk mengetahui tanggal kemarin, pergunakanlah metode kedua. Kalo ada yang lebih cepat lagi, kasih kabar ya!!!

Share

Get Filename Without Extension

If you want to get the filename without extension in PHP, you can use this function :

<?php
function getFilenameWithoutExt($filename){
    $pos = strripos($filename, '.');
    if($pos === false){
        return $filename;
    }else{
        return substr($filename, 0, $pos);
    }
}
?>


Here’s example on how to use it :

<?php
echo getFilenameWithoutExt('this_is_a_file.with_dot_on_it.txt');
//should generate output `this_is_a_file.with_dot_on_it`
?>

Share

Get Selected Row from DataGridView

This is how to get selected row from DataGridView in .NET 2.0

  1. Make sure there’s a single cell that is currently selected
  2. Get current row index using {datagridview instance}.CurrentCell.RowIdex
  3. Get the selected row using {datagridview instance}.Rows({current row index}).Cells({zero based index}).Value


Here’s the sample code :

Dim crow = DataGridView1.CurrentCell.RowIndex
dim txt as string
Try
    txt = DataGridView1.Rows(crow).Cells(0).Value & " || "
    txt = txt & DataGridView1.Rows(crow).Cells(1).Value & " || "
    txt = txt & DataGridView1.Rows(crow).Cells(2).Value

    MessageBox.show (txt)
Catch ex As Exception
    ' do nothing
End Try

Share

Get Current Directory from PHP

Yes, I’m lazy because if I want to manual, I’ll find a method to find current directory instead of reinventing the wheel. Hehehe, so, here’s what I do to find current directory :

echo dirname(__FILE__);

Now, I have new post for my blog :D.

Update: As comments from readers, you can also use getcwd() to get equal result.

Share

Strange Javascript Behaviour

Yeah… I’m dumb. But, how do I know that this simple code generate quite difficult to understand error?

	function buildParameter(scr, txt){
		var returnValue = "";
		var temp = txt.split("\n");
		returnValue = '<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?><request>'
					+ '<script>' + scr + '</script>';
		for(i = 0; i < temp.length; i++){
			x = temp&#91;i&#93;.split('=');
			returnValue += '<param name="' + x&#91;0&#93; + '">' + x[1] + '</param>';	
		}
		returnValue += '</request>';
		
		alert(returnValue);
		return returnValue;
	}

Go a head, test it with your editor. Here, I got

unterminated string literal

And how do I know what is generating this error?. You know what? Here’s the suspect :

		returnValue = '<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?><request>'
					+ '<script>' + scr + '</script>';

I don’t know why is this generating error and I’m googling without result. Maybe wrong query. But, I found the solution by trial and error. So, here’s the weapon to terminate this suspect :

		returnValue = '<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?><request>'
					+ '<' + 'script' + '>' + scr + '</' + 'script>';

Share

Active Input

Well, I think some of you confused while reading the title of the post. I just can’t find any good word. The idea of this post is to show you how to create simple CSS to show which form input is active. You know, when working with so many input box, you’ll sometime forgot what field you’re working on. Even when there’s cursor blinking on it.

So, here’s what I usually do. Continue reading “Active Input”

Share

Using Try.these to make your javascript code robust

Hi there, this is yet another prototype tutorial. Today well learn a simple way to avoid error in our javascript code. We’ll use Try.these method of prototype framework. So, how do we do this? First, we need to call the prototype library. Continue reading “Using Try.these to make your javascript code robust”

Share

HOWTO : Lock Form Field

Sedikit coba-coba dengan event di javascript, akhirnya nemu satu trik sederhana untuk mengunci form agar tidak bisa di-edit tanpa menambahkan atribut readonly maupun disable.
Berikut caranya :

<form name="form">
     <input type="text" size="12" onfocus="this.blur()" value="Locked text!" name="box" />
</form>

.
Perhatikan pada event onFocus, kita beri dia perintah this.blur(), sehingga field tersebut tidak pernah menerima focus.

Share