Robert Lovelett
Robert Lovelett

Reputation: 65

Call to undefined method CI_DB_mysqli_result::where() in CodeIgniter

I am trying to set up a route with CodeIgniter pulling photos by keys 'series' and 'id'. It would follow this logic:

photos/ : returns all of table 'photos' in database.
photos/series : return all photos matching a certain series. 'photos/nature' would return all photos with keys 'series' equaling 'nature'.
photos/series/id : returns single photo. 'photos/nature/1' would return a photo with key 'series' equaling 'nature' and 'id' equaling '1'

calling 'http://localhost/public_html/photos/nature/1' I get the following error:

A PHP Error was encountered
Severity: Error
Message: Call to undefined method CI_DB_mysqli_result::where()
Filename: models/Photos_model.php
Line Number: 14
Backtrace:

My 'photos' table structure:

CREATE TABLE `photos` (
  `filename` varchar(45) NOT NULL,
  `series` varchar(45) NOT NULL,
  `id` int(11) NOT NULL,
  `caption` varchar(45) NOT NULL,
  `description` varchar(45) NOT NULL,
  PRIMARY KEY (`filename`))

'photos' table's values:

INSERT INTO `photos` VALUES 
  ('file00053809264.jpg','nature',1,'waterfall','description of waterfall'),
  ('file000267747089.jpg','nature',2,'forest','description of burning forest'),
  ('file000325161223.jpg','cloudburst',1,'sky','description of sky'),
  ('file000267804564.jpg','cloudburst',2,'bursting abstract','description bursting abstract'),
  ('file000132701536.jpg','landmarks',1,'taj mahal','description taj mahal');

My photos controller in CodeIgniter:

<?php
    class Photos extends CI_Controller {
        public function __construct() {
            parent::__construct();
            $this->load->model('photos_model');
            $this->load->helper('url_helper');
            $this->load->helper('html'); // needed for header
            $this->load->helper('url'); // needed for footer
        }
        public function view($series = NULL, $id = NULL) {
            $data['photos'] = $this->photos_model->get_photos($series, $id)->result_array();
            if (empty($data['photos'])){
                show_404();
            }
            $data['title'] = 'Photos';
            $this->load->view('templates/header', $data);
            $this->load->view('photos/view', $data);
            $this->load->view('templates/footer');
        }
        public function index() {
            $data['photos'] = $this->photos_model->get_photos();
            $data['title'] = 'Photos';
            $this->load->view('templates/header', $data);
            $this->load->view('photos/index', $data);
            $this->load->view('templates/footer');
        }
    }

My photos model:

<?php
    class Photos_model extends CI_Model {
        public function __construct() {
            $this->load->database();
        }        
        public function get_photos($series = FALSE, $id = FALSE) {
            if ($series === FALSE && $id === FALSE) {
                $query = $this->db->get('photos');
            } else if ($id === FALSE) {
                $query = $this->db->get_where('photos', array('series' => $series));        
            } else {
                $query = $this->db->get('photos')->where('series', $series)->where('id', $id);    
            }
            return $query->result_array();
        }
    }

My 'photos/view.php' view:

<?php
echo $photos['photos'];

My routes config:

$route['photos/'] = 'photos/view';
$route['photos/(:any)'] = 'photos/view/$1';
$route['photos/(:any)/(:num)'] = 'photos/view/$1/$2';

If you could show me where and how I am slipping up, could you please explain it to me? Thank you.

Upvotes: 3

Views: 3170

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

According to error calling where() after get() might cause this issue as alternative move all where() calls before get() or use get_where()

$query = $this->db
              ->where('series', $series)
              ->where('id', $id)
              ->get('photos');

OR

$query = $this->db->get_where('photos', array('series' => $series,'id'=>$id));

Upvotes: 2

Related Questions