Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- I have 3 tables. Users, Topics and Quotes. Here is my tables and columns:
- users
- ------
- id
- user_id
- email
- username
- topics
- ------
- id
- topic_id
- user_id
- topic_name
- quotes
- ------
- id
- quote_id
- topic_id
- quote_name
- In my view I want to be able to output something like for a specific user, in this case 'Thomas':
- User: Thomas
- Topic: Nature
- Quote 1: This is fun
- Quote 2: Here is another quote
- Quote 3: And a third quote
- Topic: Sports
- Quote 1: This is a sport quote
- Quote 2: This is another one
- Quote 3: And a third
- etc, etc.
- Basicly I want to end up with just on array, with all the stuff nested inside. If that makes sense.
- */
- // model with query:
- public function get_topics()
- {
- $this->db->select('u.user_id, u.username, t.topic_id, t.topic_name, q.quote_id, q.quote_name');
- $this->db->from('users u');
- $this->db->join('topics t', 't.user_id = u.user_id'); // this joins the user table to topics
- $this->db->join('quotes q', 'q.quote_id = t.quote_id'); // this joins the quote table to the topics table
- $query = $this->db->get();
- if($query->num_rows() > 0)
- {
- foreach ($query->result() as $row)
- {
- $data[] = $row;
- }
- }
- return $data;
- }
- // in my view print_r returns this error:
- Unknown column 't.quote_id' in 'on clause'
- SELECT `u`.`user_id`, `u`.`username`, `t`.`topic_id`, `t`.`topic_name`, `q`.`quote_id`, `q`.`quote_name` FROM (`users` u) JOIN `topics` t ON `t`.`user_id` = `u`.`user_id` JOIN `quotes` q ON `q`.`quote_id` = `t`.`quote_id`
Add Comment
Please, Sign In to add comment