Cara Mengelompokkan Data di PHP. Pengelompokkan data bertujuan mengumpulkan data menjadi satu kesatuan. Pada kasus Saya, saya membuat pengelompokkan data untuk mengklasifikasinya berdasarkan kondisi tertentu. Nah kali ini cara untuk menggabungkan data tersebut akan Saya share pada tutorial ini.
Untuk cara ini yaitu memanggil isi database menggunakan query. Kemudian menampilkannya kedalam tabel. Pada kasus saya, kondisi yang digunakan yaitu “kriteria”.
Script php pengelompokkan kata dari database
Sebagai contoh untuk menampilkan data kelompok caranya:
1. Buat database sebagai contoh database dengan nama “bobot_karyawan“.
2. Kemudian buat tabel database seperti yang saya sertakan berikut ini. Kemudian inport data dibawah.
-- phpMyAdmin SQL Dump -- version 3.2.0.1 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Sep 14, 2015 at 09:16 -- Server version: 5.1.37 -- PHP Version: 5.3.0 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; -- -- Database: `karyawan` -- -- -------------------------------------------------------- -- -- Table structure for table `bobot_karyawan` -- CREATE TABLE IF NOT EXISTS `bobot_karyawan` ( `id_bobotkaryawan` int(5) NOT NULL AUTO_INCREMENT, `kd_kriteria` varchar(2) NOT NULL, `kd_karyawan` varchar(3) NOT NULL, `bobot_karyawan` double NOT NULL, `token` varchar(20) NOT NULL, PRIMARY KEY (`id_bobotkaryawan`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=101 ; -- -- Dumping data for table `bobot_karyawan` -- INSERT INTO `bobot_karyawan` (`id_bobotkaryawan`, `kd_kriteria`, `kd_karyawan`, `bobot_karyawan`, `token`) VALUES (1, 'K1', 'P1', 0.0061711759100692, '2015-07-04:21:31:09'), (2, 'K1', 'P2', 0.019055051624931, '2015-07-04:21:31:09'), (3, 'K1', 'P3', 0.0078301298780784, '2015-07-04:21:31:09'), (4, 'K1', 'P4', 0.029667337034389, '2015-07-04:21:31:09'), (5, 'K1', 'P5', 0.074890534927952, '2015-07-04:21:31:09'), (6, 'K1', 'P6', 0.0061711759100692, '2015-07-04:21:31:09'), (7, 'K1', 'P7', 0.047320191616944, '2015-07-04:21:31:09'), (8, 'K1', 'P8', 0.074890534927952, '2015-07-04:21:31:09'), (9, 'K1', 'P9', 0.019055051624931, '2015-07-04:21:31:09'), (10, 'K1', 'P10', 0.029667337034389, '2015-07-04:21:31:09'), (11, 'K1', 'P11', 0.029667337034389, '2015-07-04:21:31:09'), (12, 'K1', 'P12', 0.074890534927952, '2015-07-04:21:31:09'), (13, 'K1', 'P13', 0.074890534927952, '2015-07-04:21:31:09'), (14, 'K1', 'P14', 0.11182881561219, '2015-07-04:21:31:09'), (15, 'K1', 'P15', 0.019055051624931, '2015-07-04:21:31:09'), (16, 'K1', 'P16', 0.047320191616944, '2015-07-04:21:31:09'), (17, 'K1', 'P17', 0.019055051624931, '2015-07-04:21:31:09'), (18, 'K1', 'P18', 0.029667337034389, '2015-07-04:21:31:09'), (19, 'K1', 'P19', 0.013333126668501, '2015-07-04:21:31:09'), (20, 'K1', 'P20', 0.047320191616944, '2015-07-04:21:31:09'), (21, 'K1', 'P21', 0.047320191616944, '2015-07-04:21:31:09'), (22, 'K1', 'P22', 0.047320191616944, '2015-07-04:21:31:09'), (23, 'K1', 'P23', 0.019055051624931, '2015-07-04:21:31:09'), (24, 'K1', 'P24', 0.029667337034389, '2015-07-04:21:31:09'), (25, 'K1', 'P25', 0.074890534927952, '2015-07-04:21:31:09'), (26, 'K2', 'P1', 0.0074422697905269, '2015-07-04:21:31:09'), (27, 'K2', 'P2', 0.031608237875238, '2015-07-04:21:31:09'), (28, 'K2', 'P3', 0.0097796473256747, '2015-07-04:21:31:09'), (29, 'K2', 'P4', 0.052726754225683, '2015-07-04:21:31:09'), (30, 'K2', 'P5', 0.052726754225683, '2015-07-04:21:31:09'), (31, 'K2', 'P6', 0.0074422697905269, '2015-07-04:21:31:09'), (32, 'K2', 'P7', 0.031608237875238, '2015-07-04:21:31:09'), (33, 'K2', 'P8', 0.052726754225683, '2015-07-04:21:31:09'), (34, 'K2', 'P9', 0.031608237875238, '2015-07-04:21:31:09'), (35, 'K2', 'P10', 0.019535164459413, '2015-07-04:21:31:09'), (36, 'K2', 'P11', 0.019535164459413, '2015-07-04:21:31:09'), (37, 'K2', 'P12', 0.11940846486008, '2015-07-04:21:31:09'), (38, 'K2', 'P13', 0.082643538324383, '2015-07-04:21:31:09'), (39, 'K2', 'P14', 0.082643538324383, '2015-07-04:21:31:09'), (40, 'K2', 'P15', 0.031608237875238, '2015-07-04:21:31:09'), (41, 'K2', 'P16', 0.031608237875238, '2015-07-04:21:31:09'), (42, 'K2', 'P17', 0.013356401292066, '2015-07-04:21:31:09'), (43, 'K2', 'P18', 0.019535164459413, '2015-07-04:21:31:09'), (44, 'K2', 'P19', 0.019535164459413, '2015-07-04:21:31:09'), (45, 'K2', 'P20', 0.031608237875238, '2015-07-04:21:31:09'), (46, 'K2', 'P21', 0.031608237875238, '2015-07-04:21:31:09'), (47, 'K2', 'P22', 0.082643538324383, '2015-07-04:21:31:09'), (48, 'K2', 'P23', 0.031608237875238, '2015-07-04:21:31:09'), (49, 'K2', 'P24', 0.052726754225683, '2015-07-04:21:31:09'), (50, 'K2', 'P25', 0.052726754225683, '2015-07-04:21:31:09'), (51, 'K3', 'P1', 0.0095592302165339, '2015-07-04:21:31:09'), (52, 'K3', 'P2', 0.05600206201429, '2015-07-04:21:31:09'), (53, 'K3', 'P3', 0.013663070514115, '2015-07-04:21:31:09'), (54, 'K3', 'P4', 0.089844601510945, '2015-07-04:21:31:09'), (55, 'K3', 'P5', 0.033620545617641, '2015-07-04:21:31:09'), (56, 'K3', 'P6', 0.0095592302165339, '2015-07-04:21:31:09'), (57, 'K3', 'P7', 0.020985915057252, '2015-07-04:21:31:09'), (58, 'K3', 'P8', 0.033620545617641, '2015-07-04:21:31:09'), (59, 'K3', 'P9', 0.05600206201429, '2015-07-04:21:31:09'), (60, 'K3', 'P10', 0.013663070514115, '2015-07-04:21:31:09'), (61, 'K3', 'P11', 0.033620545617641, '2015-07-04:21:31:09'), (62, 'K3', 'P12', 0.089844601510945, '2015-07-04:21:31:09'), (63, 'K3', 'P13', 0.05600206201429, '2015-07-04:21:31:09'), (64, 'K3', 'P14', 0.05600206201429, '2015-07-04:21:31:09'), (65, 'K3', 'P15', 0.05600206201429, '2015-07-04:21:31:09'), (66, 'K3', 'P16', 0.05600206201429, '2015-07-04:21:31:09'), (67, 'K3', 'P17', 0.033620545617641, '2015-07-04:21:31:09'), (68, 'K3', 'P18', 0.013663070514115, '2015-07-04:21:31:09'), (69, 'K3', 'P19', 0.033620545617641, '2015-07-04:21:31:09'), (70, 'K3', 'P20', 0.020985915057252, '2015-07-04:21:31:09'), (71, 'K3', 'P21', 0.020985915057252, '2015-07-04:21:31:09'), (72, 'K3', 'P22', 0.05600206201429, '2015-07-04:21:31:09'), (73, 'K3', 'P23', 0.013663070514115, '2015-07-04:21:31:09'), (74, 'K3', 'P24', 0.089844601510945, '2015-07-04:21:31:09'), (75, 'K3', 'P25', 0.033620545617641, '2015-07-04:21:31:09'), (76, 'K4', 'P1', 0.012376243110747, '2015-07-04:21:31:09'), (77, 'K4', 'P2', 0.073318361393566, '2015-07-04:21:31:09'), (78, 'K4', 'P3', 0.018908442378627, '2015-07-04:21:31:09'), (79, 'K4', 'P4', 0.10789357245507, '2015-07-04:21:31:09'), (80, 'K4', 'P5', 0.018908442378627, '2015-07-04:21:31:09'), (81, 'K4', 'P6', 0.012376243110747, '2015-07-04:21:31:09'), (82, 'K4', 'P7', 0.012376243110747, '2015-07-04:21:31:09'), (83, 'K4', 'P8', 0.018908442378627, '2015-07-04:21:31:09'), (84, 'K4', 'P9', 0.073318361393566, '2015-07-04:21:31:09'), (85, 'K4', 'P10', 0.0087944866139528, '2015-07-04:21:31:09'), (86, 'K4', 'P11', 0.047705667856238, '2015-07-04:21:31:09'), (87, 'K4', 'P12', 0.047705667856238, '2015-07-04:21:31:09'), (88, 'K4', 'P13', 0.030171364727471, '2015-07-04:21:31:09'), (89, 'K4', 'P14', 0.030171364727471, '2015-07-04:21:31:09'), (90, 'K4', 'P15', 0.073318361393566, '2015-07-04:21:31:09'), (91, 'K4', 'P16', 0.073318361393566, '2015-07-04:21:31:09'), (92, 'K4', 'P17', 0.047705667856238, '2015-07-04:21:31:09'), (93, 'K4', 'P18', 0.0087944866139528, '2015-07-04:21:31:09'), (94, 'K4', 'P19', 0.047705667856238, '2015-07-04:21:31:09'), (95, 'K4', 'P20', 0.030171364727471, '2015-07-04:21:31:09'), (96, 'K4', 'P21', 0.030171364727471, '2015-07-04:21:31:09'), (97, 'K4', 'P22', 0.030171364727471, '2015-07-04:21:31:09'), (98, 'K4', 'P23', 0.018908442378627, '2015-07-04:21:31:09'), (99, 'K4', 'P24', 0.10789357245507, '2015-07-04:21:31:09'), (100, 'K4', 'P25', 0.018908442378627, '2015-07-04:21:31:09');
3. Buat file untuk menampilkan data kelompok. Misalkan saja “data.php“. Dan isikan script berikut ini.Berikut ini script phpnya:
' AND kd_kriteria='$kd_kriteria[$i]' ORDER BY bobot_karyawan DESC"; $sql = mysql_query($tampil); while($data = mysql_fetch_array($sql)) { $j++; echo " <td>$j</td> <td>$data[kd_karyawan]</td> <td>$data[bobot_karyawan]</td> </tr>"; } echo '</table>'; } ?>
4. Dan yang terakhir untuk mengakses data tersebut silahkan akses menggunakan url file yang ada pada direktori anda letakkan, dengan cara menambahkan pada akhir :
data.php?kriteria=K1&token=2015-07-04:21:31:09
Dan berikut ini hasil pengelompokkan data untuk 2 contoh kriteria:
Pengelompokkan data K1
Pengelompokkan data K2
Pada link diatas untuk kriteria bisa di ganti dengan K2 dan seterusnya. Untuk token dan kriteria sesuai yang ada di database. Oke begitulah cara panggil database dengan mengelompokkan berdasarkan kondisi yang tertentu dan yang kita mau.