3. Tutorial Codeigniter : Membuat Web Event Organizer (User/Admin & Login)



Sebelum ke pembahasan selanjutnya, saya sarankan melihat tutorial sebelumnya.

User/admin
a)       Buat database “event_organizer” dan tabel “user/admin”


b)      Di “application->views->admin” buat folder “user” dan di dalamnya buat file list.php dan form.php
c)       Di “application->controllers” buat file User.php
d)      Di “application->models” buat file Model_user.php


Model_user.php
 <?php  
 /*  
 * To change this template, choose Tools | Templates  
 * and open the template in the editor.  
 */  
 class Model_user extends CI_Model {  
  function __construct() {  
   parent::__construct();  
  }  
  public function check($email, $pass) {  
   $sql = "SELECT * FROM user WHERE email=? AND password=?";  
   $query = $this->db->query($sql, array($email, $pass));  
   if ($query->num_rows() > 0) {  
    $result = $query->result();  
    return $result[0];  
   } else {  
    return null;  
   }  
   $query->free_result();  
  }  
  public function all() {  
   $sql = "SELECT * FROM user";  
   $query = $this->db->query($sql);  
   $result = $query->result();  
   return $result;  
   $query->free_result();  
  }  
  public function find($id) {  
   $sql = "SELECT * FROM user WHERE userid=?";  
   $query = $this->db->query($sql, array($id));  
   if ($query->num_rows() > 0) {  
    $result = $query->result();  
    return $result[0];  
   } else {  
    return null;  
   }  
   $query->free_result();  
  }  
  public function add($param) {  
   $sql = "INSERT INTO user (userid, name, email, password) VALUES (?, ?, ?, ?)";  
   $this->db->query($sql, array(uniqid(), $param['name'], $param['email'], md5($param['password'])));  
   return true;  
  }  
  public function edit($param) {  
   if(trim($param['password']) == ""){  
    $sql = "UPDATE user SET name=?, email=? WHERE userid=?";  
    $this->db->query($sql, array($param['name'], $param['email'], $param['userid']));  
   }else{  
    $sql = "UPDATE user SET name=?, email=?, password=? WHERE userid=?";  
    $this->db->query($sql, array($param['name'], $param['email'], md5($param['password']), $param['userid']));  
   }  
   return true;  
  }  
  public function delete($id) {  
   $sql = "DELETE FROM user WHERE userid = ?";  
   $this->db->query($sql, array($id));  
   return true;  
  }  
 }  
 ?>  

User.php
 <?php  
 defined('BASEPATH') OR exit('No direct script access allowed');  
 class User extends CI_Controller {  
  public function __construct() {  
   parent::__construct();  
   $this->isLogin();  
   $this->clearCache();  
   $this->load->model('Model_user');  
  }  
  private function isLogin() {  
   $isLogin = $this->session->userdata('logged_in');  
     if ($isLogin != 'yes' ) {  
    redirect(base_url('login'));  
   }  
  }  
  private function clearCache() {  
   $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");  
   $this->output->set_header("Pragma: no-cache");  
  }  
  public function index() {  
   $data['all'] = $this->Model_user->all();  
   $data['title'] = "Users";  
   $data['content'] = 'admin/user/list';  
   $data['css'] = 'admin/template/datatables/css';  
   $data['js'] = 'admin/template/datatables/js';  
   $this->load->view('admin/template/body', $data);  
  }  
  public function add() {  
   $data['user'] = array(  
    'userid' => '',  
    'name' => '',  
    'email' => '',  
    'password' => ''  
   );  
   $data['title'] = "Add User";  
   $data['content'] = 'admin/user/form';  
   $this->load->view('admin/template/body', $data);  
  }  
  public function edit($id){  
   $user = $this->Model_user->find($id);  
   $data['user'] = array(  
    'userid' => $user->userid,  
    'name' => $user->name,  
    'email' => $user->email,  
    'password' => $user->password,  
   );  
   $data['title'] = "Edit User";  
   $data['content'] = 'admin/user/form';  
   $this->load->view('admin/template/body', $data);  
  }  
  public function save() {  
   $param = $this->input->post();  
   if ($param['userid'] == "") {  
    $result = $this->Model_user->add($param);  
   } else {  
    $result = $this->Model_user->edit($param);  
   }  
   redirect(base_url('user'));  
  }  
  public function delete($id) {  
   $this->Model_user->delete($id);  
   redirect(base_url('user'));  
  }  
 }  

