PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 29, 2022

[FIXED] How to call model in controller on constructor because I need write only one time

 January 29, 2022     codeigniter, php     No comments   

Issue

I create a model for fetching data then write code

model..

 function get_courses(){
        $this->db->from(TABLE_COURSE);
        $this->db->where('name !=', '');
        $query = $this->db->get();
        $result = '';
        if($query){
            if($query->num_rows() > 0)
                $result = $query->result();
        }
        return $result;
    }

Controller

defined('BASEPATH') OR exit('No direct script access allowed');
class Quiz extends CI_Controller {
    public function __construct(){
        parent::__construct();
        $this->load->model('Mastermodel','',TRUE);
    }
    public function index()
    {
        $data['courses'] = $this->Mastermodel->get_courses();
        $data['view_file'] = "content/quiz/quiz_list";
        $this->load->view('layout/dashboard/layout', $data);
    }
    public function quiz_of_day()
    {
        $data['courses'] = $this->Mastermodel->get_courses();
        $data['view_file'] = "content/quiz/quiz_of_day";
        $this->load->view('layout/dashboard/layout', $data);
    }
    public function quiz_edit()
    {
        $data['courses'] = $this->Mastermodel->get_courses();
        $data['view_file'] = "content/quiz/quiz_edit";
        $this->load->view('layout/dashboard/layout', $data);
    }
}

How to call model in controller on constructor because I need write only one time?


Solution

To achieve what you want you can do the following...

  1. You need to make the $data array a property of the class

  2. Reference the new $this->data throughout the class

  3. You need to move your call to the model in the constructor.

So what you get is this

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

class Quiz extends CI_Controller {

    protected $data = array(); // Old school definition of an array (instead of [])for safety

    public function __construct() {
        parent::__construct();
        $this->load->model('Mastermodel', '', TRUE);

        $this->data['courses'] = $this->Mastermodel->get_courses();
    }

    public function index() {

        $this->data['view_file'] = "content/quiz/quiz_list";
        $this->load->view('layout/dashboard/layout', $this->data);
    }

    public function quiz_of_day() {
        $this->data['view_file'] = "content/quiz/quiz_of_day";
        $this->load->view('layout/dashboard/layout', $this->data);
    }

    public function quiz_edit() {
        $this->data['view_file'] = "content/quiz/quiz_edit";
        $this->load->view('layout/dashboard/layout', $this->data);
    }
}


Answered By - TimBrownlaw
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing