我有3个表,如下所示:
[USERS] user_id username
[TOPICS] topic_id user_id topic_name
[QUOTES] quote_id topic_id quote_name
我希望能够在我看来做这样的事情:
Username: Thomas
Topic 1: Whatever
Quotes: One quote,another quote,and a third quote,all belonging to Topic 1.
Topic 2: Another topic from Thomas
Quotes: Yes indeed,Okay thanks,I love Stack Overflow,These quotes belong to Topic 2.
但我不能让它工作,我一直在尝试一切,包括奇怪的东西,如:
public function get_quotes() { $this->db->select('*'); $this->db->from('topics'); $this->db->join('quotes','topic_id = quote_id'); $query = $this->db->get(); if($query->num_rows() > 0) { foreach ($query->result() as $row) { $data[] = $row; } } return $data; }这有点奇怪吗,我应该尝试使用’where’代替吗?就像是:
$this->db->where('user',$user_id); $this->db->where('topic',$topic_id); $this->db->where('quote',$quote_id);我真的很感激我能得到的任何帮助,或只是一个手指指向正确的方向!
要使用分析器,请将其粘贴到控制器中:
$this->output->enable_profiler(TRUE);
它将导致所有数据库调用,所有POST变量等的良好输出;
参考这里:http://codeigniter.com/user_guide/libraries/output.html
UPDATE
user_id,username,topic_id,topic_name,quote_id,quote_name
这是您想要的活动查询(如果足够清楚,您还可以使用方法链接):
$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.topic_id = t.topic_id'); // this joins the quote table to the topics table $query = $this->db->get();
您的结果集将类似于:
user_id | username | topic_id | topic_name | quote_id | quote_name 1 |Thomas |1 |Whatever |1 |One quote,anot... 2 |Ryan |4 |Another... |6 |To be or not to...
一旦你有了这个结果集,只需循环遍历数据输出它,并检查你是否有来自同一个人的多个引号(比如按user_id排序,如果它是同一个人,则在第二个循环上进行测试,否则输出新用户名).
原文地址:https://www.jb51.cc/php/130472.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。