Wednesday, July 14

PHP: Dates and time in PHP

I will discuss dates and time in PHP. To kick things off, take a look at PHP's time()
function, which displays the current time measured in the number of seconds
since the Unix Epoch (January 1 1970 00:00:00 GMT):

print time();

This is also called a UNIX timestamp, and as mentioned, displays the current
time in seconds passed since the Unix Epoch. A sample output may look like:

1069142586

Using date() to display the date and time

Now, we want to display not just the current time and date, but in a user
friendly format. To do that we should enlist PHP's date() function.
This date function is used to convert from a UNIX timestamp to a human readable
date. The date() function looks like:

string date ( string format [, int timestamp])

You can see that the timestamp is surrounded by '[' and ']', which means it's
optional. If we put it then the function will use it and if we don't put it then
the function will use the default timestamp. The default timestamp which is used
when we don't put any timestamp is the current time.

Let's start our work with the date function. We want to show the user what
date and time is now.

print date("l, F jS Y - H:i:s");
//Example output: Tuesday, November 18th 2003 - 03:20:14

PHP: 5 Top PHP Coding Tutorials, Tips and Tricks

PHP is used in more a less most websites on the internet. We can learn from 5 top useful PHP tips / tricks and tutorials.

Limit Characters From Your Text


In this tutorial you will learn how to limit characters from a sentence without cutting words up. This is a really useful tutorial.

http://www.jooria.com/Limit-Characters-From-Your-Text-a139.html

Create An Advanced Password Recovery Utility

Learn how to create a very advanced password recovery tool using PHP. This can be useful, and you can implement it in to your website login system.

http://net.tutsplus.com/tutorials/php/creating-an-advanced-password-recovery-utility/

Login To Analytics API Using PHP

Great little tip / trick that teaches you how to login to the Google Analytics API using ClientLogin.

http://www.electrictoolbox.com/google-analytics-login-php-curl-username-password/
Error 404 Pages With PHP Auto-Mailer


An awesome tutorial for creating a custom error 404 page.

http://net.tutsplus.com/tutorials/php/404403-website-error-pages-with-php-auto-mailer/
Resession (Session Manager)

Check this out. Its a really nifty session manager that you can integrate with your website.

http://www.milesj.me/resources/script/session-manager

Tuesday, July 13

PHP: return value dari fungsi

Tidak seperti dalam bahasa pemograman C, java, C# dan sebagainya, dalam PHP untuk fungsi yang dapat mengembalikan nilai (return value) tidak didefinisikan terlebih dahulu. baik itu return value nya berupa satu nilai, atau beberapa nilai.
kali ini saya akan membahas bagaimana cara membuat fungsi yang mempunyai return value, berikut contohnya:


function jumlahan($bil1, $bil2){
$hasil = $bil1+$bil2;
return $hasil;
}

$total = jumlahan(4,5);
echo $total;



pada fungsi di atas terdapat fungsi jumlahan yang menerima 2 parameter yaitu bil1, dan bil2. pada fungsi tersebut kedua bilangan itu dilakukan operasi penjumlahan dan hasilnya disimpan dalam variabel hasil.
pada kode diatas proses penjumlahan adalah bilangan 4 dan 5, hasilnya akan disimpan di variabel total.

PHP: Class PHP

Class sering disebut juga sebagai objek. Dengan menggunakan objek proses pembuatan program menjadi lebih cepat, karena objek-objek yang sudah ada dapat dipakai untuk membuat objek yang lain.

Objek dapat didefinisikan dengan sintaksis berikut:

class baru {
var $property;
function SetPro($arg) {
//definisi method;
}
}


sintaksis di atas mendefinisikan sebuah objek yang bernama "baru" dengan satu property $property dan satu method yaitu SetPro(). Pada method SetPro() juga terdapat satu paramater yang dipassing.

Untuk contoh lebih lanjut, kita akan membuat class buah yang warna dan berat buahnya bisa diganti-ganti. langsung saja dipraktekkan:

class Buah {
var $berat="2 kg";
var $warna="merah";
var $harga;

function UbahWarna($WarnaBaru) {
$this->warna=$WarnaBaru;
}

function UbahBerat($BeratBaru) {
$this->berat=$BeratBaru;
}
}
?>

nah... kemudian untuk contoh penggunaannya adalah, misalnya file diatas dinamai Buah.Class.php :

include("Buah.Class.php");
echo "

Buah";
$buah = new Buah;
echo "

Berat Buah: ".$buah->berat."
";
echo "Warna: ".$buah->warna."
";
echo "

