Advertisement
andrekj

Controller

Oct 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3.  
  4. class Home extends CI_Controller
  5. {
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. $this->load->library('form_validation');
  10. $this->load->library('session');
  11. }
  12. public function index()
  13. {
  14. $data['user'] = $this->db->get_where('user', ['username' => $this->session->userdata('username')])->row_array();
  15.  
  16. $this->load->view('templates/h_header', $data);
  17. $this->load->view('templates/h_banner', $data);
  18. $this->load->view('templates/h_navbar', $data);
  19. $this->load->view('home/index', $data);
  20. $this->load->view('templates/h_footer');
  21. }
  22. public function profile()
  23. {
  24. $data['title'] = 'Profile';
  25. $data['user'] = $this->db->get_where('user', ['username' => $this->session->userdata('username')])->row_array();
  26.  
  27. $this->load->view('templates/h_header', $data);
  28. $this->load->view('templates/h_banner', $data);
  29. $this->load->view('templates/h_navbar', $data);
  30. $this->load->view('home/profile', $data);
  31. $this->load->view('templates/h_footer');
  32. }
  33. public function edit()
  34. {
  35. $data['title'] = 'Edit Profile';
  36. $data['user'] = $this->db->get_where('user', ['username' =>
  37. $this->session->userdata('username')])->row_array();
  38.  
  39. $this->form_validation->set_rules('name', 'Full Name', 'required|trim');
  40.  
  41. if ($this->form_validation->run() == false) {
  42. $this->load->view('templates/h_header', $data);
  43. $this->load->view('templates/h_banner', $data);
  44. $this->load->view('templates/h_navbar', $data);
  45. $this->load->view('home/edit-profile', $data);
  46. $this->load->view('templates/h_footer');
  47. } else {
  48. $username = $this->input->post('username');
  49. $nama = $this->input->post('nama');
  50. $email = $this->input->post('email');
  51. $telepon = $this->input->post('telepon');
  52. $alamat = $this->input->post('alamat');
  53.  
  54. $upload_image = $_FILES['image']['name'];
  55.  
  56. if ($upload_image) {
  57. $config['allowed_types'] = 'gif|jpg|png';
  58. $config['max_size'] = '2048';
  59. $config['upload_path'] = './assets/img/profile/';
  60.  
  61. $this->load->library('upload', $config);
  62.  
  63. if ($this->upload->do_upload('image')) {
  64. $old_image = $data['user']['image'];
  65. if ($old_image != 'default.jpg') {
  66. unlink(FCPATH . 'assets/img/profile/' . $old_image);
  67. }
  68.  
  69. $new_image = $this->upload->data('file_name');
  70. $this->db->set('image', $new_image);
  71. } else {
  72. echo $this->upload->display_errors();
  73. }
  74. }
  75.  
  76. $this->db->set('nama', $nama, 'email', $email, 'telepon', $telepon, 'alamat', $alamat);
  77. $this->db->where('username', $username);
  78. $this->db->update('user');
  79.  
  80. $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Profil mu telah di update</div>');
  81. redirect('home/profile');
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement