如何根据数据库值设置条件以在Codeigniter中添加新用户?

时间:2015-09-28 04:50:35

标签: php codeigniter

我有三个用户管理员转销商和用户。现在Admin创建了经销商和用户。因此,Admin会为特定的转销商添加用户。经销商在数据库中有一个名为分配块的数据库,这是经销商可以拥有的用户的最大值所以我在用户控制器中添加我的用户,我想检查一个条件如果我为XYZ经销商添加用户,那么我必须检查分配块值。如果它大于显示错误消息而不允许添加该用户。

以下是我添加用户的代码:

控制器:

public function add_user()
 { 

            $usertype = $this->session->userdata('usertype'); 
            $this->load->model('reseller_m'); 

            if ($usertype == "admin") { 
            $this->data['user'] = $this->user_m->get_new(); 
            $rules = $this->user_m->rules_admin; 
            $rules['password']['rules'] .= '|required'; 
            $this->form_validation->set_rules($rules); 

            if ($this->form_validation->run() == TRUE) { 

            $data = $this->user_m->array_from_post(array('sip_id', 'sip_pass', 'name', 'key', 'email', 'password', 'phone', 'status', 'created', 'balance')); 
            $data['password'] = $this->user_m->hash($data['password']); 

            $key = $this->user_m->save($data, $id); 
            redirect('admin/user'); 
            } 

            $resellers = $this->reseller_m->get_drop_down(); 
            if(count($resellers) > 0) { 
            foreach($resellers as $value) { 
            $dropdown[$value->key] = $value->key; 
            } 
            } 
            $this->data['resellers'] = $dropdown;



            $this->data['subview'] = 'admin/user/add'; 
            $this->load->view('admin/_layout_main', $this->data); 
            } else { 
            $this->load->view('permission'); 
            } 
            }

添加用户视图:

<h3><?php echo empty($user->id) ? '<i class="glyphicon glyphicon-user"></i> Add a Reseller' : 'Edit User ' . $user->name; ?></h3>
        <?php echo validation_errors(); ?>
        <?php echo form_open(); ?>
        <table class="table">

            <tr>
                <td>SIP Username</td>
                <td><?php echo form_input('sip_id', set_value('sip_id', $user->sip_id)); ?></td>
            </tr>

            <tr>
                <td>SIP Password</td>
                <td><?php echo form_input('sip_pass', set_value('sip_pass', $user->sip_pass)); ?></td>
            </tr>



        <tr> 
        <td>Key</td> 
        <td> 
        <?php echo form_dropdown('key', set_value('key', $resellers,$user->key));?>
        </td> 
        </tr>


            <tr>
                <td>Name</td>
                <td><?php echo form_input('name', set_value('name', $user->name)); ?></td>
            </tr>

            <tr>
                <td>Email</td>
                <td><?php echo form_input('email', set_value('email', $user->email)); ?></td>
            </tr>

            <tr>
                <td>Password</td>
                <td><?php echo form_password('password'); ?></td>
            </tr>

            <tr>
                <td>Confirm password</td>
                <td><?php echo form_password('password_confirm'); ?></td>
            </tr>


            <tr>
                <td>Balance</td>
            <td>Yes<?php echo form_radio('balance','Yes', $this->input->post('balance') ? $this->input->post('balance') : $user->balance);  ?> No <?php echo form_radio('balance','No', $this->input->post('balance') ? $this->input->post('balance') : $user->balance);  ?></td>   
            </tr>

            <tr>
                <td>Phone</td>
                <td><?php echo form_input('phone', set_value('phone', $user->phone)); ?></td>
            </tr>



            <tr>
                <td>Status</td>
                <td><?php echo form_dropdown('status', array('Active' => 'active', 'Inactive' => 'inactive', 'Delete' => 'delete'), $this->input->post('status') ? $this->input->post('status') : $user->status ); ?></td>  
            </tr>



            <tr>
                <td>Created</td>
                <td><?php echo form_input('created', set_value('created', $user->created)); ?></td>
            </tr>


            <tr>
                <td></td>
                <td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
            </tr>


        </table>
        <?php echo form_close();?>

