Advertisement
Mujiburrohman

API CRUD, Register, Login, Upload foto Using Codeigniter

Apr 16th, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 20.41 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. // if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  4.  
  5. class Api extends CI_Controller {
  6.  
  7.  
  8. // Ini Api Untuk Panggil data ya di insert
  9. // ini untuk GET data tb_siswa
  10. function get_siswa(){
  11.     $this->db->SELECT("nama, kelas, alamat, umur, tgl_lahir, CONCAT('https://lombaidn.000webhostapp.com/server_sekolah/foto/Siswa/', foto)as foto");
  12.     $this->db->from('tb_siswa');
  13.     $hasil = $this->db->get();
  14.  
  15.     // cek kondisi ada datanya apa gak
  16.     if ($hasil -> num_rows() > 0) {
  17.         // Bikin response
  18.         $data["pesan"] = "Data Ada";
  19.         $data["sukses"] = true;
  20.         $data['data'] = $hasil->result();
  21.     }else{
  22.         $data["pesan"] = "Data Tidak Ditemukan";
  23.         $data["sukses"] = false;
  24.     }
  25.     echo json_encode($data);
  26. }
  27. function get_users(){
  28.     $this->db->SELECT("nama, username, no_telp, alamat, email, jenis_kelamin,CONCAT('https://lombaidn.000webhostapp.com/server_sekolah/foto/', foto) as foto");
  29.     $this->db->from('tb_user');
  30.     $hasil = $this->db->get();
  31.     // cek kondisi ada datanya apa gak
  32.     if ($hasil -> num_rows() > 0) {
  33.         // Bikin response
  34.         $data["pesan"] = "Data Ada";
  35.         $data["sukses"] = true;
  36.         $data['data'] = $hasil->result();
  37.     }else{
  38.         $data["pesan"] = "Data Tidak Ditemukan";
  39.         $data["sukses"] = false;
  40.     }
  41.     echo json_encode($data);
  42.  
  43.  
  44. }  
  45. // ini untuk GET data tb_berita
  46. function get_berita(){
  47.     $this->db->SELECT("judul, konten, tgl_terbit, penerbit,CONCAT('https://lombaidn.000webhostapp.com/server_sekolah/foto/berita/', gambar)as gambar");
  48.     $this->db->from('tb_berita');
  49.     $hasil = $this->db->get();
  50.  
  51.     if ($hasil -> num_rows() > 0) {
  52.         //Bikin response
  53.         $data["pesan"] = "Data Ada";
  54.         $data["sukses"] = true;
  55.         $data["data"] = $hasil->result();
  56.     }else{
  57.         $data["pesan"] = "Data Tidak Ditemukan";
  58.         $data["sukses"] = false;
  59.     }
  60.     echo json_encode($data);
  61. }
  62. // ini untuk GET data tb_kegiatan
  63. function get_kegiatan(){
  64.     $this->db->SELECT("nama_kegiatan, tujuan, keterangan, jam, tgl, lokasi, CONCAT('https://lombaidn.000webhostapp.com/server_sekolah/foto/Kegiatan/', foto) as foto");
  65.     $this->db->from('tb_kegiatan');
  66.     $hasil = $this->db->get();
  67.  
  68.     if ($hasil -> num_rows() > 0) {
  69.         //Bikin response
  70.         $data["pesan"] = "Data Ada";
  71.         $data["sukses"] = true;
  72.         $data["data"] = $hasil->result();
  73.     }else{
  74.         $data["pesan"] = "Data Tidak Ditemukan";
  75.         $data["sukses"] = false;
  76.     }
  77.     echo json_encode($data);
  78. }
  79. // ini untuk GET data tb_laporan
  80. function get_laporan(){
  81.     $this->db->SELECT("nama, kelas, wali, poin, melanggar, keterangan, tgl_lapor, pelapor, CONCAT('https://lombaidn.000webhostapp.com/server_sekolah/foto/Laporan/', foto) as foto");
  82.     $this->db->from('tb_laporan');
  83.     $hasil = $this->db->get();
  84.  
  85.     if ($hasil -> num_rows() > 0) {
  86.         //Bikin response
  87.         $data["pesan"] = "Data Ada";
  88.         $data["sukses"] = true;
  89.         $data["data"] = $hasil->result();
  90.     }else{
  91.         $data["pesan"] = "Data Tidak Ditemukan";
  92.         $data["sukses"] = false;
  93.     }
  94.     echo json_encode($data);
  95. }
  96. // ini untuk GET data tb_guru
  97. function get_guru(){
  98.     $this->db->SELECT("nama, umur, tgl_lahir, no_telp, alamat, CONCAT('https://lombaidn.000webhostapp.com/server_sekolah/foto/Guru/', foto) as foto");
  99.     $this->db->from('tb_guru');
  100.     $hasil = $this->db->get();
  101.  
  102.     // cek kondisi ada datanya apa gak
  103.     if ($hasil -> num_rows() > 0) {
  104.         // Bikin response
  105.         $data["pesan"] = "Data Ada";
  106.         $data["sukses"] = true;
  107.         $data['data'] = $hasil->result();
  108.     }else{
  109.         $data["pesan"] = "Data Tidak Ditemukan";
  110.         $data["sukses"] = false;
  111.     }
  112.     echo json_encode($data);
  113. }  
  114. // ini untuk GET data tb_guru
  115. function get_peraturan(){
  116.     $hasil = $this->db->get("tb_peraturan");
  117.  
  118.     // cek kondisi ada datanya apa gak
  119.     if ($hasil -> num_rows() > 0) {
  120.         // Bikin response
  121.         $data["pesan"] = "Data Ada";
  122.         $data["sukses"] = true;
  123.         $data['data'] = $hasil->result();
  124.     }else{
  125.         $data["pesan"] = "Data Tidak Ditemukan";
  126.         $data["sukses"] = false;
  127.     }
  128.     echo json_encode($data);
  129. }  
  130.  
  131. // ++++++++++++++++++++++++Membuat Fungsi Menghapus Data++++++++++++++++++++++++++
  132.  
  133. // // membuat fungsi delete untuk tb_siswa
  134. // function hapus_siswa($id){
  135.  
  136. //  $this->db->where('id',$id);
  137.    
  138.  
  139. //  $getId = $this->db->get('tb_siswa');
  140. //  if ($getId -> num_rows() == 0) {
  141. //      $data['sukses'] = false;
  142. //      $data['pesan'] = "datanya belum ada tidak Bisa Hapus";
  143.  
  144.  
  145. //  }else{
  146.  
  147. //  $this->db->where('id',$id);
  148.  
  149.  
  150. //  $hasil = $this->db->delete('tb_siswa');
  151. //  if ($hasil ) {
  152. //      // Bikin respones k emobile
  153. //      $data["pesan"] = "Berhasil Hapus Data";
  154. //      $data["sukses"] = true;
  155.  
  156. //  }else{
  157. //      $data["pesan"] = "datanya tidak bisa dihapus";
  158. //      $data["sukses"] = false;
  159. //  }
  160. // }       
  161. //  echo json_encode($data);
  162. // }   
  163. // // membuat fungsi delete untuk tb_berita
  164. // function hapus_berita($id){
  165.  
  166. //  $this->db->where('id',$id);
  167.  
  168. //  $getId = $this->db->get('tb_berita');
  169. //  if ($getId -> num_rows() == 0) {
  170. //      $data['sukses'] = false;
  171. //      $data['pesan'] = "beritanya belum ada tidak Bisa Hapus";
  172.  
  173.  
  174. //  }else{
  175.  
  176. //  $this->db->where('id',$id);
  177.  
  178.  
  179. //  $hasil = $this->db->delete('tb_berita');
  180. //  if ($hasil ) {
  181. //      // Bikin respones k emobile
  182. //      $data["pesan"] = "Berhasil Hapus Data Bang";
  183. //      $data["sukses"] = true;
  184.  
  185. //  }else{
  186. //      $data["pesan"] = "beritanya tidak bisa dihapus";
  187. //      $data["sukses"] = false;
  188. //  }
  189. // }       
  190. //  echo json_encode($data);
  191. // }   
  192. // // membuat fungsi delete untuk tb_kegiatan
  193. // function hapus_kegiatan($id){
  194.  
  195. //  $this->db->where('id',$id);
  196.    
  197.  
  198. //  $getId = $this->db->get('tb_kegiatan');
  199. //  if ($getId -> num_rows() == 0) {
  200. //      $data['sukses'] = false;
  201. //      $data['pesan'] = "datanya belum ada, tidak Bisa Hapus";
  202.  
  203.  
  204. //  }else{
  205.  
  206. //  $this->db->where('id',$id);
  207.  
  208.  
  209. //  $hasil = $this->db->delete('tb_kegiatan');
  210. //  if ($hasil ) {
  211. //      // Bikin respones k emobile
  212. //      $data["pesan"] = "Berhasil Hapus kegiatan";
  213. //      $data["sukses"] = true;
  214.  
  215. //  }else{
  216. //      $data["pesan"] = "kegiatan tidak bisa dihapus";
  217. //      $data["sukses"] = false;
  218. //  }
  219. // }       
  220. //  echo json_encode($data);
  221. // }   
  222. // // membuat fungsi delete untuk tb_laporan
  223. // function hapus_laporan($id){
  224.  
  225. //  $this->db->where('id',$id);
  226.    
  227.  
  228. //  $getId = $this->db->get('tb_laporan');
  229. //  if ($getId -> num_rows() == 0) {
  230. //      $data['sukses'] = false;
  231. //      $data['pesan'] = "laporan belum ada, tidak Bisa Hapus";
  232.  
  233.  
  234. //  }else{
  235.  
  236. //  $this->db->where('id',$id);
  237.  
  238.  
  239. //  $hasil = $this->db->delete('tb_laporan');
  240. //  if ($hasil ) {
  241. //      // Bikin respones k emobile
  242. //      $data["pesan"] = "Berhasil Hapus laporan";
  243. //      $data["sukses"] = true;
  244.  
  245. //  }else{
  246. //      $data["pesan"] = "laporan tidak bisa dihapus";
  247. //      $data["sukses"] = false;
  248. //  }
  249. // }       
  250. //  echo json_encode($data);
  251. // }   
  252. // // membuat fungsi delete untuk tb_guru
  253. // function hapus_guru($id){
  254.  
  255. //  $this->db->where('id',$id);
  256.    
  257.  
  258. //  $getId = $this->db->get('tb_guru');
  259. //  if ($getId -> num_rows() == 0) {
  260. //      $data['sukses'] = false;
  261. //      $data['pesan'] = "datanya belum ada tidak Bisa Hapus";
  262.  
  263.  
  264. //  }else{
  265.  
  266. //  $this->db->where('id',$id);
  267.  
  268.  
  269. //  $hasil = $this->db->delete('tb_guru');
  270. //  if ($hasil ) {
  271. //      // Bikin respones k emobile
  272. //      $data["pesan"] = "Berhasil Hapus Data";
  273. //      $data["sukses"] = true;
  274.  
  275. //  }else{
  276. //      $data["pesan"] = "datanya tidak bisa dihapus";
  277. //      $data["sukses"] = false;
  278. //  }
  279. // }       
  280. //  echo json_encode($data);
  281. // }   
  282. // // membuat fungsi delete untuk tb_peraturan
  283. // function hapus_peraturan($id){
  284.  
  285. //  $this->db->where('id',$id);
  286.    
  287.  
  288. //  $getId = $this->db->get('tb_peraturan');
  289. //  if ($getId -> num_rows() == 0) {
  290. //      $data['sukses'] = false;
  291. //      $data['pesan'] = "datanya belum ada tidak Bisa Hapus";
  292.  
  293.  
  294. //  }else{
  295.  
  296. //  $this->db->where('id',$id);
  297.  
  298.  
  299. //  $hasil = $this->db->delete('tb_peraturan');
  300. //  if ($hasil ) {
  301. //      // Bikin respones k emobile
  302. //      $data["pesan"] = "Berhasil Hapus Data";
  303. //      $data["sukses"] = true;
  304.  
  305. //  }else{
  306. //      $data["pesan"] = "datanya tidak bisa dihapus";
  307. //      $data["sukses"] = false;
  308. //  }
  309. // }       
  310. //  echo json_encode($data);
  311. // }   
  312.  
  313. // ++++++++++++++++++++++++++++Batas Fungsi Menghapus Data+++++++++++++++++++++++++++++++
  314.  
  315. // Membuat post di tb_siswa / INSERT
  316. function tambah_siswa(){
  317. // Variabel inputan mobile
  318.     $nama       = $this->input->post("nama");
  319.     $kelas      = $this->input->post("kelas");
  320.     $alamat     = $this->input->post("alamat");
  321.     $umur       = $this->input->post("umur");
  322.     $tgl_lahir  = $this->input->post("tgl_lahir");
  323.  
  324.  
  325.     $config['upload_path']          = './foto/';
  326.     $config['allowed_types']        = 'gif|jpg|png';
  327.     $config['max_size']             = 1000;
  328.     $config['max_width']            = 1024;
  329.     $config['max_height']           = 768;
  330.  
  331.     $this->load->library('upload', $config);
  332.  
  333.     if ( ! $this->upload->do_upload('foto')){
  334.         echo("gagal ");
  335.  
  336.     }else{
  337.         $data_upload = $this->upload->data(); // Get the file data
  338.         $fileData[] = $data_upload; // It's an array with many data
  339.         // Interate throught the data to work with them
  340.         foreach ($fileData as $file) {
  341.             $file_data = $file;
  342.         }
  343.  
  344.         // var_dump($file_data);
  345.  
  346.         $input = array();
  347.         $input['foto']                      = $file_data['file_name'];
  348.         $input["nama"]                      = $nama;
  349.         $input["kelas"]                     = $kelas;
  350.         $input["alamat"]                    = $alamat;
  351.         $input["umur"]                      = $umur;
  352.         $input["tgl_lahir"]                 = $tgl_lahir;
  353.  
  354.  
  355.         // Using quoery for insert database
  356.  
  357.         $status = $this->db->insert("tb_siswa",$input);
  358.  
  359.  
  360.         $data = array();
  361.         // Cek berhasil apa gak
  362.         if ($status) {
  363.            
  364.             $data['sukses'] = true;
  365.             $data['pesan'] = "Insert Berhasil";
  366.         }else{
  367.  
  368.             $data['sukses'] = false;
  369.             $data['pesan'] = "Insert tidak berhasil";
  370.  
  371.         }
  372.  
  373.         echo json_encode($data);          
  374.     }
  375. }
  376. // Membuat post di tb_berita / INSERT
  377. function tambah_berita(){
  378. // Variabel inputan mobile
  379.     $judul          = $this->input->post("judul");
  380.     $konten         = $this->input->post("konten");
  381.     $tgl_terbit     = $this->input->post("tgl_terbit");
  382.     $penerbit       = $this->input->post("penerbit");
  383.     $gambar         = $this->input->post("gambar");
  384.  
  385.  
  386.     $config['upload_path']          = './foto/';
  387.     $config['allowed_types']        = 'gif|jpg|png';
  388.     $config['max_size']             = 1000;
  389.     $config['max_width']            = 1024;
  390.     $config['max_height']           = 768;
  391.  
  392.     $this->load->library('upload', $config);
  393.  
  394.     if ( ! $this->upload->do_upload('gambar')){
  395.         echo("gagal ");
  396.  
  397.     }else{
  398.         $data_upload = $this->upload->data(); // Get the file data
  399.         $fileData[] = $data_upload; // It's an array with many data
  400.         // Interate throught the data to work with them
  401.         foreach ($fileData as $file) {
  402.             $file_data = $file;
  403.         }
  404.  
  405.         // var_dump($file_data);
  406.  
  407.         $input = array();
  408.         $input['gambar'] = $file_data['file_name'];
  409.         $input["judul"]                     = $judul;
  410.         $input["konten"]                    = $konten;
  411.         $input["tgl_terbit"]                = $tgl_terbit;
  412.         $input["penerbit"]                  = $penerbit;
  413.  
  414.  
  415.         // Using quoery for insert database
  416.  
  417.         $status = $this->db->insert("tb_berita",$input);
  418.  
  419.  
  420.         $data = array();
  421.         // Cek berhasil apa gak
  422.         if ($status) {
  423.            
  424.             $data['sukses'] = true;
  425.             $data['pesan'] = "Insert Berhasil";
  426.         }else{
  427.  
  428.             $data['sukses'] = false;
  429.             $data['pesan'] = "Insert tidak berhasil";
  430.  
  431.         }
  432.  
  433.         echo json_encode($data);          
  434.     }
  435. }
  436.   // Membuat post di tb_kegiatan / INSERT
  437. function tambah_kegiatan(){
  438. // Variabel inputan mobile
  439.     $nama_kegiatan          = $this->input->post("nama_kegiatan");
  440.     $tujuan                 = $this->input->post("tujuan");
  441.     $keterangan             = $this->input->post("keterangan");
  442.     $jam                    = $this->input->post("jam");
  443.     $tgl                    = $this->input->post("tgl");
  444.     $lokasi                 = $this->input->post("lokasi");
  445.  
  446.     $config['upload_path']          = './foto/';
  447.     $config['allowed_types']        = 'gif|jpg|png';
  448.     $config['max_size']             = 1000;
  449.     $config['max_width']            = 1024;
  450.     $config['max_height']           = 768;
  451.  
  452.     $this->load->library('upload', $config);
  453.  
  454.     if ( ! $this->upload->do_upload('foto')){
  455.         echo("gagal ");
  456.  
  457.     }else{
  458.         $data_upload = $this->upload->data(); // Get the file data
  459.         $fileData[] = $data_upload; // It's an array with many data
  460.         // Interate throught the data to work with them
  461.         foreach ($fileData as $file) {
  462.             $file_data = $file;
  463.         }
  464.  
  465.         // var_dump($file_data);
  466.  
  467.         $input = array();
  468.         $input['foto'] = $file_data['file_name'];
  469.         $input["nama_kegiatan"]             = $nama_kegiatan;
  470.         $input["tujuan"]                    = $tujuan;
  471.         $input["keterangan"]                = $keterangan;
  472.         $input["jam"]                       = $jam;
  473.         $input["tgl"]                       = $tgl;
  474.         $input["lokasi"]                    = $lokasi;
  475.  
  476.  
  477.         // Using quoery for insert database
  478.  
  479.         $status = $this->db->insert("tb_kegiatan",$input);
  480.  
  481.  
  482.         $data = array();
  483.         // Cek berhasil apa gak
  484.         if ($status) {
  485.            
  486.             $data['sukses'] = true;
  487.             $data['pesan'] = "Insert Berhasil";
  488.         }else{
  489.  
  490.             $data['sukses'] = false;
  491.             $data['pesan'] = "Insert tidak berhasil";
  492.  
  493.         }
  494.  
  495.         echo json_encode($data);          
  496.     }
  497.  
  498.  
  499.    
  500.  
  501.  
  502.   }
  503.   // Membuat post di tb_laporan / INSERT
  504. function tambah_laporan(){
  505. // Variabel inputan mobile
  506.     $nama                       = $this->input->post("nama");
  507.     $kelas                      = $this->input->post("kelas");
  508.     $wali                       = $this->input->post("wali");
  509.     $poin                       = $this->input->post("poin");
  510.     $melanggar                  = $this->input->post("melanggar");
  511.     $keterangan                 = $this->input->post("keterangan");
  512.     $tgl_lapor                  = $this->input->post("tgl_lapor");
  513.     $pelapor                    = $this->input->post("pelapor");
  514.     $foto                       = $this->input->post("foto");
  515.  
  516.  
  517.     $config['upload_path']          = './foto/';
  518.     $config['allowed_types']        = 'gif|jpg|png';
  519.     $config['max_size']             = 0;
  520.     $config['max_width']            = 2024000;
  521.     $config['max_height']           = 2024000;
  522.  
  523.     $this->load->library('upload', $config);
  524.  
  525.     if ( ! $this->upload->do_upload('foto')){
  526.         echo("gagal ");
  527.  
  528.     }else{
  529.         $data_upload = $this->upload->data(); // Get the file data
  530.         $fileData[] = $data_upload; // It's an array with many data
  531.         // Interate throught the data to work with them
  532.         foreach ($fileData as $file) {
  533.             $file_data = $file;
  534.         }
  535.  
  536.         // var_dump($file_data);
  537.  
  538.         $input = array();
  539.         $input['foto']                      = $file_data['file_name'];
  540.         $input["nama"]                      = $nama;
  541.         $input["kelas"]                     = $kelas;
  542.         $input["wali"]                      = $wali;
  543.         $input["poin"]                      = $poin;
  544.         $input["melanggar"]                 = $melanggar;
  545.         $input["keterangan"]                = $keterangan;
  546.         $input["tgl_lapor"]                 = $tgl_lapor;
  547.         $input["pelapor"]                   = $pelapor;
  548.  
  549.  
  550.         // Using quoery for insert database
  551.  
  552.         $status = $this->db->insert("tb_laporan",$input);
  553.  
  554.  
  555.         $data = array();
  556.         // Cek berhasil apa gak
  557.         if ($status) {
  558.            
  559.             $data['sukses'] = true;
  560.             $data['pesan'] = "Insert Berhasil";
  561.         }else{
  562.  
  563.             $data['sukses'] = false;
  564.             $data['pesan'] = "Insert tidak berhasil";
  565.  
  566.         }
  567.  
  568.         echo json_encode($data);          
  569.     }
  570. }
  571. // Membuat post di tb_guru / INSERT
  572. function tambah_guru(){
  573. // Variabel inputan mobile
  574.     $nama       = $this->input->post("nama");
  575.     $umur       = $this->input->post("umur");
  576.     $tgl_lahir  = $this->input->post("tgl_lahir");
  577.     $no_telp    = $this->input->post("no_telp");
  578.     $alamat     = $this->input->post("alamat");
  579.     $foto       = $this->input->post('foto');
  580.  
  581.  
  582.     $config['upload_path']          = './foto/';
  583.     $config['allowed_types']        = 'gif|jpg|png';
  584.     $config['max_size']             = 1000;
  585.     $config['max_width']            = 1024;
  586.     $config['max_height']           = 768;
  587.  
  588.     $this->load->library('upload', $config);
  589.  
  590.     if ( ! $this->upload->do_upload('foto')){
  591.         echo("gagal ");
  592.  
  593.     }else{
  594.         $data_upload = $this->upload->data(); // Get the file data
  595.         $fileData[] = $data_upload; // It's an array with many data
  596.         // Interate throught the data to work with them
  597.         foreach ($fileData as $file) {
  598.             $file_data = $file;
  599.         }
  600.  
  601.         // var_dump($file_data);
  602.  
  603.         $input = array();
  604.         $input['foto']                      = $file_data['file_name'];
  605.         $input["nama"]                      = $nama;
  606.         $input["umur"]                      = $umur;
  607.         $input["tgl_lahir"]                 = $tgl_lahir;
  608.         $input["no_telp"]                   = $no_telp;
  609.         $input["alamat"]                    = $alamat;
  610.  
  611.  
  612.         // Using quoery for insert database
  613.  
  614.         $status = $this->db->insert("tb_guru",$input);
  615.  
  616.  
  617.         $data = array();
  618.         // Cek berhasil apa gak
  619.         if ($status) {
  620.            
  621.             $data['sukses'] = true;
  622.             $data['pesan'] = "Insert Berhasil";
  623.         }else{
  624.  
  625.             $data['sukses'] = false;
  626.             $data['pesan'] = "Insert tidak berhasil";
  627.  
  628.         }
  629.  
  630.         echo json_encode($data);          
  631.     }
  632. }
  633. function tambah_peraturan(){
  634. // Variabel inputan mobile
  635.     $judul = $this->input->post("judul");
  636.     $isi = $this->input->post("isi");
  637.     $penulis = $this->input->post("penulis");
  638.  
  639.  
  640.     // d implementasi nama field databasenya
  641.     $simpan = array();
  642.     $simpan["judul"]    = $judul;
  643.     $simpan["isi"]      = $isi;
  644.     $simpan["penulis"]  = $penulis;
  645.  
  646.  
  647.     // Using quoery for insert database
  648.  
  649.     $status = $this->db->insert("tb_peraturan",$simpan);
  650.  
  651.  
  652.     $data = array();
  653.     // Cek berhasil apa gak
  654.     if ($status) {
  655.        
  656.         $data['sukses'] = true;
  657.         $data['pesan'] = "Insert Berhasil";
  658.     }else{
  659.  
  660.         $data['sukses'] = false;
  661.         $data['pesan'] = "Insert tidak berhasil";
  662.  
  663.     }
  664.  
  665.     echo json_encode($data);
  666.  
  667.  
  668.   }
  669.  
  670. //  ++++++++++++++++++ Buat Fungsi Register +++++++++++++++++++++
  671.  
  672. function register(){
  673.  
  674.  
  675. //variable untuk ambil inputan dari mobile
  676.  $nama          = $this->input->post("nama");
  677.  $username      = $this->input->post("username");
  678.  $no_telp       = $this->input->post("no_telp");
  679.  $alamat        = $this->input->post("alamat");
  680.  $email         = $this->input->post("email");
  681.  $jenis_kelamin = $this->input->post("jenis_kelamin");
  682.  $password      = $this->input->post("password");
  683.  $foto          = $this->input->post("foto");
  684.  $level         = $this->input->post("level");
  685.  
  686.     $config['upload_path']          = './foto/';
  687.     $config['allowed_types']        = 'gif|jpg|png';
  688.     $config['max_size']             = 10000;
  689.     $config['max_width']            = 10024;
  690.     $config['max_height']           = 10768;
  691.  
  692.     $this->load->library('upload', $config);
  693.  
  694.     if ( ! $this->upload->do_upload('foto')){
  695.         echo("gagal ");
  696.  
  697.     }else{
  698.         $data_upload = $this->upload->data(); // Get the file data
  699.         $fileData[] = $data_upload; // It's an array with many data
  700.         // Interate throught the data to work with them
  701.         foreach ($fileData as $file) {
  702.             $file_data = $file;
  703.         }
  704.  
  705.         // var_dump($file_data);
  706.  
  707.         $input = array();
  708.         $input['foto']                          = $file_data['file_name'];
  709.         $input["nama"]                          = $nama;
  710.         $input["username"]                      = $username;
  711.         $input["no_telp"]                       = $no_telp;
  712.         $input["alamat"]                        = $alamat;
  713.         $input["email"]                         = $email;
  714.         $input["jenis_kelamin"]                 = $jenis_kelamin;
  715.         $input["password"]                      = md5($password);
  716.         $input["level"]                         = $level;
  717.  
  718.  
  719.  
  720.         // Using quoery for insert database
  721.  
  722.         $status = $this->db->insert("tb_user",$input);
  723.  
  724.  
  725.         $data = array();
  726.         // Cek berhasil apa gak
  727.         if ($status) {
  728.            
  729.             $data['sukses'] = true;
  730.             $data['pesan'] = "Insert Berhasil";
  731.         }else{
  732.  
  733.             $data['sukses'] = false;
  734.             $data['pesan'] = "Insert tidak berhasil";
  735.  
  736.         }
  737.  
  738.         echo json_encode($data);          
  739.     }
  740. }
  741.  
  742.  
  743. // function loginUser(){
  744.  
  745.  
  746. //  $email = $this->input->post("email");
  747. //  $password = $this->input->post("password");
  748.  
  749. //  $this->db->where('user_email',$email);
  750. //  $verifEmail = $this->db->get("tb_user");
  751.  
  752. //  $this->db->where('user_password',$password);
  753. //  $verifPassword = $this->db->get("tb_user");
  754.  
  755. //  // cek kondisi ada datanya apa gak
  756. //  if ($verifEmail -> num_rows() != 0 && $verifPassword -> num_rows() != 0) {
  757. //      // Bikin respones k emobile
  758. //      $data["pesan"] = "Sukses Login";
  759. //      $data["sukses"] = true;
  760. //      $data['data'] = $verifEmail->result();
  761. //  }else if ($verifEmail -> num_rows() == 0){
  762. //      $data["pesan"] = "username salah bang!";
  763. //      $data["sukses"] = false;
  764. //  }else{
  765. //      $data["pesan"] = "password salah bang!";
  766. //      $data["sukses"] = false;
  767.  
  768. //  }
  769. //  echo json_encode($data);
  770. // }   
  771.  
  772. function login(){
  773.  
  774.  $username = $this->input->post("username");
  775.  $password = $this->input->post("password");
  776.  
  777.  $this->db->where("username",$username);
  778.  $this->db->where("password",md5($password));
  779.  
  780.  
  781.  $hasil = $this->db->get("tb_user");
  782.  
  783.   //check query ada datanya apa enggak
  784.   if($hasil -> num_rows() > 0 ){
  785.  
  786.     //bikin response k mobile
  787.    $data['pesan'] = "login berhasil";
  788.    $data['sukses']  = true ;
  789.    $data['data'] = $hasil->row();
  790.  
  791.   }
  792.   else{
  793.    $data['pesan'] = "username atau password salah";
  794.    $data['sukses']  = false ;
  795.  
  796.   }
  797.   echo json_encode($data);
  798.  
  799. }
  800.  
  801.  
  802. }
  803. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement