How to interect database results in codeigniter?

87 views Asked by At

i have an problem. I have two tables:

1 called 'Offers' 1 called 'Networks'

In the 'Offers' table i have an field called 'NetworkId' and in my table 'Networks' i have an list of all networks with fild 'Id' and 'Name'.

I have an method in my Model to get all the rows in table 'Offers'. What i want know is how can i get the value of field 'Name' located in table 'Networks' using the 'NetworkID' that i grab with my method in my model.

I need create an new method ? create function ? idk what to do.

this is my controller atm:

public function index()
    {
        // Get List of the Offers
        $this->load->model('offers_model');

        $data['results_offers'] = $this->offers_model->list('all');

        $this->load->view('offers_home', $data);

    }

and this is my model code:

function list($id){

    if($id != "all")
    {
        $query = $this->db->get_where('offers', array('offerid' => $id));
    }
    else
    {
        $query = $this->db->get('offers');
    }


    return $query->result();
}

Thanks for help me!

1

There are 1 answers

0
Satish Ravipati On BEST ANSWER

Try this

function getAllOffers(){
    $query = $this->db->select('a.fieldname, b.name')->from('offers as a')->join('networks as b','a.networkid = b.id')->get();
    return $query->result_array();
}

Further Information check Active Record