getting the id when the data is saved into the database using codeigniter -
getting the id when the data is saved into the database using codeigniter -
hi have form when save, saved database. want when info saved database id on displaying next page. here's controller below in function add_new
<?php if ( ! defined('basepath')) exit('no direct script access allowed'); class create_album extends ci_controller { public function __construct(){ parent::__construct(); $this->load->library('session'); $this->load->model('admin_model', 'am'); $this->load->library('form_validation'); if(!$this->session->userdata('logged_in')){ redirect('login'); } } public function detail($id){ homecoming $id; $this->data['item'] = $this->am->getitem($id); print_r($this->data['item']);exit; } public function add_new(){ $this->form_validation->set_rules('title', 'title', 'required'); $this->form_validation->set_rules('description', 'description', 'required'); if($this->form_validation->run() == false){ $this->data['title'] = 'create new album'; $this->data['logout'] = 'logout'; $this->data['home'] = 'activities'; $session_data = $this->session->userdata('logged_in'); $this->data['id'] = $session_data['id']; $this->data['username'] = $session_data['username']; $this->load->view('pages/admin_header', $this->data); $this->load->view('content/create_album', $this->data); $this->load->view('pages/admin_footer'); }else{ $array = array( 'title'=>$this->input->post('title'), 'description'=>$this->input->post('description') ); $this->am->savealbum($array); $id = $this->db->id; $this->data['item'] = $this->am->getitem($id); homecoming $this->am->savealbum($id); foreach($this->data['item'] $item){ $itemid = $item->id; } homecoming $itemid; redirect('create_album/detail/id/'.$itemid); } } public function index(){ $this->data['title'] = 'create album'; $this->data['logout'] = 'logout'; $this->data['home'] = 'activities'; $session_data = $this->session->userdata('logged_in'); $this->data['id'] = $session_data['id']; $this->data['username'] = $session_data['username']; $this->load->view('pages/admin_header', $this->data); $this->load->view('content/create_album', $this->data); $this->load->view('pages/admin_footer'); } }
my model
<?php if ( ! defined('basepath')) exit('no direct script access allowed'); class admin_model extends ci_model{ public function getitem($id){ homecoming $this->db->select('item.id, item.parent_id, item.title, item.description, item.filename ' ) ->from('item') ->where('item.id', $id) ->get()->result_object(); $this->db->get('item'); } }
?>
can help me figured out? want id when info saved. help muchly appreciated. give thanks you
simply utilize
$this->db->insert_id(); // returns row id.
here how controller should like
public function add_new(){ $this->form_validation->set_rules('title', 'title', 'required'); $this->form_validation->set_rules('description', 'description', 'required'); if($this->form_validation->run() == false){ $this->data['title'] = 'create new album'; $this->data['logout'] = 'logout'; $this->data['home'] = 'activities'; $session_data = $this->session->userdata('logged_in'); $this->data['id'] = $session_data['id']; $this->data['username'] = $session_data['username']; $this->load->view('pages/admin_header', $this->data); $this->load->view('content/create_album', $this->data); $this->load->view('pages/admin_footer'); }else{ $array = array( 'title'=>$this->input->post('title'), 'description'=>$this->input->post('description') ); $this->am->savealbum($array); $id = $this->db->id; $this->data['item'] = $this->am->getitem($id); homecoming $this->am->savealbum($id); foreach($this->data['item'] $item){ $itemid = $item->id; } homecoming $itemid; redirect('create_album/detail/id/'.$this->db->insert_id()); // here? } }
codeigniter
Comments
Post a Comment