Noah Goodrich
Noah Goodrich

Reputation: 227

Passing query result to 2nd function in Codeigniter

I'm trying to run two queries in two functions in a model on Codeigniter. The 2nd query runs based on the result of the first by passing the variable between the functions.

The first query gets the row from the 'photos' database based on the URL the user goes to. The second query is run against the 'users' database and the row is selected based on the 'userid' field from the row of the first query.

I need to be able to get all data from the rows of both queries. I get a 404 error at the moment, any ideas as how to get it working?

Code is as follows:

Model:

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

class Viewphoto_model extends CI_Model {


    public function get_photo($id)
    {
        $db_photos = $this->load->database('photos', TRUE);
        $db_photos->select('*');
        $db_photos->select("DATE_FORMAT(uploaddate, '%d/%m/%y') as uploaddate_formatted", FALSE);
        $db_photos->from('photos');
        $db_photos->where('approved', '1');
        $db_photos->where('id', $id);

        $result = $db_photos->get()->row();
        $userid = $this->get_user($result->userid);
    }

    public function get_user($userid)
    {
        $db_users = $this->load->database('users', TRUE);
        $db_users->select('firstname, lastname, email');
        $db_users->from('useraccounts');
        $db_users->where('id', $userid);

        $query = $db_users->get();
        return $query->row();
    }


}

Controller:

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

class Viewphoto extends CI_Controller {


    public function __construct()
    {
        parent::__construct();
        $this->load->model('viewphoto_model');
    }


    public function view($id)
    {
        $data['photo'] = $this->viewphoto_model->get_photo($id);

        if (empty($data['photo']))
        {
        show_404();
        }

        $data['user'] = $this->viewphoto_model->get_user($userid);

        if (empty($data['user']))
        {
        show_404();
        }

        $data['title'] = $data['photo']->title.' by '.$data['photo']->username;
        $data['meta_description'] = $data['photo']->description;
        $data['directory'] = 'sub';


        $this->load->view('templates/header', $data);
        $this->load->view('viewphoto/viewphoto', $data);
        $this->load->view('templates/footer', $data);
    }
}

Upvotes: 0

Views: 1805

Answers (1)

landons
landons

Reputation: 9547

You seem to have a slight logic issue. Your get_photo() function should return the photo object, which your controller can then use in the call to get_user()...

public function get_photo($id)
{
    $db_photos = $this->load->database('photos', TRUE);
    $db_photos->select('*');
    $db_photos->select("DATE_FORMAT(uploaddate, '%d/%m/%y') as uploaddate_formatted", FALSE);
    $db_photos->from('photos');
    $db_photos->where('approved', '1');
    $db_photos->where('id', $id);

    return $db_photos->get()->row();
}

Then in your controller....

public function view($id)
{
    $data['photo'] = $this->viewphoto_model->get_photo($id);

    if (empty($data['photo']))
    {
    show_404();
    }

    // Pass the photo's userid here
    $data['user'] = $this->viewphoto_model->get_user($data['photo']->userid);

    if (empty($data['user']))
    {
    show_404();
    }

    // ...
}

Upvotes: 1

Related Questions