如何在Codeigniter中创建更新数据库onclick的递增按钮?

时间:2015-08-13 06:33:18

标签: php html codeigniter

我是codeigniter的新手。我有一个项目管理员分配给它的经销商平衡。现在我想在html中创建一个按钮,它为管理员提供了增加按钮点击余额的界面。当他增加它时,值也应该在数据库中更新。也许这很简单,但我无法做到这一点!我想我应该使用ajax或Jquery这可能<​​/ p>

下面是我的视图代码:(想要在余额输入之外添加递增和递减按钮)

<?php echo validation_errors(); ?>
<?php echo form_open(); ?>
<table class="table">
    <tr>
        <td>SIP Username</td>
        <td><?php echo form_input('sip_username', set_value('sip_username', $user->sip_username)); ?></td>
    </tr>
    <tr>
        <td>SIP Password</td>
        <td><?php echo form_input('sip_password', set_value('sip_password', $user->sip_password)); ?></td>
    </tr>
    <tr>
        <td>Key</td>
        <td><?php echo form_input('key', set_value('key', $user->key), 'readonly'); ?></td>
    </tr>
    <tr>
        <td>Allocation Block</td>
        <td><?php echo form_input('allocation_block', set_value('allocation_block', $user->allocation_block)); ?></td>
    </tr>
    <tr>
        <td>Name</td>
        <td><?php echo form_input('name', set_value('name', $user->name)); ?></td>
    </tr>   
    <tr>
        <td>Reseller 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>User_Required</td>
        <td><?php echo form_input('user_num', set_value('user_num', $user->user_num)); ?></td>
    </tr>
    <tr>
        <td>Balance</td>
        <td><?php echo form_input('balance', set_value('balance', $user->balance)); ?></td>
    </tr>
    <tr>
        <td>Phone</td>
        <td><?php echo form_input('phone', set_value('phone', $user->phone)); ?></td>
    </tr>

    <tr>
        <td>Address</td>
        <td><?php echo form_input('address', set_value('address', $user->address)); ?></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>Country</td>
        <td><?php echo form_input('country', set_value('country', $user->country)); ?></td>
    </tr>

    <tr>
        <td>Country Code</td>
        <td><?php echo form_input('country_code', set_value('country_code', $user->country_code)); ?></td>
    </tr>
    <tr>
        <td></td>
        <td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
    </tr>
    </table>
    <?php echo form_close();?>

控制器:

public function edit ($id = NULL)
{
    // Fetch a user or set a new one
    if ($id) {
        $this->data['user'] = $this->reseller_m->get($id);
        count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
    }
    else {
        $this->data['user'] = $this->reseller_m->get_new();
    }

    // Set up the form
    $rules = $this->reseller_m->rules_admin;
    $id || $rules['password']['rules'] .= '|required';
    $this->form_validation->set_rules($rules);

    // Process the form
    if ($this->form_validation->run() == TRUE) {



        $data = $this->reseller_m->array_from_post(array('sip_username','sip_password','key','allocation_block','name','email','password','phone','balance','user_num','address','country','country_code','created','modified','status'));


        $data['password'] = $this->reseller_m->hash($data['password']);

        $key=$this->reseller_m->save($data, $id);

    //here we get the last inserted record id in $last_id
        $last_id = $this->db->insert_id();

    //The logic to create blank rows in user table mapped to reseller_id    

        $values=array($this->input->post('name'),$this->input->post('country_code'),$this->input->post('allocation_block'),$this->input->post('user_num'));

        $key=implode('-',$values);

        $this->db->where('id',$last_id);
        $this->db->update('reseller',array('key'=>$key));

            for($i=1; $i<=$data['user_num'];$i++)
            {
            $userdata=array('key'=>$key);
        // here users is taken name of user table with retailer_id is field
             $this->db->insert('users',$userdata);
             }


        redirect('admin/reseller');
    }

    // Load the view
    $this->data['subview'] = 'admin/reseller/edit';
    $this->load->view('admin/_layout_main', $this->data);
}

1 个答案:

答案 0 :(得分:4)

如果您熟悉ajax,可以使用ajax。

单击该按钮,您可以调用ajax并在控制器函数中编写代码以更新数据库中的值。