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);
}
}
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 use smarty and ci 3.0.6 can I use a base controller in my core? I do it but I don't have any value in my view
controller:
class MY_Controller extends CI_Controller {
public $stats;
public $title;
public function __construct() {
parent::__construct();
$this->load->model('User_Model');
$var->stats = $this->User_Model->count_Unverified_adviser();
$t=$var->stats->Unverified;
$var->title = 'title';
$this->custom_smarty->assign('vars',$var);
$this->load->vars($var);
} }
my controller that load view:
class Adviser extends MY_Controller {
function __construct()
{
parent::__construct();
$this->load->model('User_Model');
}
public function index()
{
$this->custom_smarty->assign('url',base_url());
$this->custom_smarty->display('test.tpl');
}
my test.tpl:
<td>{$vars.title}</td>
You are giving an object to the assign function.
To access $vars.title in this way you should give an array:
$this->custom_smarty->assign('vars', array( 'title' => 'somevalue' ));
CodeIgniter session available in one controller but not in other...
Session is setting user controller
class User extends CI_Controller {
// SEssion worked here
public function __construct()
{
parent::__construct();
session_start();
}
function setSess (){
// database model call, Value comes from database
$_SESSION['user'] = array ( 'isLoggedIn' => true,
'id' => $userData[0]['id'],
'username' => 'ABC',
'email_address' => $userData[0]['email_address'],
'country' => $userData[0]['country'],
'lastLoggedin' => $lastLoginTime
);
// Redirect to profile
}
}
Unable to receive it in
class Profile extends CI_Controller {
public function __construct()
{
parent::__construct();
session_start();
}
public function index() {}
public function display() {
ECHO "<PRE>";
print_r($_SESSION);
$data['title'] = 'Profile of '.$_SESSION['user']['username'];
// Gives error here while echoing $_SESSION['user']['username']
}
}
What am i missing here? Any suggestion?
Do not use session_start(), instead, load CodeIgniter session library
In User Controller:
class User extends CI_Controller {
public function __construct()
{
parent::__construct();
if(!isset($this->session)) {
$this->load->library('session'); # do not use 'session_start();'
}
}
function setSess (){
$_SESSION['user'] = array ( 'isLoggedIn' => true,
'id' => $userData[0]['id'],
'username' => 'ABC',
'email_address' => $userData[0]['email_address'],
'country' => $userData[0]['country'],
'lastLoggedin' => $lastLoginTime
);
}
...
}
In Profile Controller:
class Profile extends CI_Controller {
public function __construct()
{
parent::__construct();
if(!isset($this->session)) {
$this->load->library('session');
}
}
...
}
Also make sure your config file is setup correctly. Follow documentation here CodeIgniter Sessions
So here is my controller:
class Search extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('search_model');
$this->search_model->search_result = $_POST;
}
public function index()
{
$data['results'] = $this->search_model->get_results();
$this->load->view('search_results', $data);
}
And here is my model:
class Search_model extends CI_Model {
protected $search_query;
function __construct($search_query)
{
parent::__construct();
$this->load->database();
$this->search_query = $search_query;
}
But this doesn't seem to work. What I want to do is pass the posted form ($_POST) to my model, then do stuff with it. But it seems messy to pass $_POST to each method of my model. My plan is to extract the variables sent with $_POST and construct these as properties such as $website_url, $text_query etc..., then call these in methods with $this->website_url;
I'm relatively new to CodeIgniter so just getting to grips with the basics
for your special purpose you can try this code
Controller:
class Search extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('search_model');
$this->init();
}
private function init()
{
$this->search_model->init( $this->input->post() );
}
public function index()
{
$data['results'] = $this->search_model->get_results();
$this->load->view('search_results', $data);
}
model:
class Search_model extends CI_Model {
protected $search_query;
function __construct()
{
parent::__construct();
$this->load->database();
}
public function init( $search_query )
{
$this->search_query = $search_query;
}
you have protected $search_query; which you can't access it from your controller.
You either have to change it to public or create getter and setter for it. or just getter depending on your domain/business logic.
And it should have been obvious as you should get an error saying
Fatal error: Cannot access protected property in file some/path/to/file!
Don't put the 'search query' in your model constructor.
Controller:
class Search extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('search_model');
}
public function index()
{
if ($this->input->server('REQUEST_METHOD') == 'POST')
{
// you should probably validate/clean the post data instead
// of sending it straight to the model
$results = $this->search_model->get_where($_POST);
}
else
{
// if it's not a post, you probably need something...
// either here, or somewhere in your view to handle empty data
$results = array();
}
$data['results'] = $results
$this->load->view('search_results', $data);
}
Your Model:
class Search_model extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->database(); // <--- you may want to autoload 'database' library
}
function get_where($where)
{
$this->db->where($where);
// add select, order, joins, etc here
return $this->db->get('mytable'); // whatever your table name is
}
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