dongzg2006 2016-10-31 14:06
浏览 19
已采纳

投票系统候选人[关闭]

I want to create a vote system to choose a new head village. How can I make a function to vote the candidate ?

Each user can vote only once.

My code (I know this will be wrong):

function vote_calon(){

    $id_calon = $this->input->post('id_calon');
    $voted= $this->input->post('voted');
    $data = array(
        'voted' =>  'set ++1',
    );

    $where = array(
        'id_calon' => $id_calon,        
    );

    $this->Admin_dcalon->vote_calon($where,$data,'calon');      
    redirect('voter/voter_sukses');
}
  • 写回答

1条回答 默认 最新

  • doucong1992 2016-10-31 15:04
    关注

    First, I guess you have already logged user or submitted user, not guest/anonymous which not login yet or not submit input of his name. Otherwise the code workflow will be different.

    Table in your database:

    table
    #calon_table => profile of governor or person who will voted 
    id [int auto_increment]
    name [varchar]
    
    #user_table = profile of user who will vote
    id [int auto_increment]
    name [varchar]
    password, avatar, and another information here
    
    #vote_table = table to store vote result
    id_user [int ,PRIMARY KEY, foreign key of user_table.id] //while this set as Primary Key , you are never get duplicate user for voting
    id_calon [int ,foreign key of calon_table.id]
    

    And the code will be:

    function vote_calon(){
        $id_calon = $this->input->post('id_calon'); //example : 2
        $id_user = $this->input->post('voted'); //example: 324234
        $data = array(
            'id_user' =>  $id_user,
            'id_calon' =>  $id_calon
        );
    
        $where = array(
            'id_user' => $id_user //this is where clause to keep 1 user only can vote 1 governor
        );
    
        $check = $this->db->where('vote_table', $where);
        if($check->num_rows() > 0)
        {
            echo "you already use your vote"; //this is for make sure user cant vote more than 1
            //Or you can try update: $this->db->update('vote_table', $data, $where);
        }
        else
        {
            //insert data
            $this->db->insert('vote_table', $data);
        }
    
        redirect('voter/voter_sukses');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能