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;
Question
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;
OTHER TIPS
try this
function get_records(){
$data = $this->db->select('*')
->from('quotes')
->join('authors', 'authors.id = quotes.author_id')
->join('genre', 'genre.id = quotes.id')
->order_by('id', 'RANDOM')
->limit(1)->row_array();
///$data = array();
return $data;
}
try ->row_array() if you want to fetch single row. If you want to fetch all records try result_array(). this will give you output in an array...
you need to first pull your results through result_array()
Corrected Code:
function get_records(){
$data = array();
$query = $this->db->select('*')
->from('quotes')
->join('authors', 'authors.id = quotes.author_id')
->join('genre', 'genre.id = quotes.id')
->order_by('id', 'RANDOM')
->limit(1);
///$data = array();
if ($query->num_rows()) {
foreach ($query->result_array() as $row) {
$data[] = $row;
}
}
return $data;
}
you made a mistake in function
function get_records(){
$query = $this->db->select('*')
->from('quotes')
->join('authors', 'authors.id = quotes.author_id')
->join('genre', 'genre.id = quotes.id')
->order_by('id', 'RANDOM')
->limit(1);
///$data = array();
return $data;
}
you return $data; this should be return
$query->result();
your model has an error, you are returning $data
, which is not defined!
add simply this:
return $query->result();
also change your controller:
$this->load->view('welcome_message', $data);
and then you get your data in your view like this:
foreach ($quote as $row){
$id=$row->author_id;
echo $id;
}