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' ));
Related
I need to access $data[] variables in whole site, in all views. I created TestLeangue controller and assign some values to $data but in all views say that variable is not defined
class TestLanguage extends MY_Controller
{
public function __construct() {
parent::__construct();
$this->lang->load("menu","english");
}
function index() {
$data['shipping'] = $this->lang->line('menu_shipping');
$this->load->view('templates/navigation', $data);
}
}
I echo and say that I have undefined variable.
<?= $shipping; ?>
Why don't you use that data like a global variable?
class MY_Controller extends ...
{
protected $data = [];
}
Then everytime when you extend your custom controller you will have access to the general data.
class MyCustomStackOverflowController extends MY_Controller
{
public function index()
{
$this->load->view('index', $this->data);
}
}
Also, you can use $this->data to load it with general information for current controller like:
class MyCustomStackOverflowController extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->data['title'] = 'Stackoverflow';
}
public function index()
{
...
}
}
Use Index of $data as a variable name ($shipping), to get the value that you have assigned as $data['shipping']
Controller
class TestLanguage extends MY_Controller
{
public function __construct() {
parent::__construct();
$this->lang->load("menu","english");
}
function index() {
$data['shipping'] = $this->lang->line('menu_shipping');
$this->load->view('templates/navigation', $data);
}
}
View :templates/navigation
<?php
print_r($shipping);
?>
Am trying to load view file in application/core/MY_Controller.php but its giving as follows.
Message: Undefined property: Edustaticlanding::$load
Filename: core/MY_Controller.php
Function is as follows in MY_Controller.php
function menu(){
$arrr = array();
return $arrdata['menu'] = $this->load->view('menu',$arrr,true);
}
And am calling this function in controller(Edustaticlanding.php) as follows.
function __construct(){
$this->menucontent = $this->menu();
print_r($this->menucontent); die;
}
Pls correct me.. where its going wrong.. thanks
On application/core/MY_Controller.php
Add public $data = array() like below
<?php
class MY_Controller extends CI_Controller
{
public $data = array();
public function __construct()
{
parent::__construct();
$this->data['menu'] = $this->menu();
}
public function menu(){
return $this->load->view('menu', NULL, TRUE);
}
}
On the controller then once add public $data = array(); you can access the menu on view
You have to use $this->data now on controller
<?php
class Example extends MY_Controller {
public $data = array();
public function index() {
$this->data['title'] = 'Welcome to Codeigniter';
$this->load->view('example', $this->data);
}
}
On the example view now you can echo
<?php echo $menu;?>
Add extends CI_Controller to your core controller like following codes:
class MY_Controller extends CI_Controller {
Please check below mentioned solution. You need to call parent constructor first. So it will load all basic configurations.
First extent CI_Controller class and call parent constructor like describe below
class MY_Controller extends CI_Controller {
public function __construct{
parent::__construct();
}
}
Please let me if it not works.
What I'm trying to do on the surface seems simple, basic OOP PHP but I just can't get it working. I have a controller class which is calling a model, that model extends another model of mine but it throws an error saying it can't find it:
Controller (Welcome.php):
public function __construct()
{
parent::__construct();
$this->load->model('users_model');
}
public function index()
{
$this->users_model->getAll();
}
Users Model (User_model.php):
class Users_model extends Base_model
{
public function __construct()
{
parent::__construct();
}
}
Base Model (Base_model.php):
class Base_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function getAll($table)
{
$query = $this->db->query('Query here');
return $query;
}
}
This gives me the error Fatal error: Class 'base_model' not found in /ci/application/models/Users_model.php on line 3
Save your Base_model in application/core named as Base_model.php with following code.
class Base_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function getAll($table=FALSE)
{
$query = $this->db->query('Query here');
return $query;
}
}
Save User_model in application/models named as User_model.php having following code
class User_model extends Base_model
{
public function __construct()
{
parent::__construct();
}
}
Then make a controller Welcome.php in appliation/controllers having following code with extending CI_Controller
public function __construct()
{
parent::__construct();
$this->load->model('user_model');//loads user_model
}
public function index()
{
$data = $this->user_model->getAll(); //need a variable to hold return data
}
You just have to locate the Base_model in your Users_model like below code and you can access the functions of base model easily.
require('application/models/base_model.php');
class User_model extends Base_model
{
function __construct()
{
parent::__construct();
}
}
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 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);
}
}