• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codeigniter Vote Candidate System

#1
Exclamation 
Hai, i'm new in codeigniter and i want to create a vote candidate system as my thesis. I want to create function each user can vote 1 candidate only, and after user vote the candidate the user status will be change from '1' to '0'.
1= its mean user can vote
0= its mean user can't vote.

i already try to create it, but doesnt work.

Can someone help me ?
Big thanks from me.

Here is my code.

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');
 

class 
Admin_calon extends CI_Controller
{
 
 
   function __construct() {
 
       parent::__construct();
 
 
      
        
        $this
->load->model('Admin_dcalon');
        
$this->load->model('Admin_dvoter');
        
$this->load->model('Voter_model');
        
$this->load->helper('url');
        
$this->load->database();
 
      
    
}
 
 
   public function index() {
        
$data['calon'] = $this->Admin_dcalon->tampil_data()->result();
 
       $this->load->view('Admin_calon',$data);
        
 
   }
    
    function 
tambah_calon(){
        
$nama $this->input->post('nama');
        
                
$config['upload_path'] = base_url(). './uploads/';
 
               $config['allowed_types'] = 'jpg|png|jpeg';
 
               $config['max_size'] = '0';
                
                
$this->load->library('upload'$config);
 
               $this->upload->initialize($config);
 
               $this->upload->do_upload();
                
        
$img $this->input->post('img');
        
 
        
$data = array(
            
'nama' => $nama,
            
'img' => $img,
            
'voted' => 0
            
);
        
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Data Tersimpan</div>');
        
$this->Admin_dcalon->tambah_data($data,'calon');
        
redirect('Admin_calon');
    }
    
    
    public function 
delete_calon($id_calon){
        
$where = array('id_calon' => $id_calon);
        
$this->Admin_dcalon->hapus_data($where,'calon');
         
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Data Terhapus</div>');
        
redirect('Admin_calon');
    }
 
     function 
edit_calon($id_calon){
        
$where = array('id_calon' => $id_calon);
        
$data['calon'] = $this->Admin_dcalon->edit_data($where,'calon')->result();
        
$this->load->view('Admin_ecalon',$data);
    }
    
    function 
update_calon(){
    
$id_calon $this->input->post('id_calon');
    
$nama $this->input->post('nama');
    
$img $this->input->post('img');

    
$data = array(
        
'nama' => $nama,
        
'img' => $img,        
    );

    
$where = array(
        
'id_calon' => $id_calon
    
);
    
$this->session->set_flashdata('msg','<div class="alert alert-info text-center">Perubahan Tersimpan</div>');
    
$this->Admin_dcalon->update_data($where,$data,'calon');
    
redirect('Admin_calon');
}

function 
vote_calon(){
 
  $id_calon $this->input->post('id_calon');
    
$voted $this->input->post('voted');
    

    
$data = array(
                
'id_calon' => $id_calon,
                
'voted' => +1
    
);

    
$where = array(
        
'id_calon' => $id_calon,
    );
    
    
$this->Admin_dcalon->vote_calon($where,$data,'calon');
    
redirect('voter/voter_sukses');
 
   
}

    
 
   public function logout() {
 
       $data = ['id_admin''username'];
 
       $this->session->unset_userdata($data);
 
 
       redirect ('Admin_login');
 
   }
}
?>

Model

PHP Code:
<?php

class Admin_dcalon extends CI_Model{
    
    function 
tampil_data(){
        
$this->db->select('*');
        
$this->db->from('calon');
        return 
$this->db->get();
    }
    
    function 
tambah_data($data,$table){
        
$this->db->insert($table,$data);
    }
    
    function 
hapus_data($where,$table){
    
$this->db->where($where);
    
$this->db->delete($table);
    }
    
    function 
edit_data($where,$table){        
    return 
$this->db->get_where($table,$where);
    }
    
    function 
update_data($where,$data,$table){
        
$this->db->where($where);
        
$this->db->update($table,$data);
    }
    
     function 
vote_calon($where,$data,$table)
 
   {
 
      $this->db->where($where);
        
$this->db->update($table,$data);    
 
   }
    
    

}

?>

and here is my view, from user page.

PHP Code:
<!doctype html>
<
html>


<
head>
 
       <title>Vote Kepala Desa</title>
 
       <link href="<?php echo base_url('asset/bootstrap/css/style.css');?>" rel="stylesheet"
 
       <link href="<?php echo base_url('asset/bootstrap/css/bootstrap.min.css');?>" rel="stylesheet">
 
        <link href="<?php echo base_url('asset/bootstrap/css/sb-admin.css');?>" rel="stylesheet">
 
        <link href="../../../asset/bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css">
 
        
    
    
</head>


