Codeigniter投票候选功能

时间:2016-11-05 06:12:55

标签: php mysql codeigniter

我尝试在Codeigniter上制作候选人投票系统。每个用户只能投票1个候选人,投票后候选用户状态将从' 1'到' 0'所以用户不能再投票。有人可以帮我创建这个功能吗?

这是我的代码。



<?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');
    }
}
?>
&#13;
&#13;
&#13;

和此模型代码。

&#13;
&#13;
<?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);	
    }
	
	

}

?>
&#13;
&#13;
&#13;

请帮帮我。感谢每一个人。 *

0 个答案:

没有答案