1

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

4

3 回答 3

2

你不应该在一个文件中放置一个以上的类。控制器应该是这样的。

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);
    }
}
于 2013-06-10T08:48:26.620 回答
1

连同@shin,我想将其包含到此文件中

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

并编辑它以添加您的模型

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

并从任何控制器随时加载模型。这将自动加载您的模型。无需添加

 $this->load->model('modelName');
于 2013-06-10T09:13:14.700 回答
0

提示:保持简单

在您的控制器中:

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;
}

在你的模型中:

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

您的index函数是执行调用的主要函数,而所有处理工作都在process_exam_data函数中完成。

祝你今天过得愉快 :)

于 2017-05-01T11:33:39.580 回答