Jumat, 09 November 2012

Aplikasi Sederhana PHP-MySQL (Select, Insert, Update, Delete, Searching, Paging) menggunakan Konsep OOP

Aplikasi Sederhana PHP-MySQL (Select, Insert, Update, Delete, Searching, Paging) menggunakan Konsep OOP

Bagi Programmer yang baru menginjakan di Dunia PHP, atau bahasa pemrograman lainnya, hal yang pertama dicari adalah mengetahui sifat-sifat dari bahasa pemrograman tersebut. Kemudian, bagaimana cara nya mengakses Database, menampilkan data, menyimpan data, merubah data, menghapus data, melakukan pencarian, dan membuat paging.
Kali ini kita akan coba membuat aplikasi sederhana menggunakan PHP-MySQL dengan kasus seperti diatas. Kurang lebih memiliki tampilan seperti dibawah :
Aplikasi PHP MySQL
Sebelumnya, kita lihat artikel ini , karena kita akan menggunakan konsep OOP dalam melakukan akses database. untuk script lengkap bisa dilihat disini :
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
    $filename = 'database.class.php';
    if (!file_exists($filename)){
        echo "File Konfigurasi tidak ditemukan";
        die();
    }
    include $filename;
    $cn = new mySql();
    if(!$cn->openKoneksi()){
        echo "Tidak Bisa Konek";
        die();
    }
    if (!isset($_REQUEST["mode"])){
        $action = "simpan";
    }else{
        $kodeBank = $_POST["tID"];
        $namaBank = $_POST["tNama"];
        switch ($_REQUEST["mode"]) {
            case "simpan":
                $action = "simpan";
                $values = "'".$kodeBank."','".$namaBank."'";
                $field = "kodeBank,namaBank";
                if($cn->insertRows('tRefBank',$field,$values)){
                    echo "Data Tersimpan";
                }
            break;
            case "edit":
                $action ="update";
                $sql = "SELECT * FROM tRefBank WHERE kodeBank='".$_REQUEST["kodeBank"]."'";
                $exec = $cn->execute($sql);
                while ($row = $cn->getArray()){
                    $kodeBank = $row[0];
                    $namaBank = $row[1];
                }
            break;
            case "update":
                /* script insert */
                $action = "simpan";
                $where = "kodeBank='".$kodeBank."'";
                $field = "namaBank='".$namaBank."'";
                if($cn->updateRows('tRefBank',$field,$where)){
                    echo "Data Tersimpan";
                }
                $kodeBank = "";
                $namaBank = "";
                break;
            case "hapus":
                $action = "simpan";
                $where = "kodeBank='".$_REQUEST["kodeBank"]."'";
                $cn->deleteRows('tRefBank',$where);
            break;
        }
    }
    /* script update */
    /* script delete */
    /* script search */
?>
<a href="<?php echo "http://".$_SERVER['SERVER_NAME']."/_test-script/paging/";?>">REFRESH/HOME</a>
<hr/>
<form method="POST" action="<?php echo $PHP_SELF;?>?mode=<?php echo $action;?>">
<table>
    <tr>
        <td>ID Bank</td>
        <td>:</td>
        <td><input type="text" name="tID" id="tID" value="<?php echo $kodeBank;?>"  /></td>
    </tr>
    <tr>
        <td>Nama Bank</td>
        <td>:</td>
        <td><input type="text" name="tNama" id="tNama" value="<?php echo $namaBank;?>" /></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td><input type="submit" value="simpan" /></td>
    </tr>
</table>
</form>
<?php /*******************************************************************************/ ?>
<?php
?>
<table>
    <tr>
        <td>
            <form action="<?php $PHP_SELF;?>" method="get">
                <input type="text" name="namaBank"/><input type="submit" value="cari !"/>
            </form>
        </td>
    </tr>
