MVC - Model not working correctly - php

This is the code for my controller:
class products extends CI_Controller {
public function index() {
$this->load->model('getproducts');
$this->load->view('productlist');
}
}
?>
When I load this, it returns a blank page, however when I comment out the line that loads the model, it returns the view as expected (pointing to an error in the model). Here is the code for the model:
<?php
class getproducts extends Model {
function listProducts() {
$query = $this->db->get('ProjectProducts');
if ($query->num_rows() > 0) {
foreach ($query->result() as $row {
$productdata[] = $row;
}
return $productdata;
}
}
}
?>
Am I missing something obvious here? I'm not even using any of the data from the model yet.

What version you use of codeigniter
please note that you should extends CI_Model not model
class getproducts extends CI_Model {

Please make sure your controller start letter be capital ie. upper
case and extend it as CI_Model

you are missing
function __construct()
{
parent::__construct();
}
and this code before your function listProducts and after class starting hopefully this will resolve your issue.
and also use same function in your controller as well

Related

Add another controller in codeigniter

Hello I'm currently facing a problem of adding another controller and the problem is that
I have 2 controllers
class 1st_Controller extends CI_Controller {
}
and
class 2nd_Controller extends CI_Controller{
my 2 models are working perfectly fine and the only problem is that I need to call 1 model per each controller
for example
1st controller is for 1st model and 2nd controller is for 2nd model.
Now what I've tried so far is
class 2nd_Controller extends 1st_Controller {
public function __construct()
{
header("Access-Control-Allow-Origin: *");
parent::__construct();
$this->load->model('2nd_model','2ndmodel');
$this->load->helper('url');
$this->load->library("pagination");
$this->load->library("session");
}
public function index()
{
$data['title'] = 'System Login';
$get_all_inv = $this->2ndmodel->get_all();
$data["tryvariable"] = $get_all_inv;
$this->template->load('default_layout','contents','myview2nd', $data);
}
}
I tried echo in my view like this
<?php echo $tryvariable; ?>
but no luck because the error says that it is an undefined variable .
Your second controller can't be define cause it's not define as subclass_prefix in your codeigniter application.
class 2nd_Controller extends 1st_Controller { //codeigniter don't recognize this.
}
The simpliest way to solve your problem is.. the fact that you can call multiple models in 1 Controller.
so you can have:
class 1st_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('1st_model','1stmodel');
$this->load->model('2nd_model','2ndmodel');
}
}
or call just once.
class 2nd_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('2nd_model','2ndmodel');
}
}
Hope that helps.
So what I did was write like this on my route to have my 2nd_Controller working
$route['default_controller'] = '1st_Controller';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['2nd_Controller'] = '2nd_Controller';
Now everything is working perfectly. Thanks for all the help

Codeigniter: Multilevel model extends not working. Getting error "Class not found"

I am trying to apply multilevel extends in models.
See my below code.
I have one model "Order", which extends CI's core model
Class Order extends CI_Model {
function __construct() {
parent::__construct();
}
}
Now I am creating new "Seller_order" model from "Order" model
Class Seller_order extends Order {
function __construct() {
parent::__construct();
}
}
Now when i am loading "Seller_order" model inside controller.
class Seller_order_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Seller_order');
}
}
At the time of loading i am getting following error:
Fatal error: Class 'Order' not found
Help please.
Do i need to load first "Order" model then "Seller_order"??
I think i don't need to load "Order" model if i am extending it.
I'm not going to wrap this in a lot of words, hoping the code itself might explain what is needed.
I've added in some Debug echo's to help show how things run, which I did as I "played" with this to figure it out.
I'll assume the following layout... Not as you have it, so you'll have to change it to suit.
application
-> controllers
-> Seller_order_controller.php
-> models
-> Order.php
-> Seller_order.php
Controller - Seller_order_controller
class Seller_order_controller extends CI_Controller {
function __construct() {
parent::__construct();
echo "construct(): I am the <b>Seller Order Controller</b> Constructor<br>";
$this->load->model('seller_order');
}
public function index() {
echo "This worked";
echo '<br>';
echo $this->seller_order->show_order();
}
}
Model - Seller_order.php
require APPPATH.'models/Order.php';
Class Seller_order extends Order {
function __construct() {
parent::__construct();
echo "construct(): I am the <b>Seller Order</b> Constructor<br>";
}
}
Model - Order.php
Class Order extends CI_Model {
function __construct() {
parent::__construct();
echo "construct(): I am the <b>Order</b> Constructor<br>";
}
public function show_order() {
echo "This is showing an Order";
echo '<br>';
}
}
As a side note: Not sure why you would want to extend models like this. The usual rule is each module has it's own model(s).
I've never needed to do this, but if I ever do, now I know how.

