Codeigniter在同一页面上读取和更新

时间:2015-02-23 11:33:36

标签: codeigniter

我有一个显示表中数据的函数和另一个更新表的函数。问题是,当我调用函数Edit时我想从表中检索数据并使用Edit表单加载一个新视图,两者都在同一页面上,但不知怎的当我尝试编辑我的表中的任何记录时,实际表格因$this->uri->segment(3)而发生变化。

我知道只使用一个函数来读取和更新表来解决这个问题,但在这种情况下解决方案是什么?

这是代码:

class BASE_Controller extends CI_Controller {


function __construct()
{
    parent::__construct();

}

public function render($viewName, $data = null ){

    if(empty($viewName)){
        die('view name pls');
    }

    $myView = $this->load->view($viewName, $data, true);

$this->load->view('general', array('myView' => $myView));
}
}

public function display(){
    if($this->session->userdata('is_logged_in') !== 1 ){
        die('Restricted area');
    }else{
        $this->load->library('pagination');
        $config['base_url'] = 'http://localhost:80/ci/home/display';
        $config['total_rows'] = $this->homemodel->total_rows();
        $config['per_page'] = 5;
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        if($this->uri->segment(3) >0){
        $startpage = $this->uri->segment(3)*$config['per_page']-$config['per_page'];
        }else{
            $startpage = $this->uri->segment(3);
             }
        $data['rec'] = $this->homemodel->AfisareModel($config['per_page'],$startpage);
        $data['links'] = $this->pagination->create_links();
        $this->render('display',$data);

 public function edit($id){
 $this->display();
    $data = array(
        'username' => $this->input->post('name'),
        'password' => $this->input->post('password'),
        'email' => $this->input->post('email')
    );

    $data['id'] = $id;

        $data['record'] = $this->homemodel->edit_model($id, $data);

    $this->load->view('edit',$data);
}

1 个答案:

答案 0 :(得分:0)

尝试查看,如下所示

<form method="post" id="add_grocery_form" name="add_grocery_form">
        <li><label>user_name</label>
            <?php
             $data=array(
                'name'=>'user_name',
                'id'=>'user_name',
                'value'=>@$user_name!=''? @$user_name : set_value('user_name'),
                'onmouseover'=>"js:this.focus()"
            );
               echo form_input($data);
            ?>
        </li>
            <li>
           <?php
            $data=array(
            'type'=>'submit',
            'name'=>'submit',
            'class'=>'submit-btn',
            'style'=>'float:right',
            'value'=>'submit'
            );
            echo form_input($data);
           ?>
        </li>  
    </ul>
</form>

您可以使用if condition在控制器中执行select语句。例如,您可以传递一个参数用于编辑选项。然后使用参数创建控制器以获取数据库值,然后执行以下操作

if ($sno != '') {
        $stock_result=$this->mess_stock_model->select_mess_stock($sno);
        $data['user_name'] = $stock_result[0]->user_name;
    }

'value'=>@$user_name!=''? @$user_name : set_value('user_name')

set_val命令将有助于替换同一页面中的数据库值。