I am new to CI and I want to update some data in mysql . So here is my controller
class Ci_update extends CI_Controller
{
function __construct() {
parent::__construct();
}
function index()
{
$data = array
(
'title' => 'Data Structure using C',
'text' => 'Data Structure Using C, for, IIIrd Sem VTU CSE students'
);
$id = 4 ;
$this->load->model('ci_update_model');
$this->ci_update_model($data,$id);
}
}
and my model is :
class Ci_update_model extends CI_Model
{
function __construct() {
parent::__construct();
}
function updateData($data,$id)
{
$this->db->where('id',$id);
$this->db->update('data',$data);
}
}
But when I tried to run the program , it says Call to undefined method Ci_update::ci_update_model() in C:\wamp\www\ci\application\controllers\ci_update.php on line 19
What wrom am I doing?
Use as below
$this->load->model('ci_update_model');
$this->ci_update_model->updateData($data,$id);
DO this changes in your Controller code,Rest is same:
$this->load->model('ci_update_model');
$this->ci_update_model->updateData($data,$id);
in your controller's constructer add this line
$this->load->model('Ci_update_model');
and the error will get resolved
Related
How to make a global variable that always changing?
I want to create something like protected $global_data; in controller Global_data.php that can be called by a lot of controller, and return variable $global_data value to whenever controller that has $this->load->library('../controllers/Global_data').
But when I tried to call it, it gives me this error Unable to locate the specified class: Session.php, so I think CodeIgniter 3.1.8 not allowed me to do this.
So how to achieve what I'm looking for? do I need to put it on model instead, library file or is there another way to do it?
Thank you.
Here is Global_data.php content
protected $global_data;
public function __construct()
{
parent::__construct();
$this->global_data = array(
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
);
}
Can_be_anything_controller.php content
class Can_be_anything_controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('../controllers/Global_data');
}
public function index()
{
$data = $this->global_data;
$data['page_title'] = 'Dashboard';
$data['page_directory'] = 'pages/dashboard';
$this->load->view('template', $data);
}
}
You may create a library for that in libraries directory
Global_data.php file
class Global_data{
public $global_data;
protected $CI;
public function __construct() {
$this->CI = & get_instance();
}
public function common_data()
{
$this->global_data= array(
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
);
return $this->global_data;
}
public function any_method(){
$query = $this->CI->db->get('table_name');
}
}
Now you can load it in any controller like
$this->load->library('Global_data')
Then use data
$data = $this->Global_data->common_data();
Also you may use HMVC model to use any method in any controller
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
Make a MY_Controller in application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public $global_data;
public function __construct() {
parent::__construct();
$this->global_data = 'whateveryouwant';
}
public function somemethod() {
return '123';
}
}
then any other controllers in your application/controllers that need to access global_data should extend it like so:
class Somecontroller extends MY_Controller {
public function index() {
echo $this->global_data;
echo $this->somemethod(); // works with methods too
}
}
If you need to run more complex code just put everything into a library or model and autoload it. All public methods and properties are globally available. HMVC seems overkill for what you want.
This kind of data can also be handled very nicely by config files.
/application/config/global_data.php
<php
$config['foo'] = "some foo";
$config['bar'] = 42;
$config['baz'] = array('one', 'two', 'three');
In a controller load the config file with
$this->config->load('global_data');
The access the items using
echo $this->config->item('foo');
echo $this->config->item('bar') * 2; //outputs 84
$data = $this->config->item('baz');
Config documentation
I am trying to fetch items by id but instead its returning null array.The code works fine when I fetch all the items.
Here is the controller:
class Search extends REST_Controller
{
function __construct()
{
// Construct the parent class
parent::__construct();
$this->load->model('Courses_model');
$this->load->model('Courses_category_model');
$this->load->model('Courses_lessons_model');
$this->load->model('Courses_photos_model');
$this->load->model('Providers_model');
}
function index_get()
{
$category_id = $this->get('category_id');
$result = $this->Courses_model->get_courses_category($category_id);
$this->response([
'status' => TRUE,
'data' => $result
], REST_Controller::HTTP_OK);
}
}
and the model file is here and the model file is here:
class Courses_model extends CI_Model
{
public function __construct()
{
// Call the CI_Model constructor
parent::__construct();
}
public function get_courses_category($category_id)
{
$this->db->where('category_id', $category_id);
$query = $this->db->get('courses');
return $query->result();
}
i have seem u r question and find out ans please check the below link i think help u.
https://github.com/chriskacerguis/codeigniter-restserver/blob/master/application/libraries/REST_Controller.php
RestApi Post request using CodeIgniter
hahaa. Finally, after a little snap I solved this. I just defined the routes and it worked like a charm.
When i try to write and run the Code Igniter tutorial, it throws this error:
Call to undefined method News_model::get_news() in application\controllers\news.php on line 21
Here is line 21:
$data['news'] = $this->news_model->get_news($slug);
mews model
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this ->db->get('news')
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
}
If you use your model in controller - you must write this code
$this->load->model('News_model');
$data['news'] = $this->News_model->get_news($slug);
And check line
parent::__construct();
in your controller __construct method.
(PHP does not instantiate the parent constructor automatically if the child defines a constructor, unless the child specifically instantiates the parent's constructor)
If you use model from another side your application you must write this code
$CI = &get_instance();
$CI->load->model('News_model');
$data['news'] = $CI->News_model->get_news($slug);
Without posting more code it looks like there are two things to check here.
First, you have to call the parent constructor in your model. So your News_model constructor should look like this:
function __construct()
{
parent::__construct();
$this->load->database();
}
Second, if the code for line 21 you posted above is correct, the model name has to match your class name. So line 21 should read:
$data['news'] = $this->News_model->get_news($slug);
Note the uppercase 'N' in News_model.
This is essential what yAnTar wrote but phrased differently. Hopefully you'll understand either one of them.
may this will help you
1.In your controller:-
parent::__construct();
$this->load->model('News_model ');
$data['news'] = $this->news_model->get_news($slug);
2.In Model:-
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug)
{
if ($slug)
{
$query = $this ->db->get('news')
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
}
It seems model not loaded.
You have to load model before using it
Either in controller or in autoload array
In controller load model using
$this->load->model('model_name');
And in your case
$this->load->model('news_model');
I am new to codeigniter. I am trying to insert datat to mysql database to a table named class_record. My controller add_record.php coding as below :
class Add_record extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->model('add_record_model');
}
}
And my model add_record_model is as below :
class add_record_model extends CI_Model{
function __construct(){
parent::__construct();
}
function index(){
$data = array(
'roll_number' => 15,
'student_name' => 'Dhrubajyoti Baishya',
'branch_code' => 'CS'
);
$this->db->insert('class_record',$data);
}
}
But when I type http://localhost/codeigniter/index.php/add_record in url data are not inserted to the database. What is the problem ?
You're not actually doing anything in the controller and models don't have index functions the way you're thinking.
You want something like this:
class Add_record extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->model('add_record_model');
$this->add_record_model->insertRecords();
}
}
class add_record_model extends CI_Model{
function __construct(){
parent::__construct();
}
function insertRecords(){
$data = array(
'roll_number' => 15,
'student_name' => 'Dhrubajyoti Baishya',
'branch_code' => 'CS'
);
$this->db->insert('class_record',$data);
}
}
The controller does what it says it controls things. By loading the model all you are doing is exposing the models functions to the controller to use directly. Honestly you would pass the data to the model from the controller as well, the function you have is a good little test function but does nada really. How you really want to be doing it is something along these lines.
class Add_record extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$data = array(
'roll_number' => 15,
'student_name' => 'Dhrubajyoti Baishya',
'branch_code' => 'CS'
);
$this->load->model('add_record_model');
$this->add_record_model->insertRecords($data);
}
}
class add_record_model extends CI_Model{
function __construct(){
parent::__construct();
}
function insertRecords($data){
$this->db->insert('class_record',$data);
}
}
I am doing an ajax call..which doesnt output any mysql errors..Basically, I am trying to delete two records...
public function task_delete($task_id)
{
$this->load->database();
$this->db->delete('task_pages', array('id' => $task_id));
$this->db->delete('tasks', array('id' => $task_id));
}
This is the model..
And this is the controller..
class Ajax extends CI_Controller {
public function delete()
{
$this->load->database();
$this->load->model('tasks_model','task_delete');
$result=$this->task_delete-> task_delete($this->input->post('myId'));
echo $result;
}
}
Where does my code fails?
UPDATE:
I get an error..the method deleteTask is being called on none object
Assuming your model is also called task_delete and the function you want to call is deleteTask, then you have given the function in your model the wrong name, it should be declared as this:
public function deleteTask($task_id)