这是我的项目控制器,它显示项目列表,并从items_view.php查看它。
class Items extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('crud');
}
function index(){
$data['items'] = $this->crud->read('items');
$data['header_info'] = $this->crud->header_info('items');
#$data['single_row'] = $this->crud->read_single('items','ID', 1);
$this->load->view('items_view', $data);
}
function edit($id){
$data['single_row'] = $this->crud->read_single('items','ID', $id);
$this->load->view('items_view', $data);
}
function insert(){
if($_POST){
$data = array(
'Desc' => $_POST['Desc'],
'Cost' => $_POST['Cost']
);
if($_POST['status']=='update'){
$this->crud->update('items', $data, 'ID',$_POST['id']);
echo "Updated...";
}elseif ($_POST['status']=='new'){
$last_row = $this->crud->insert('items', $data);
if($last_row){
#Data insertion completed.....
#now ready to get back my items list page.....!
}
}
}
}
}
在items_view.php也有表单,可以用户添加一些更多的项目到列表,所以我想,当用户提交表单的插入方法将执行,所以如何回到我以前的页面而不丢失数据。
在insert()
中获取插入行或更新行的id,然后像这样重定向到index()
redirect("items/index/".$last_row);
In index()
function index($id = ""){
if($id) {
// fetch data from db and pass it to view
} else {
// pass empty value to view
}
$data['items'] = $this->crud->read('items');
$data['header_info'] = $this->crud->header_info('items');
$this->load->view('items_view', $data);
}