form.php
     <header class="page-header">  
       <div class="container-fluid">  
        <h2 class="no-margin-bottom">Forms</h2>  
       </div>  
      </header>  
      <ul class="breadcrumb">  
       <div class="container-fluid">  
        <li class="breadcrumb-item"><a href="index.html">Home</a></li>  
        <li class="breadcrumb-item active">Form User</li>  
       </div>  
      </ul>  
      <!-- Forms Section-->  
      <section class="forms">   
       <div class="container-fluid">  
        <div class="row">  
         <div class="col-lg-12">  
          <div class="card">  
           <div class="card-close">  
            <div class="dropdown">  
             <button type="button" id="closeCard" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="dropdown-toggle"><i class="fa fa-ellipsis-v"></i></button>  
             <div aria-labelledby="closeCard" class="dropdown-menu has-shadow"><a href="#" class="dropdown-item remove"> <i class="fa fa-times"></i>Close</a><a href="#" class="dropdown-item edit"> <i class="fa fa-gear"></i>Edit</a></div>  
            </div>  
           </div>  
           <div class="card-header d-flex align-items-center">  
            <h3 class="h4">Form User</h3>  
           </div>  
           <div class="card-body">  
            <form class="form-horizontal" action="<?php echo base_url(); ?>user/save" method="post">  
             <div class="form-group row">  
              <label class="col-sm-3 form-control-label">Name</label>  
              <div class="col-sm-9">  
               <input type="text" value="<?php echo $user['name']; ?>" class="form-control" name="name" required>  
               <input type="hidden" value="<?php echo $user['userid']; ?>" class="form-control" name="userid">  
              </div>  
             </div>  
             <div class="line"></div>  
             <div class="form-group row">  
              <label class="col-sm-3 form-control-label">Email</label>  
              <div class="col-sm-9">  
               <input type="text" class="form-control" value="<?php echo $user['email']; ?>" name="email" required>  
              </div>  
             </div>  
             <div class="line"></div>  
             <div class="form-group row">  
              <label class="col-sm-3 form-control-label">Password</label>  
              <div class="col-sm-9">  
               <input type="password" class="form-control" value="<?php echo $user['password']; ?>" required>  
              </div>  
             </div>  
             <div class="form-group row">  
              <div class="col-sm-4 offset-sm-3">  
               <button type="submit" class="btn btn-secondary">Cancel</button>  
               <button type="submit" class="btn btn-primary">Save</button>  
              </div>  
             </div>  
            </form>  
           </div>  
          </div>  
         </div>  
        </div>  
       </div>  
      </section>  

list.php
 <header class="page-header">  
       <div class="container-fluid">  
        <h2 class="no-margin-bottom">Users</h2>  
       </div>  
      </header>  
      <!-- Breadcrumb-->  
      <ul class="breadcrumb">  
       <div class="container-fluid">  
        <li class="breadcrumb-item"><a href="index.html">Home</a></li>  
        <li class="breadcrumb-item active">Users</li>  
       </div>  
      </ul>  
      <section class="tables">    
       <div class="container-fluid">  
        <div class="row">  
         <div class="col-lg-12">  
          <div class="form-group row">  
            <div class="col-sm-1">  
             <a href="<?php echo base_url("user/add");?>" class="btn btn-primary">Add</a>  
            </div>  
           </div>  
          <div class="card">  
           <div class="card-close">  
            <div class="dropdown">  
             <button type="button" id="closeCard" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="dropdown-toggle"><i class="fa fa-ellipsis-v"></i></button>  
             <div aria-labelledby="closeCard" class="dropdown-menu has-shadow"><a href="#" class="dropdown-item remove"> <i class="fa fa-times"></i>Close</a><a href="#" class="dropdown-item edit"> <i class="fa fa-gear"></i>Edit</a></div>  
            </div>  
           </div>  
           <div class="card-header d-flex align-items-center">  
            <h3 class="h4">List User</h3>  
           </div>  
           <div class="card-body">  
            <table width="100%" class="table table-bordered " id="dataTables-example">  
             <thead>  
              <tr class="success">  
               <th>#</th>  
               <th>Name</th>  
               <th>Email</th>  
               <th>Aksi</th>  
              </tr>  
             </thead>  
             <tbody>  
             <?php $no=1; for ($i=0; $i < count($all); $i++) { ?>  
              <tr>  
               <th><?= $no++ ?></th>  
               <td><?= $all[$i]->name ?></td>  
               <td><?= $all[$i]->email ?></td>  
               <td><a href="<?php echo base_url(); ?>user/edit/<?= $all[$i]->userid ?>" class="btn btn-sm btn-primary">Edit</a> <a href="<?php echo base_url(); ?>user/delete/<?= $all[$i]->userid ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this item?');">Delete</a></td>  
              </tr>  
             <?php } ?>  
             </tbody>  
            </table>  
           </div>  
          </div>  
         </div>       
        </div>  
      </div>  
      </section>  


Login
e)      Di “application->views->admin” terdapat login.html dan ubah menjadi login.php
f)        Di “application->controller” buat file Login.php

