SHOW:
|
|
- or go back to the newest paste.
| 1 | - | function filter_table($kota,$usia,$profesi,$jeniskelamin){
|
| 1 | + | <?php |
| 2 | - | if ($kota == 'semuakota' && $usia == 'semuausia' && $jeniskelamin == 'semuajeniskelamin' && $profesi == 'semuaprofesi') {
|
| 2 | + | |
| 3 | - | $query = "SELECT tbl_polling.image as Gambar, tbl_polling.nama as Nama , count(*) as Jumlah from tbl_choice inner join tbl_polling on tbl_choice.pilihan=tbl_polling.id group by tbl_choice.pilihan order by Jumlah desc"; |
| 3 | + | //helper function |
| 4 | - | $result = $this->mysqli->query($query); |
| 4 | + | function prepare_mysqli_where_helper($param_type, $conditions) {
|
| 5 | - | } |
| 5 | + | $get_condition_values = function($key) use ($conditions) {
|
| 6 | return $conditions[$key]; | |
| 7 | - | else if ($kota != 'semuakota' && $usia != 'semuausia' && $jeniskelamin != 'semuajeniskelamin' && $profesi == 'semuaprofesi'){
|
| 7 | + | }; |
| 8 | - | $query = "SELECT tbl_polling.image as Gambar, tbl_polling.nama as Nama , count(*) as Jumlah from tbl_choice inner join tbl_polling on tbl_choice.pilihan=tbl_polling.id where kota = '$kota' and usia = '$usia' and jk = '$jeniskelamin' group by tbl_choice.pilihan order by Jumlah desc"; |
| 8 | + | |
| 9 | - | $result = $this->mysqli->query($query); |
| 9 | + | $keys = array_keys($param_type); |
| 10 | - | } |
| 10 | + | $condition_query = array(); |
| 11 | foreach ($keys as $column_name) {
| |
| 12 | - | else if ($kota != 'semuakota' && $usia != 'semuausia' && $jeniskelamin == 'semuajeniskelamin' && $profesi != 'semuaprofesi'){
|
| 12 | + | $condition_query[] = "`{$column_name}` = ?";
|
| 13 | - | $query = "SELECT tbl_polling.image as Gambar, tbl_polling.nama as Nama , count(*) as Jumlah from tbl_choice inner join tbl_polling on tbl_choice.pilihan=tbl_polling.id where kota = '$kota' and usia = '$usia' and profesi = '$profesi' group by tbl_choice.pilihan order by Jumlah desc"; |
| 13 | + | } |
| 14 | - | $result = $this->mysqli->query($query); |
| 14 | + | |
| 15 | - | } |
| 15 | + | $where_condition = implode(' AND ', $condition_query);
|
| 16 | $types = implode('', array_values($param_type));
| |
| 17 | - | else if ($kota != 'semuakota' && $usia == 'semuausia' && $jeniskelamin != 'semuajeniskelamin' && $profesi != 'semuaprofesi'){
|
| 17 | + | $values = array_map($get_condition_values, $keys); |
| 18 | - | $query = "SELECT tbl_polling.image as Gambar, tbl_polling.nama as Nama , count(*) as Jumlah from tbl_choice inner join tbl_polling on tbl_choice.pilihan=tbl_polling.id where kota = '$kota' and profesi = '$profesi' and jk = '$jeniskelamin' group by tbl_choice.pilihan order by Jumlah desc"; |
| 18 | + | $types_and_values = array_merge([$types], $values); |
| 19 | - | $result = $this->mysqli->query($query); |
| 19 | + | |
| 20 | - | } |
| 20 | + | return compact("where_condition", "types", "values", "types_and_values");
|
| 21 | } | |
| 22 | - | else if ($kota == 'semuakota' && $usia != 'semuausia' && $jeniskelamin != 'semuajeniskelamin' && $profesi != 'semuaprofesi'){
|
| 22 | + | |
| 23 | - | $query = "SELECT tbl_polling.image as Gambar, tbl_polling.nama as Nama , count(*) as Jumlah from tbl_choice inner join tbl_polling on tbl_choice.pilihan=tbl_polling.id where usia = '$usia' and profesi = '$profesi' and jk = '$jeniskelamin' group by tbl_choice.pilihan order by Jumlah desc"; |
| 23 | + | function refValues($arr) {
|
| 24 | - | $result = $this->mysqli->query($query); |
| 24 | + | if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+ |
| 25 | - | } |
| 25 | + | {
|
| 26 | $refs = array(); | |
| 27 | - | else if ($kota == 'semuakota' && $usia == 'semuausia' && $jeniskelamin == 'semuajeniskelamin' && $profesi != 'semuaprofesi'){
|
| 27 | + | foreach($arr as $key => $value) |
| 28 | - | $query = "SELECT tbl_polling.image as Gambar, tbl_polling.nama as Nama , count(*) as Jumlah from tbl_choice inner join tbl_polling on tbl_choice.pilihan=tbl_polling.id where profesi = '$profesi' group by tbl_choice.pilihan order by Jumlah desc"; |
| 28 | + | $refs[$key] = &$arr[$key]; |
| 29 | - | $result = $this->mysqli->query($query); |
| 29 | + | return $refs; |
| 30 | - | } |
| 30 | + | } |
| 31 | return $arr; | |
| 32 | - | else if ($kota == 'semuakota' && $usia == 'semuausia' && $jeniskelamin != 'semuajeniskelamin' && $profesi == 'semuaprofesi'){
|
| 32 | + | } |
| 33 | - | $query = "SELECT tbl_polling.image as Gambar, tbl_polling.nama as Nama , count(*) as Jumlah from tbl_choice inner join tbl_polling on tbl_choice.pilihan=tbl_polling.id where jk = '$jeniskelamin' group by tbl_choice.pilihan order by Jumlah desc"; |
| 33 | + | |
| 34 | - | $result = $this->mysqli->query($query); |
| 34 | + | function filter_table($kota,$usia,$profesi,$jeniskelamin) {
|
| 35 | - | } |
| 35 | + | $main_query = "SELECT tbl_polling.image AS Gambar, tbl_polling.nama AS Nama , COUNT(*) AS Jumlah FROM tbl_choice INNER JOIN tbl_polling ON tbl_choice.pilihan=tbl_polling.id"; |
| 36 | $group_order = "GROUP BY tbl_choice.pilihan ORDER BY Jumlah DESC"; | |
| 37 | - | else if ($kota == 'semuakota' && $usia != 'semuausia' && $jeniskelamin == 'semuajeniskelamin' && $profesi == 'semuaprofesi'){
|
| 37 | + | $conditions = array(); |
| 38 | - | $query = "SELECT tbl_polling.image as Gambar, tbl_polling.nama as Nama , count(*) as Jumlah from tbl_choice inner join tbl_polling on tbl_choice.pilihan=tbl_polling.id where usia = '$usia' group by tbl_choice.pilihan order by Jumlah desc"; |
| 38 | + | $param_type = array(); |
| 39 | - | $result = $this->mysqli->query($query); |
| 39 | + | |
| 40 | - | } |
| 40 | + | if ($kota != 'semuakota') {
|
| 41 | $conditions["kota"] = $kota; | |
| 42 | - | else if ($kota != 'semuakota' && $usia == 'semuausia' && $jeniskelamin == 'semuajeniskelamin' && $profesi == 'semuaprofesi'){
|
| 42 | + | $param_type["kota"] = 's'; |
| 43 | - | $query = "SELECT tbl_polling.image as Gambar, tbl_polling.nama as Nama , count(*) as Jumlah from tbl_choice inner join tbl_polling on tbl_choice.pilihan=tbl_polling.id where kota = '$kota' group by tbl_choice.pilihan order by Jumlah desc"; |
| 43 | + | } |
| 44 | - | $result = $this->mysqli->query($query); |
| 44 | + | if ($usia != 'semuausia') {
|
| 45 | - | } |
| 45 | + | $conditions["usia"] = $usia; |
| 46 | $param_type["usia"] = 's'; | |
| 47 | - | else if ($kota != 'semuakota' && $usia != 'semuausia' && $jeniskelamin == 'semuajeniskelamin' && $profesi == 'semuaprofesi'){
|
| 47 | + | } |
| 48 | - | $query = "SELECT tbl_polling.image as Gambar, tbl_polling.nama as Nama , count(*) as Jumlah from tbl_choice inner join tbl_polling on tbl_choice.pilihan=tbl_polling.id where kota = '$kota' and usia = '$usia' group by tbl_choice.pilihan order by Jumlah desc"; |
| 48 | + | if ($jeniskelamin != 'semuajeniskelamin') {
|
| 49 | - | $result = $this->mysqli->query($query); |
| 49 | + | $conditions["jk"] = $jeniskelamin; |
| 50 | - | } |
| 50 | + | $param_type["jk"] = 's'; |
| 51 | } | |
| 52 | - | else if ($kota == 'semuakota' && $usia == 'semuausia' && $jeniskelamin != 'semuajeniskelamin' && $profesi != 'semuaprofesi'){
|
| 52 | + | if ($profesi != 'semuaprofesi') {
|
| 53 | - | $query = "SELECT tbl_polling.image as Gambar, tbl_polling.nama as Nama , count(*) as Jumlah from tbl_choice inner join tbl_polling on tbl_choice.pilihan=tbl_polling.id where jk = '$jeniskelamin' and profesi = '$profesi' group by tbl_choice.pilihan order by Jumlah desc"; |
| 53 | + | $conditions["profesi"] = $profesi; |
| 54 | - | $result = $this->mysqli->query($query); |
| 54 | + | $param_type["profesi"] = 's'; |
| 55 | - | } |
| 55 | + | } |
| 56 | //call helper | |
| 57 | - | else if ($kota == 'semuakota' && $usia != 'semuausia' && $jeniskelamin == 'semuajeniskelamin' && $profesi != 'semuaprofesi'){
|
| 57 | + | $helper = prepare_mysqli_where_helper($param_type, $conditions); |
| 58 | - | $query = "SELECT tbl_polling.image as Gambar, tbl_polling.nama as Nama , count(*) as Jumlah from tbl_choice inner join tbl_polling on tbl_choice.pilihan=tbl_polling.id where usia = '$usia' and profesi = '$profesi' group by tbl_choice.pilihan order by Jumlah desc"; |
| 58 | + | //build query |
| 59 | - | $result = $this->mysqli->query($query); |
| 59 | + | $query = $main_query; |
| 60 | - | } |
| 60 | + | if (count($conditions) > 0) {
|
| 61 | $query .= ' WHERE ' . $helper['where_condition']; | |
| 62 | - | else if ($kota != 'semuakota' && $usia == 'semuausia' && $jeniskelamin != 'semuajeniskelamin' && $profesi == 'semuaprofesi'){
|
| 62 | + | } |
| 63 | - | $query = "SELECT tbl_polling.image as Gambar, tbl_polling.nama as Nama , count(*) as Jumlah from tbl_choice inner join tbl_polling on tbl_choice.pilihan=tbl_polling.id where kota = '$kota' and jk = '$jeniskelamin' group by tbl_choice.pilihan order by Jumlah desc"; |
| 63 | + | $query .= $group_order; |
| 64 | - | $result = $this->mysqli->query($query); |
| 64 | + | //mysqli statement prepare and bind params |
| 65 | - | } |
| 65 | + | $stmt = $mysqli->prepare($query); |
| 66 | call_user_func_array(array($stmt, "bind_param"), refValues($helper['types_and_values'])); | |
| 67 | - | else if ($kota != 'semuakota' && $usia == 'semuausia' && $jeniskelamin == 'semuajeniskelamin' && $profesi != 'semuaprofesi'){
|
| 67 | + | //execute and return |
| 68 | - | $query = "SELECT tbl_polling.image as Gambar, tbl_polling.nama as Nama , count(*) as Jumlah from tbl_choice inner join tbl_polling on tbl_choice.pilihan=tbl_polling.id where kota = '$kota' and profesi = '$profesi' group by tbl_choice.pilihan order by Jumlah desc"; |
| 68 | + | $result = $stmt->execute(); |
| 69 | - | $result = $this->mysqli->query($query); |
| 69 | + | return $result; |
| 70 | - | } |
| 70 | + | } |