<
body style="font-family:'Segoe UI Light'">

 
       <!-- Navigation -->
 
       <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
 
           <!-- Brand and toggle get grouped for better mobile display -->
 
           <div class="navbar-header">
 
           </div>
 
           <!-- Top Menu Items -->
 
           <ul class="nav navbar-left top-nav">
 
               
                
<li class="active">
 
                       <a href="<?= site_url('voter/voter_home') ?>"><class="fa fa-fw fa-dashboard"></i>Home</a>
 
                   </li>
 
                   <li>
 
                       <a href="<?= site_url('voter/voter_vote') ?>" ><class="fa fa-fw fa-bar-chart-o"></i>Vote</a>
 
                   </li>
 
                   <li>
 
                       <a href="<?= site_url('voter/voter_hasil') ?>" ><class="fa fa-fw fa-bar-chart-o"></i>Hasil Voting</a>
 
                   </li>
 
           </ul>
 
           
            
<ul class="nav navbar-right top-nav">
 
               
                
                        
<li>
 
                   <a style="float:right" href="<?= site_url('voter/voter_home/logout') ?>">Logout</a>
 
                   
                        
</li>
 
           </ul>
 
     
        
</nav>
 
       
        
        
        
           
        
     
         
<center>
 
                   <div class="col-lg-10" style="float:inherit">
 
                   
                      
<div class="panel panel-default panel-profile m-b-0">
 
                       <div class="panel-heading" ></div>
 
                               
                                
<div class="panel-body text-center" >
 
                                     <img src="<?php echo base_url('asset/img/certificate.png');?>" height="200" width="200">
 
                       

                        
<h1><strong>Vote Calon Kepala Desa</strong></h1>
 
                       <p><strong>Sistem E-voting Desa Pakraman Cucukan</strong></p>
 
                       <img src="<?php echo base_url('asset/img/bookmark.png');?>" height="50" width="50">
 
                           
                            
<br
 
                           <br>
 
                           <center>
 
                           <h5 class="alert alert-danger">
 
                           <strong>PERHATIAN VOTE HANYA DAPAT DILAKUKAN 1X SATU KALI )</strong></h5>
 
                           </center>        
 
                                             
                                 
                               
<form class="" action="<?= site_url('admin_calon/vote_calon') ?>" method="post"
 
        <center><table style="float:inherit" class="table table-bordered table-hover table-striped" >
        <?
php 
        $no 
1;
        foreach(
$calon as $calon){ 
        
?>
       
        <div class="col-lg-4" action=""  method="post">
        
             <div class="panel panel-default panel-profile m-b-0">
             <div class="panel-heading" ></div>
                        <br>
                        <img class="img-circle" style="width:200px;height:200px" src="<?php echo  base_url('upload/'.$calon->img?>">
                        <h4><strong>Calon <?php echo $calon->id_calon ?></strong></h4>
                        <h5><?php echo $calon->nama ?></h5>
                        <br>
                        
                        <button class="btn btn-md btn-success" type="submit" >Vote</button>
                         </img>    
                          <br>
                          <br>
             </div> 
        </div>
        <?php ?>
        
        </table></center>
        </form>
                                      
                                 
                                
                                
                        </div>
                      </div>
                  
                  </div>
       </center>
                
         
 
</body>

</html> 

Thanks for any help.

Gus bala
Reply

#2
If 1 user can vote just once for 1 candidate, I think that it will be easier for you instead of vote status 0|1, to have field voted_for - which will keep the candidate ID ..
So when its NULL or 0 this mean that the user has not voted yet..

In case that such votes will be not just once per your site life it will be better if you do that in a separate table "user_votes"
With 3 fields :

User_id : the id of the one who set vote,
Candidate_id : the id of the candidate
Vote_id : the id of the Vote (this field will separate each voting campaign as: summer2016, winter2017 and etc..)

In this case you can add and unique index for ( user_id + vote_id ) which will limit users to vote just once per campaign.
Best VPS Hosting : Digital Ocean
Reply

#3
(11-05-2016, 01:52 AM)sv3tli0 Wrote: If 1 user can vote just once for 1 candidate, I think that it will be easier for you instead of vote status 0|1,  to have field voted_for - which will keep the candidate ID ..
So when its NULL or 0 this mean that the user has not voted yet..

In case that such votes will be not just once per your site life it will be better if you do that in a separate table "user_votes"
With 3 fields :

User_id : the id of the one who set vote,
Candidate_id : the id of the candidate
Vote_id : the id of the Vote (this field will separate each voting campaign as: summer2016, winter2017 and etc..)

In this case you can add and unique index for ( user_id + vote_id ) which will limit users to vote just once per campaign.

Thanks for your suggestion. But my problem is how to create a vote function i'm stuck on it. I already create a separate table for user.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.