1 个答案:

答案 0 :(得分:1)

首先,在用户表中,您必须有一列来指示特定用户的相应转销商ID。因此,如果该列已经存在,则应在表中添加该列。 然后在模型中,您可以添加一个函数来按照重新定位器ID计算用户。

function reseler_users_count($reseler_id){//count users by reseller
    $this->db->where('key', $reseler_id);
    return $this->db->count_all_results('users_tbl');
    //DB Query: Select * FROM users_tbl WHERE key = '$reseller_id';
    //return number of rows
} 

同样在您的模型中,添加一个检查分配区块值的函数,并将其与已为该代理商注册的用户数进行比较。它返回true或false,但您可以修改它以返回注册用户或false,或其他任何内容。

function check_reseller_allocation($reseler_id){
    $this->db->select('allocation_block');
    $this->db->where('id', $reseler_id);
    $query = $this->db->get('reselers_tbl');
//DB QUERY: SELECT 'allocation_block' FROM 'reselers_tbl' WHERE 'id' = $reseller_id;
    $reseler = $query->row();// Get row (it should be only one row, but I didn't add the condition though; you can add if($query->num_rows() == 1) )
    $allocation_block = $reseler->alocation_block; 
    $reseller_users = $this->reseler_users_count($reseler_id);//call reseler_users_count() to find users registered for this reseller
    if($reseller_users < $allocation_block)return TRUE;//is the number of users registered with ('key' = $reseller_id) is smaller then the ALLOCATION BLOCK, return TRUE else return FALSE;
        return FALSE;
}

然后你的控制器看起来像这样:

     public function add_user() { 

            $usertype = $this->session->userdata('usertype'); 
            $this->load->model('reseller_m'); 

            if ($usertype == "admin") { 
                $this->data['user'] = $this->user_m->get_new(); 
                $rules = $this->user_m->rules_admin; 
                $rules['password']['rules'] .= '|required'; 
                $this->form_validation->set_rules($rules); 

                if ($this->form_validation->run() == TRUE) {
                    if($this->reseller_m->check_reseller_allocation($reseller_id)){//check_reseller_allocation will return TRUE if ALLOCATION BLOCK value is not reached (so you can add another user) and FALSE if it is reached, so you cannot add another user
//if TRUE (you can add one more). then the script continues, adding the user
                        $data = $this->user_m->array_from_post(array('sip_id', 'sip_pass', 'name', 'key', 'email', 'password', 'phone', 'status', 'created', 'balance'));
                        $data['password'] = $this->user_m->hash($data['password']); 

                        $key = $this->user_m->save($data, $id); 
                        redirect('admin/user'); 
                    }else{//if FALSE (ALLOCATION BLOCK val is reached) display some error view; I called it 'max_allocation', but you may call it whatever you want
                     $this->load->view('max_allocation');}
                } 

                $resellers = $this->reseller_m->get_drop_down(); 
                if(count($resellers) > 0) { 
                    foreach($resellers as $value) { 
                        $dropdown[$value->key] = $value->key; 
                    } 
                } 
                $this->data['resellers'] = $dropdown;
                $this->data['subview'] = 'admin/user/add'; 
                $this->load->view('admin/_layout_main', $this->data); 
            } else { 
                $this->load->view('permission'); 
            } 
        }

编辑:我误认为reseler_users_count()中的表名。现在好了。您必须更改&#39; users_tbl&#39;用户名表的实际名称。对于&#39; reselers_tbl&#39;相同,请使用转销商表的实际名称对其进行标记。此外,两个函数都将转销商的ID作为参数。在add_user()控制器功能中,您必须具有该值。也许你应该传递$ _POST。