Setelah perintah \"\$buah->UbahWarna(\"merah\")\"
";
$buah->UbahWarna("merah");
echo "Warna: ".$buah->warna."
";
?>

Monday, July 12

Javascript: Sorting dan Paging Data Table

Banyak cara untuk membuat tampilan tabel dalam website kita lebih menarik. dengan menambahkan fitur sorting, paging dan searching untuk data dalam tabel akan membuat pengunjung lebih mudah dalam mencari informasi yang ada.
Salah satu cara yang bisa digunakan adalah menggunakan Jquery, dan kali ini saya akan mencoba membicarakan tentang dataTables. dataTables adalah sebuah jQuery plugin yang bisa digunakan untuk mempercantik table dan menambahkan fitur2 yang lebih bermanfaat. contoh tampilannya adalah seperti ini:


untuk lebih jelasnya silahkan kunjungi langsung di dataTables

Javascript: Counting sampai bilangan tertentu

pada tulisan ini akan saya bahas tentang bagaimana caranya melakukan counting dari 0 sampai bilangan tertentu, tentunya dengan delay supaya terlihat iterasi penghitungannya.

fungsi javascript yang digunakan adalah sebagai berikut:

function incCounter() {
var currCount = parseInt($('#counter1').html());
$('#counter1').text(currCount+1);
if (currCount+1 != max) {
setTimeout('incCounter()',delay);
}

}


cara kerja fungsi javascript di atas adalah mengambil id dari html, kemudian diiterasi dengan delay tertentu. jika sudah sampai max maka iterasi berhenti.
berikut adalah contoh penggunaannya secara lengkap:



<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Penghitungan Suara</title>
<link rel="stylesheet" href="images/main.css" type="text/css" title="main" media="screen">
<style type="text/css">
<!--
.style1 {
font-size: 60px;
font-weight: bold;
padding-left:30px;

font-family: "Times New Roman", Times, serif;
margin-top:40px;
}
.style2 {

margin-top:20px;
}
-->
</style>
<script src="jquery.js"></script>

<script type="text/javascript">

var max = 9;// set with php

var delay = 500;

$(function(){

incCounter();

});
function incCounter() {
var currCount = parseInt($('#counter1').html());
$('#counter1').text(currCount+1);
if (currCount+1 != max) {
setTimeout('incCounter()',delay);
}
else{
index = index +1;
if(index < jumca) $('#td1').text('X');

}
}



</script>
</head>

<body>
<div align="center" class="style2">

<table width="568" >
<tr>
<td height="21" colspan="4">&nbsp;</td>

<tr>
<td height="132" valign="middle" ><span class="style1" id="counter1">0</span></td>

</tr>
</table>
</div>
</body>
</html>

PHP: koneksi ODBC

Bismillah... akhirnya nulis lagi setelah sekial lama menghilang karena liburan...hehe. kali ini saya akan menampilkan class PHP yang berfungsi untuk koneksi dengan ODBC (Class PHP ODBC). Nah class ini bisa di gunakan untuk memanipulasi database yang koneksinya menggunakan ODBC.


<?php

class odbc{

public $hostname;
public $username;
public $password;
public $database;

private $connection;
private $query;
private $res;

/*** default constructor รข�� ***/
public function __construct() {
$this->hostname = '';
$this->username = '';
$this->password = '';
$this->database = '';
}
/** **/
public function __set($nama, $nilai) {
switch ($nama) {
case "hostname" : $this->hostname = $nilai; break;
case "username" : $this->username = $nilai; break;
case "password" : $this->password = $nilai; break;
case "database" : $this->database = $nilai; break;
default:
throw new Exception('Error...');
}
}

/** adeh.. nie function connect dlm yg akan dipanggil oleh app **/

private function odbc_connect_func() {
if($this->connection = odbc_pconnect($this->hostname,$this->username,$this->password) ) /* $this->database akan guna nnti la yer.. */
return true;
else
return false;
}

public function connect() {
if ($this->odbc_connect_func() === false) {
throw new Exception('Error...!');
}
}

public function query($getQuery) {
if(!$this->res = odbc_exec($this->connection,$getQuery)) {
throw new Exception("Query gagal..".odbc_error());
return false;
}
else { return true; }
}

public function fetchall() {
$ret = array();
while($row = odbc_fetch_array($this->res)) {
$ret[] = $row;
}
return $ret;
}

public function numrow(){
$ret = 0;
}


}

?>


Nah, seperti itulah class nya. (download lengkap di sini) kemudian bagaimana cara menggunakannya akan saya bahas pada postingan selanjutnya... terimakasih.

Pencarian Google