Search code examples
phpmysqlcodeignitermodelscontrollers

Codeigniter errors for model array


This simple functionality driving me crazy - I'm trying to pull a row from database and pass it as array of information to my controller and same to my view - before passing this to view I need to get the slug field and redirect to domain/quotes/this-is-test to show correct information but I can't even do a basic query. Can you please help me with my lack of knowledge!

Model

  <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Quotes_model extends CI_Model{

  function __construct(){
     parent::__construct();
  }

  function get_records(){
    $data = array();
    $query = $this->db->select('*')
             ->from('quotes');

return $query->result();
}
}

Controller

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

class Welcome extends CI_Controller {


    public function index()
    {
        $this->load->helper('url');
        $this->load->model('quotes_model');
        $data['quote']= $this->quotes_model->get_records();
        $this->load->view('welcome_message', array('data' => $data));

    }
}

and I have done this in my view :

View

    <?php die(var_dump($data)); ?>

Error

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: models/quotes_model.php

Line Number: 16

array(1) { ["quote"]=> NULL }

Solution

  • try

    $data['quote']= $this->quotes_model->get_records();
     $this->load->view('welcome_message', $data);
    

    and return your result from model:-

    return $query->result();
    

    or

    return $query;