Codeigniter Error Unable to locate the specified class: Loader.php

i am trying to make a core class in my codeigniter but its giving error that Unable to locate the specified class: Loader.php.
My Core class is MY_base.php and code is.
class MY_base extends CI_Controller{
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
My model Mod_practice.php code is
class Mod_practice extends CI_Model{
public function get_header(){
$query = $this->db->get('header');
$result = $query->result_array();
return $result;
}
}
My home.php ( main controller) code is
class Home extends MY_loader{
function index(){
parent::MY_base();
}
}
but when i try to run Home controller its giving me the following error
Unable to locate the specified class: Loader.php.
Where can be the error ? Thanks in advance.
You are doing something wrong. You need to construct CI_controller first.
class MY_base extends CI_Controller{
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
And now you can do this:
class Home extends MY_base{
public function __construct()
{
parent::__construct();
// Here you have access to load_header() function
}
}
Ou - and also your create MY_base and then refer to MY_loader.
may bee you can use HMVC Codeigniter https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

Simple Code Igniter Controller and Model causing a 500 error - no CI error shown

I've just started learning Code Igniter and I've run in to a problem almost straight away.
Using the code below and a database setup with the correct privileges I get a 500 error (server error). CI is not throwing an error and I can't see anything wrong with this code.
I'm using the latest version of CI and testing this code:
Controller: products.php
class Products extends CI_Controller {
public function index() {
$this->load->model('products');
$this->load->view('products');
}
}
Model: products.php
class Products extends CI_Model {
function __construct() {
parent::__construct();
}
function get_products() {
$q = $this->db->get('product');
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
I've put these classes in the directories they should be in with a filename of the same name as the class....if I comment out the load-model code in the controller my view is shown. My database connections are correct because if I change them to something incorrect CI throws an error. There's some sort of problem with the model or the loading of it.
You have conflicting class names, the modell and the controller class both called Products.
Try this:
class Products extends CI_Controller {
public function index() {
$this->load->model('products_model','products');
$this->load->view('products');
}
}
Model: products_model.php
class Products extends CI_Model {
function __construct() {
parent::__construct();
}
function get_products() {
$q = $this->db->get('product');
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
A good convention to use is to name your controller ProductsController and then leave the model Products. So all controllers are SomethingController.
Example:
class ProductsController extends CI_Controller
where the filename become products_controller.php
and the model remains:
class Products extends CI_model
and remains products.php
Now your code should work.

How to load methods of a controller from another one?

I have the following controller:
class Tests extends CI_Controller {
public function update_record_test()
{
//some methods
}
}
?>
But I need to execute Method1 method from Controller1 controller. How can I do it?
There's not much information here, or I may be missing something, but what's wrong with
class Tests extends CI_Controller {
public function update_record_test()
{
$controller1 = new Controller1();
$controller1->Method1();
}
}
If you mean you want to execute a function of your main class CI_CONTROLLER, try this.
parent::Method1();
Check the http://php.net/manual/en/keyword.extends.php for more examples

Categories