</table>
<table width="50%">
    <tr>
        <td>NO</td>
        <td>ID BANK</td>
        <td>NAMA BANK</td>
        <td>AKSI</td>
    </tr>
    <tr>
        <td colspan="4"><hr/></td>
    </tr>
    <?php
        $limit  = 5;
        if (isset($_REQUEST["namaBank"])){
            $cari = "WHERE namaBank LIKE '%".$_REQUEST["namaBank"]."%'";
        }else{
            $cari = "";
        }
        $sql = "SELECT * FROM tRefBank ".$cari;
        $exec = $cn->execute($sql);
        $totalRecords = $cn->record_count();
        if (!isset($_REQUEST["page"]) || trim($_REQUEST["page"]="") ){
            $offset = 0;
        }else{
            $offset =$_REQUEST["offset"];
        }
        //$offset = (isset($_REQUEST["page"])? $_REQUEST["offset"]:0);
        $sql = "SELECT * FROM tRefBank ".$cari." LIMIT ".$offset.", ".$limit;
        $exec = $cn->execute($sql);
        $i = $offset;
        while($r = $cn->getArray()):
        $i++;
    ?>
    <tr>
        <td><?php echo $i;?></td>
        <td><?php echo $r[0];?></td>
        <td><?php echo $r[1];?></td>
        <td><a href="<?php echo $PHP_SELF;?>?mode=edit&kodeBank=<?php echo $r[0];?>">EDIT</a> - <a href="<?php echo $PHP_SELF;?>?mode=hapus&kodeBank=<?php echo $r[0];?>">HAPUS</a></td>
    </tr>
    <?php endwhile; ?>
    <tr>
        <td colspan="4">
            <?php echo $cn->recordsetNav($totalRecords, $PHP_SELF."?namaBank=".$_REQUEST["namaBank"]."&page=".$offset,$offset,$limit,'10%','yes');?>
        </td>
    </tr>
</table>
Sementara berbeda dengan class yang diberikan pada artikel sebelumnya, kita tambahkan fungsi pagination :
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function recordsetNav($totalrecords,$page_url,$offset=0,$limit=0,$tablewidth='25%',$verbiage='yes'){
                # EXAMPLE USAGE: recordsetNav($mysql_link,$users_query,$PHP_SELF,$offset,$limit,'75%',0); #
                $pagenumber = ($offset + $limit) / $limit;
            $totalpages = intval($totalrecords/$limit);
            if ($totalrecords%$limit > 0) $totalpages++;
            $navstring  = "<table align=\"center\" width=\"".$tablewidth."\" cellspacing=\"0\" cellpadding=\"1\" border=\"0\" align=center>";
            // only show <<PREV NEXT>> row if $totalrecords is greater than $limit
            if ($totalrecords > $limit){
                $navstring .= "<tr>";
                if ($offset != 0){
                    $navstring .= "<td valign='middle' width='25%' nowrap><a class='small' href='".$page_url."&offset=".($offset-$limit)."'> &lt;&lt; PREVIOUS  </a></td>";
                }else{
                    $navstring .= "<td width='25%' nowrap>&nbsp;</td>";
                }
                if($totalrecords-$offset <= $limit){
                    $navstring .= "<td width='25%' nowrap>&nbsp;</td>";
                }else{
                    $navstring .= "<td align='right' valign='middle' width='25%' nowrap border=0><a class='small' href='".$page_url."&offset=".($offset+$limit)."'> NEXT &gt;&gt; </a></td>";
                }
                $navstring .= "</tr>";
            }
            //$navstring .= "<tr><td colspan='3' align='center'>&nbsp;</td></tr>";
            if ($verbiage=="no"){
                $navstring .= "";
            }else{
                $navstring .= "<tr><td colspan='3' align='center' nowrap>";
                $navstring .= "<span class='RGBsmall'>Pages: <b>".$pagenumber."</b>/<b>".$totalpages."</b>";
                $navstring .= "&nbsp;&nbsp; total Record(s): <b>".$totalrecords."</b></span>";
                $navstring .= "</td></tr>";
            }
            $navstring .= "</table>";
            return $navstring;
            }

Comments

Tidak ada komentar:

Posting Komentar