Login.php controller
 <?php  
 defined('BASEPATH') OR exit('No direct script access allowed');  
 class Login extends CI_Controller {  
  public function __construct() {  
   parent::__construct();  
   $this->clearCache();  
  }  
  private function isLogin() {  
   $isLogin = $this->session->userdata('logged_in');  
   if ($isLogin != 'yes') {  
    redirect(base_url('login'));  
   }  
  }  
  private function clearCache() {  
   $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");  
   $this->output->set_header("Pragma: no-cache");  
  }  
  public function index() {  
   if ($this->session->userdata('logged_in') == 'yes') {  
    redirect(base_url('user'));  
    die();  
   }  
   $this->load->view("admin/login");  
  }  
  public function check() {  
   $this->load->model('Model_user');  
   $email = $this->input->post('email');  
   $pass = md5($this->input->post('password'));  
   $user = $this->Model_user->check($email, $pass);  
   if (!$user) {  
    $this->session->set_flashdata('login_error', TRUE);  
    redirect(base_url('login'));  
   } else {  
    $this->session->set_userdata(array(  
     'logged_in' => 'yes',  
     'userid' => $user->userid,  
     'email' => $user->email,  
     'name' => $user->name  
    ));  
    redirect(base_url('user'));  
   }  
  }  
  public function logout() {  
   $this->session->sess_destroy();  
   redirect(base_url('login'));  
  }  
 }  

login.php di “application->views->admin” ubah menjadi

 <!DOCTYPE html>  
 <html>  
  <head>  
   <meta charset="utf-8">  
   <meta http-equiv="X-UA-Compatible" content="IE=edge">  
   <title>Bootstrap Dashboard by Bootstrapious.com</title>  
   <meta name="description" content="">  
   <meta name="viewport" content="width=device-width, initial-scale=1">  
   <meta name="robots" content="all,follow">  
   <!-- Bootstrap CSS-->  
   <link rel="stylesheet" href="<?php echo base_url()?>public/css/bootstrap.min.css">  
   <!-- Google fonts - Roboto -->  
   <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Poppins:300,400,700">  
   <!-- theme stylesheet-->  
   <link rel="stylesheet" href="<?php echo base_url()?>public/css/style.default.css" id="theme-stylesheet">  
   <!-- Custom stylesheet - for your changes-->  
   <link rel="stylesheet" href="<?php echo base_url()?>public/css/custom.css">  
   <!-- Favicon-->  
   <link rel="shortcut icon" href="<?php echo base_url()?>public/img/favicon.ico">  
   <!-- Font Awesome CDN-->  
   <!-- you can replace it by local Font Awesome-->  
   <script src="https://use.fontawesome.com/99347ac47f.js"></script>  
   <!-- Font Icons CSS-->  
   <link rel="stylesheet" href="https://file.myfontastic.com/da58YPMQ7U5HY8Rb6UxkNf/icons.css">  
   <!-- Tweaks for older IEs--><!--[if lt IE 9]>  
     <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>  
     <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script><![endif]-->  
  </head>  
  <body>  
   <div class="page login-page">  
    <div class="container d-flex align-items-center">  
     <div class="form-holder has-shadow">  
      <div class="row">  
       <!-- Logo & Information Panel-->  
       <div class="col-lg-6">  
        <div class="info d-flex align-items-center">  
         <div class="content">  
          <div class="logo">  
           <h1>Login</h1>  
          </div>  
          <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>  
         </div>  
        </div>  
       </div>  
       <!-- Form Panel  -->  
       <div class="col-lg-6 bg-white">  
        <div class="form d-flex align-items-center">  
         <div class="content">  
          <form id="login-form" method="post" action="<?php echo base_url(); ?>login/check">  
           <div class="form-group">  
            <input id="login-username" type="text" name="email" required="" class="input-material">  
            <label for="login-username" class="label-material">Email</label>  
           </div>  
           <div class="form-group">  
            <input id="login-password" type="password" name="loginPassword" required="" class="input-material">  
            <label for="login-password" class="label-material">Password</label>  
           </div><button type="submit" class="btn btn-primary">Login</button>  
           <!-- This should be submit button but I replaced it with <a> for demo purposes-->  
          <!-- </form><a href="#" class="forgot-pass">Forgot Password?</a><br><small>Do not have an account? </small><a href="register.html" class="signup">Signup</a> -->  
         </div>  
        </div>  
       </div>  
      </div>  
     </div>  
    </div>  
    <div class="copyrights text-center">  
     <p>Design by <a href="https://bootstrapious.com/admin-templates" class="external">Bootstrapious</a></p>  
     <!-- Please do not remove the backlink to us unless you support further theme's development at https://bootstrapious.com/donate. It is part of the license conditions. Thank you for understanding :)-->  
    </div>  
   </div>  
   <!-- Javascript files-->  
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>  
   <script src="<?php echo base_url()?>public/js/tether.min.js"></script>  
   <script src="<?php echo base_url()?>public/js/bootstrap.min.js"></script>  
   <script src="<?php echo base_url()?>public/js/jquery.cookie.js"> </script>  
   <script src="<?php echo base_url()?>public/js/jquery.validate.min.js"></script>  
   <script src="<?php echo base_url()?>public/js/front.js"></script>  
   <!-- Google Analytics: change UA-XXXXX-X to be your site's ID.-->  
   <!---->  
   <script>  
    (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=  
    function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;  
    e=o.createElement(i);r=o.getElementsByTagName(i)[0];  
    e.src='//www.google-analytics.com/analytics.js';  
    r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));  
    ga('create','UA-XXXXX-X');ga('send','pageview');  
   </script>  
  </body>  
 </html>  



0 Response to " 3. Tutorial Codeigniter : Membuat Web Event Organizer (User/Admin & Login)"

Post a Comment