Guest User

Untitled

a guest
Jun 25th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.52 KB | None | 0 0
  1. /*
  2.  
  3. I have 3 tables. Users, Topics and Quotes. Here is my tables and columns:
  4.  
  5. users
  6. ------
  7. id
  8. user_id
  9. email
  10. username
  11.  
  12. topics
  13. ------
  14. id
  15. topic_id
  16. user_id
  17. topic_name
  18.  
  19. quotes
  20. ------
  21. id
  22. quote_id
  23. topic_id
  24. quote_name
  25.  
  26. In my view I want to be able to output something like for a specific user, in this case 'Thomas':
  27.  
  28. User: Thomas
  29. Topic: Nature
  30. Quote 1: This is fun
  31. Quote 2: Here is another quote
  32. Quote 3: And a third quote
  33.  
  34. Topic: Sports
  35. Quote 1: This is a sport quote
  36. Quote 2: This is another one
  37. Quote 3: And a third
  38.  
  39. etc, etc.
  40.  
  41. Basicly I want to end up with just on array, with all the stuff nested inside. If that makes sense.
  42.  
  43. */
  44.  
  45. // model with query:
  46.  
  47.     public function get_topics()
  48.     {
  49.        
  50.         $this->db->select('u.user_id, u.username, t.topic_id, t.topic_name, q.quote_id, q.quote_name');
  51.         $this->db->from('users u');
  52.         $this->db->join('topics t', 't.user_id = u.user_id'); // this joins the user table to topics
  53.         $this->db->join('quotes q', 'q.quote_id = t.quote_id'); // this joins the quote table to the topics table
  54.         $query = $this->db->get();
  55.  
  56.         if($query->num_rows() > 0)
  57.         {
  58.             foreach ($query->result() as $row)
  59.             {
  60.                 $data[] = $row;
  61.             }
  62.         }
  63.         return $data;
  64.     }
  65.  
  66. // in my view print_r returns this error:
  67.  
  68. Unknown column 't.quote_id' in 'on clause'
  69.  
  70. 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