I have created the one custom Module in Opencart.
The details of that:
Controller: file named block.php in admin/controller/custom/block.php
View : file named block.tpl in admin/view/template/custom/block.tpl
Model : file named block.php in admin/model/custom/block.php
I have successfully configured permissions.
When I load this model in Controller means, I got the following error..
Fatal error: Call to a member function load() on a non-object in F:\xampp\htdocs\shirtrecipe\admin\controller\custom\block.php on line 11
Controller Code:
<?php
class ControllerCustomBlock extends Controller
{
public function index() {
$this->language->load('custom/block');
$this->model->load('custom/block'); /* doesnt load this model */
$this->data['breadcrumbs'] = array();
...
...
Model Code:
<?php
class ModelCustomBlock extends Model
{
public function get_demo_block() {
$sql = "select demo_block_img from oc_block where id=1";
$query = $this->db->query($sql);
return $query->row;
}
}
Change :
$this->model->load('custom/block');
To:
$this->load->model('custom/block');
Have a nice day !!
Related
I am a newbie to Codeigniter and trying to build a service registration page. Not just the page but the whole website has this error.
$autoload['model'] = array('User_model','Material_model');
$autoload['libraries'] = array('database','form_validation');
class services extends CI_Controller {
public function service1()
{
$this->form_validation->set_rules('type','Type',trim|required);
$this->form_validation->set_rules('area','Area',trim|required);
$this->form_validation->set_rules('quantity','Quantity',trim|required);
$this->form_validation->set_rules('details','Details',trim|required);
$this->form_validation->set_rules('name','Name',trim|required);
$this->form_validation->set_rules('number','Number',trim|required);
$this->form_validation->set_rules('location','Location',trim|required);
if($this->form_validation->run()==FALSE){
$data['main_view']="page_view";
$this->load->view('layouts/main',$data);
} else{
$this->user_model->create_user();
redirect('/');
}
I'm getting this error in user model from material model. Th
Actually, you should load the model in your parent construct.
For example:
class services extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('your_directory_model, 'your_initialize_model');
}
}
And then, if you want to use your model. you can build code like i write below in your method.
$this->your_initialize_model->your_model_method();
I am trying to port an application's few functions from a CodeIgniter application to another existing CodeIgniter application. Both applications on themselves are working very well but when I added this thing it gives the following error:
Fatal error: Call to a member function order_by() on null in …\application\core\MY_Model.php on line 7
In this question I have removed parts unrelated to the error to simplify code.
//MY_Model.php model file
<?php
class MY_Model extends CI_Model {
protected $_order_by = '';
public function get(){
$this->db->order_by($this->_order_by);
}
}
//article_m.php model file
<?php
class Article_m extends MY_Model
{
protected $_order_by = 'pubdate desc, id desc';
}
//frontend.php controller file
<?php
class Frontend extends MY_Controller
{
function __construct()
{
$this->load->model('article_m');
}
function index()
{
$this->article_m->get();
}
}
Please help. Thank you!
whenever calling any $this->db ... you have to make sure to load your database library. Check in application\config\autoload.php for the following:
$autoload['libraries'] = array('database');
I'm a beginner in cakephp. I want to create a custom query and retrieve data from a table in cakephp. I have a table called key_uniqueword in the database.
Here is my code:
MODEL:
key_uniqueword.php
<?php
class key_uniqueword extends AppModel{
var $name = 'key_uniqueword';
public function get()
{
$this ->query("SELECT * FROM key_uniqueword");
}
}
Controller:
ReadingManualsController.php
<?php
class ReadingManualsController extends AppController{
var $name = 'ReadingManuals';
function index(){
$this ->set('results',$this ->key_uniqueword-> get());
}}
view:
index.ctp
<!DOCTYPE html>
<?php
foreach($results as $result)
{
//display results here
}
?>
But I'm getting this error message.
Error: Call to a member function get() on a non-object
File: F:\xampp\htdocs\18\cake\app\Controller\ReadingManualsController.php
Line: 22
How can I correct this?
Your model file :
KeyUniqueword.php
<?php
class KeyUniqueword extends AppModel{
public $name = 'KeyUniqueword';
public $useTable = 'key_uniqueword'; // use own table name
public function get()
{
return $this ->query("SELECT * FROM key_uniqueword");
}
}
Your controller:
<?php
class ReadingManualsController extends AppController{
public $name = 'ReadingManuals';
public $uses = array('KeyUniqueword','ReadingManual')
function index(){
$this ->set('results',$this ->KeyUniqueword-> get());
}}
Firstly, your get() function does not return anything! Also, '$this->key_uniqueword' does not yet exist. There are two main ways to prepare that variable, including:
class ReadingManualsController extends AppController{
var $name = 'ReadingManuals';
public $uses = array('key_uniqueword');//populates $this->key_uniqueword
...
Or you can create it before using it, e.g. inside the controller:
$this->loadModel('key_uniqueword');
//now you can use $this->key_uniqueword
But... You should realy do a simple CakePHP tutorial and follow the conventions. In rare/tricky cases a custom query may be required, but this is not one of them.
I used multiple layouts in Laravel using code as below, it works.
class UsersController extends BaseController {
public $layout = 'layouts.default';
...
}
But now I want to change the layout in Method (In UsersController)
public function myFunc(){
//I want to change myFunc's layout to 'default2'
$this->layout->content = View::make('user.myfunc');
}
How can I do? Because when I use $this->layout = 'layouts.default2'
it always returns me ErrorException: Attempt to assign property of non-object
You can use this in your controller method :
public function myFunc(){
$this->layout = View::make('layouts.default2');
$this->layout->content = View::make('user.myfunc');
}
I am a beginner in codeigniter and currently I am working on a shopping cart, taking help from http://net.tutsplus.com/tutorials/php/how-to-build-a-shopping-cart-using-codeigniter-and-jquery/ tutorial. I am using codeigniter 2.1.3.
I am getting an error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Cart::$load
Filename: controllers/cart.php
Line Number: 7
Fatal error: Call to a member function model() on a non-object in D:\xampp\htdocs\ci\application\controllers\cart.php on line 7
Can someone please tell me why it is not working?
The name of my controller is cart.php
<?php
class Cart extends CI_Controller {
public function Cart()
{
//parent::CI_Controller(); // We define the the Controller class is the parent.
$this->load->model("cart_model"); // Load our cart model for our entire class
}
public function index()
{
$data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
print_r($data['products']);
//$data['content'] = 'cart/products'; // Select view to display
//$this->load->view('index', $data); // Display the page
}
}
?>
and my model is cart_model.php
<?php
class Cart_model extends CI_Model{
//public function _construct(){
//parent::_construct();
//}
public function retive_products(){
$query = $this->db->get("products");
return $query->result_array();
}
}
/* End of file cart_model.php */
/* Location: ./application/models/cart_model.php */
?>
Codeigniter 2.1.3 is intended to support PHP 5.2.4 and newer.
Change the class constructor:
<?php
class Cart extends CI_Controller {
public function __construct()
{
parent::__construct();
}
instead of
public function cart()
{
parent::CI_Controller();
Try to review your code. You calling the retrieve_products function on model but in model you have retive_products function .
Controller
public function index() {
$data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
print_r($data['products']);
//$data['content'] = 'cart/products'; // Select view to display
//$this->load->view('index', $data); // Display the page
}
Model
public function retive_products() {
$query = $this->db->get("products");
return $query->result_array();
}