In my model I have return below function for get record id
function getLastInserted()
{
$query = $this->db->select("MAX(`UserID`)+1 as userid")->get("registeration");
return $query->result();
}
Now I want to pass that ID to mycontroller for record insertion
public function newregistration()
{
if ($this->form_validation->run())
{
$data = array(
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}
Now I want to access model function in controller and pass record id in data function How I do ??
set return in model and in controller write
$insert_id=$this->db->insert_id();
In Controller load your model first using
$this->load->model('model_name');
Then call to model's getLastInserted() function.
$ID = $this->model_name->getLastInserted();//you will get id here
In model return $query->row()->userid; insead return of $query->result().
Then modify the controller:
public function newregistration()
{
if ($this->form_validation->run())
{
$data = array(
'UserID'=> $this->model_name->getLastInserted(),
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}
hi i worked out the solution on last inserted id dear here the code:
controller:
function sample() {
$sid=$this->adminmodel->getLastInserted();
$this->adminmodel->newregistration( $sid);
}
model:
function getLastInserted()
{
$query = $query = $this->db->select('id')->order_by('id','desc')->limit(1)->get('employee_outs')->row('id');
return $query;
}
model:
public function newregistration($sid)
{
if ($this->form_validation->run())
{
$data = array(
'UserID'=> $sid,
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}
Related
I can't update data in a database.i don't know what happend.please help me. Display data Press the edit button.But i can not edit data on the database.
I have posted the code below:
Controller.php
public function ajax_update()
{
$data = array(
'placename' => $this->input->post('placename'),
'description' => $this->input->post('description'),
'tel' => $this->input->post('tel'),
'latitude' => $this->input->post('latitude'),
'longtitude' => $this->input->post('longtitude'),
'placetype_id' => $this->input->post('placetype_id'),
'province_id' => $this->input->post('province_id'),
);
$this->tblplace->update(array('placeid' => $this->input->post('placeid')), $data);
echo json_encode(array("status" => TRUE));
}
Modal.php
public function save($data)
{
$this->db->insert('tblplace', $data);
return $this->db->insert_id();
}
public function update($where, $data)
{
$this->db->update('tblplace', $data, $where);
return $this->db->affected_rows();
}
I can get data from the database to show in the textbox. But can't update data.I need to help.Thank you.
Your Controller:
public function ajax_update()
{
//You should also check the post array.
$ajax_post_data = $this->input->post();
log_message('debug', 'Ajax Post Data Array:' . print_r($ajax_post_data, TRUE));
$placeid = $this->input->post('placeid');
$data = array(
'placename' => $this->input->post('placename'),
'description' => $this->input->post('description'),
'tel' => $this->input->post('tel'),
'latitude' => $this->input->post('latitude'),
'longtitude' => $this->input->post('longtitude'),
'placetype_id' => $this->input->post('placetype_id'),
'province_id' => $this->input->post('province_id'),
);
$this->load->model('tblplace');
$updte_status = $this->tblplace->update($placeid, $data);
if($updte_status == TRUE){
echo json_encode(array("status" => true));
} else {
echo json_encode(array("status" => false));
}
}
In Model:
public function update($where, $data)
{
$this->db->where('id', $where)
->update('tblplace', $data);
log_message('debug', 'Last Update Query:' . print_r($this->db->last_query(), TRUE));
log_message('debug', 'Affected row count:' . print_r(count($this->db->affected_rows()), TRUE));
if ($this->db->affected_rows() === 1) {
return TRUE;
}else{
return FALSE;
}
}
Hope this is clear and it will help you to update the data.
There is no where condition in your update function
public function update($where, $data)
{
$this->db->where($where);
$this->db->update('tblplace', $data);
return $this->db->affected_rows();
}
I am trying to load all rows for my REST API through Postman.
I am using codeigniter-base-model MY_Model.php.
https://github.com/jamierumbelow/codeigniter-base-model
This is how my code currently looks like both in my controller/model:
Controller(api_news.php):
class Api_News extends REST_Controller {
function __construct()
{
parent::__construct();
}
function index_get()
{
$id = $this->uri->segment(3);
$this->load->model('News_model');
$news = $this->News_model->get_by(array('id' => $id));
if(isset($news['id'])) {
$this->response(array(
'message' => 'success',
'status' => 'true',
'data' => $news));
} else {
$this->response(array(
'message' => 'unsuccess',
'status' => 'false'));
}
}
}
Model(news_model.php):
class News_model extends MY_Model{
protected $_table = 'news';
protected $primary_key = 'id';
protected $return_type = 'array';
}
At the moment if I access:
localhost/my_api/api_news/id/1, 2, 3, etc...
I can access any record by its individual ID and it shows up which is great.
BUT I also want to be able to see all rows by doing this:
localhost/my_api/api_news/id/
and have all rows showing at once.
But I am not sure how to do this...and am getting an unsuccess/false if I try.
Can you please show me how? I am new to PHP in general and I appreciate any help.
Thank you so much!!
Make some changes in your Controller function as below -
function index_get(){
$id = $this->uri->segment(3);
$this->load->model('News_model');
// pass $id to model
$news = $this->News_model->get_by( $id );
if( !empty( $news ) ) {
$this->response(array(
'message' => 'success',
'status' => 'true',
'data' => $news));
} else {
$this->response(array(
'message' => 'unsuccess',
'status' => 'false'));
}
}
And in your model make id parameter optional and then check that if id is passed get data based on id otherwise return all data as below -
// $id variable is optional here
function get_by( $id = '' ) {
if ( $id == '' ) {
$news = $this->db->get( 'news' );
}
else {
$news = $this->db->get_where( 'news', array( 'id' => $id ) );
}
// return data to controller
return $news->result();
}
So if you enter id in API then data will be based on that id otherwise all data will be returned.
i want to insert records of fifty persons to the database at one time(this can be increase).when user click the save button data should be save at the database.what is the best way to do it?
Here is my controller
function month_end_data()
{
$data_save = array(
'employee_id' => $emp,
"annual" => $annual,
"casual" => $casual
);
$id = $this->monthend_mod->savemonthenddata($data_save);
echo json_encode(array("status" => TRUE, "id" => $id));
}
Model
class monthend_mod extends CI_Model
{
public function savemonthenddata($data_save = array())
{
if ($this->db->insert_batch('pay_monthend', $data_save)) {
return true;
} else {
return false;
}
}
}
view
<button onclick="month_end_data()" class="btn btn-danger">Save</button>
Try to send in a multidimensional array:
$data = array(
array(
'employee_id' => 'xx',
//... more fields
),
array(
'employee_id' => 'yy',
//... more fields
),
array(
//... and so on
),
);
...and then send that to your savemonthenddata()-method.
Here i am getting inserted data to database but how to write this in foreach loop to get multiple data please help me as a fresher am totally confused..
My controller
class Student extends CI_Controller {
public function _construct()
{
parent::_construct();
//call model
$this->load->model("StudentModel","m");
}
function index()
{
$this->load->view("index");
}
function savedata()
{
//create array for get data from index
//$data=array(
// 'studentname' => $this->input->post('studentname'),
//'gender' => $this->input->post('gender'),
//'phone' => $this->input->post('phone')
// );
$data = array(
array(
'studentname' => 'Reddy' ,
'gender' => 'Male' ,
'phone' => '456879'
),
array(
'studentname' => 'Yalla' ,
'gender' => 'Female' ,
'phone' => '12345678'
)
);
//mean that insert into database table name tblstudent
$this->db->insert_batch('tblstudent',$data);
//mean that when insert already it will go to page index
redirect("Student/index");
}
function edit($id)
{
$row=$this->m->getonerow($id);
$data['r']=$row;
$this->load->view('edit',$data);
}
function update($id)
{
$id=$this->input->post('id');
$data=array(
'studentname' => $this->input->post('studentname'),
'gender' => $this->input->post('gender'),
'phone' => $this->input->post('phone')
);
$this->db->where('id',$id);
$this->db->update('tblstudent',$data);
redirect("Student/index");
}
function delete($id)
{
$id=$this->db->where('id',$id);
$this->db->delete('tblstudent');
redirect("Student/index");
}
}
My model
class StudentModel extends CI_Model{
function _construct()
{
parent::_construct();
}
function gettable()
{
$query=$this->db->get('tblstudent');
return $query->result();
}
function getonerow($id)
{
$this->db->where('id',$id);
$query = $this->db->get('tblstudent');
return $query->row();
}
}
For CodeIgniter 3.x: insert_batch
For CodeIgniter 2.x: insert_batch
Is there a way that I can update a form input using something like insert_id?
I have a form for adding factories to a table and I use the insert_id to store the factories id in another table too after submit.
this all works great, but is there such a way for $this->db->update?
My model for updating:
function updatebedrijf($id, $data)
{
$this->db->where('idbedrijven', $id);
$this->db->update('bedrijven', $data);
}
My controller function for updating:
function updatebedrijven()
{
$dbres = $this->db->get('categorieen');
$ddmenu = array();
foreach ($dbres->result_array() as $tablerow) {
$ddmenu[$tablerow['idcategorieen']] = $tablerow['Categorie'];
}
$data['opties'] = $ddmenu;
$data['info'] = $this->members_model->getbedrijf($id);
$data['id'] = $this->uri->segment(3);
$this->load->view('members/header');
$this->load->view('members/editform', $data);
$this->load->view('members/footer');
}
for adding factories i use this with insert_id:
controller:
function addbedrijven()
{
$this->members_model->addbedrijf();
redirect('members/index');
}
model:
function addbedrijf()
{
$data1 = array(
'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'),
'Postcode' => $this->input->post('Postcode'),
'Plaats' => $this->input->post('Plaats'),
'Telefoonnummer' => $this->input->post('Telefoonnummer'),
'Email' => $this->input->post('Email'),
'Website' => $this->input->post('Website'),
'Profiel' => $this->input->post('Profiel'),
'Adres' => $this->input->post('Adres'),
'logo' => $this->input->post('logo')
);
$this->db->insert('bedrijven',$data1);
if($this->db->affected_rows() >= 1)
{
$to_bedrijfcategorieen['idbedrijven'] = $this->db->insert_id();
$to_bedrijfcategorieen['idcategorieen'] = $this->input->post('categorieen');
$this->insert_bedrijfcat($to_bedrijfcategorieen);
}else{
return FALSE;
}
}
function insert_bedrijfcat($data1)
{
$this->db->insert('bedrijfcategorieen', $data1);
return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
}
Hope it's clear enough.