coder
coder

Reputation: 159

codeigniter help causing error while loading model

I am not sure whats wrong with my code.......it causing an error while loading model....... please help........... my controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Exams extends CI_Controller {
    public function index(){
        $ob = new exam_class();
        $ob->set_exam(0,'Html exam','1','20');
        $ob->create_exam();
        echo 'success';
    }
}

class exam_class{
    private $id;
    private $title;
    private $catagory;
    private $timeLength;

    function set_exam($id,$title,$catagory,$timeLength){
        $this->id = $id;
        $this->title = $title;
        $this->catagory = $catagory;
        $this->timeLength = $timeLength;
    }

    function create_exam(){
        $this->load->model('examModel');
        $this->examModel->create_exams($title,$catagory,$timeLength);
    }
}

model

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ExamModel extends CI_Model {
    public function create_exams($title,$catagory,$timeLength){
        $data = array(
           'title' => $title ,
           'catagory' => $catagory ,
           'timeLength' => $timeLength
        );

        $this->db->insert('exams', $data);
    }
}

error A PHP Error was encountered

Severity: Notice

Message: Undefined property: exam_class::$load

Filename: controllers/exams.php

Line Number: 26

Fatal error: Call to a member function model() on a non-object in C:\xampp\htdocs\exam\application\controllers\exams.php on line 26

Upvotes: 1

Views: 124

Answers (3)

Valentino Pereira
Valentino Pereira

Reputation: 1325

Tip : Keep it simple

In your controller :

public function __construct()
{
    parent::__construct();
    $this->load->model('examModel');
}

public function index()
{
    $exam_data = $this->process_exam_data(0,'Html exam','1','20');

    $insert_status = $this->examModel->create_exams($exam_data);

    if($insert_status===TRUE){
        echo "Exam Insert Successful!";
    }
    else{
        echo "Exam Insert Failed"; 
    }
}

public function process_exam_data($id, $title, $category, $timelength)
{
   // Do whatever you want with the data, calculations etc.

   // Prepare your data array same as to be inserted into db
   $final_data = array(
                   'title'      => $processed_title,
                   'catagory'   => $processed_category,
                   'timeLength' => $processed_time
                );
   return $final_data;
}

And in your model :

public function create_exams($data)
{
    $result = $this->db->insert('exams', $data); // Query builder functions return true on success and false on failure
    return $result;
}

Your index function is the main function which does the calling, whereas all the processing work is done in the process_exam_data function.

Have a nice day :)

Upvotes: 0

sharif2008
sharif2008

Reputation: 2798

Along with @shin i want to include that go to this file

....\testproject\application\config\autoload.php

and edit this to add your models

$autoload['model'] = array('modelName1','modelName2');

and to load the models from any time from any controller . This will automatically load your models.No need to add

 $this->load->model('modelName');

Upvotes: 1

shin
shin

Reputation: 32721

You shouldn't put more than one class in a file. Controller should be something like this.

class Exams extends CI_Controller {

    private $id;
    private $title;
    private $catagory;
    private $timeLength;
    public function __construct()
    {
        parent::__construct();
        $this->load->model('examModel');
    }

    public function index(){
        $this->set_exam(0,'Html exam','1','20');
        $this->create_exam();
        echo 'success';
    }   

    function set_exam($id,$title,$catagory,$timeLength){
        $this->id = $id;
        $this->title = $title;
        $this->catagory = $catagory;
        $this->timeLength = $timeLength;
    }

    function create_exam(){
        $this->examModel->create_exams($title,$catagory,$timeLength);
    }
}

Upvotes: 2

Related Questions