Php 表联接:无法显示所需的数据
如果我有这样的桌子:Php 表联接:无法显示所需的数据,php,codeigniter,join,Php,Codeigniter,Join,如果我有这样的桌子: ID | Title | Topic | Summary 1 | A | Technology | ... 2 | B | Health | ... 3 | C | Sport | ... 这是我的CI_模型: function show($limit, $offset) { $this->db->select('document.id, documen
ID | Title | Topic | Summary
1 | A | Technology | ...
2 | B | Health | ...
3 | C | Sport | ...
这是我的CI_模型:
function show($limit, $offset)
{
$this->db->select('document.id, document.title, document.summary, document.id_topic AS topic');
$this->db->from('document');
$this->db->join('topic', 'topic.id_topic = document.id_topic');
$this->db->limit($limit, $offset);
$this->db->order_by('id', 'asc');
return $this->db->get()->result();
}
这是我的控制器:
$docdata = $this->Trainingmodel->show($this->limit, $offset);
...
$this->table->set_heading('ID', 'Title', 'Topic', 'Summary');
foreach ($docdata as $doc)
{
$this->table->add_row($doc->id, $doc->title, $doc->topic, $doc->summary);
}
显然,主题显示的是id,而不是名称。
例如:
ID | Title | Topic | Summary
1 | A | 1 | ...
2 | B | 2 | ...
3 | C | 3 | ...
我该怎么办?我想显示主题的名称,而不是主题的id。可能是因为这个
文档。id\u topic
$this->db->select('document.id, document.title, document.summary, topic.topic');
$this->db->from('document');
$this->db->join('topic', 'topic.id = document.id_topic');
$this->db->select('document.id, document.title, document.summary, document.id_topic AS topic');
如果是类似于
document.topic
或document.name\u topic
?查看您在其他答案中作为注释发布的表结构,我认为您需要在select()
中使用topic.topic
,在join()
中使用topic.id=document.id\u topic
$this->db->select('document.id, document.title, document.summary, topic.topic');
$this->db->from('document');
$this->db->join('topic', 'topic.id = document.id_topic');
您的
主题
的表结构是什么?它不起作用。我有两个这样的表:第一个表(主题表)结构:id(INT(1)),主题(varchar(25)),第二个表(文档表)结构:id(INT 3),title(varchar(100)),id_主题(INT(1)),summary(TEXT)。当我试图使用您的解决方案时,它给我一个警告:致命错误:调用成员函数result()在非-object@user2338965你没有先告诉我结构。查看我的编辑。它不起作用。我有两个这样的表:第一个表(主题表)结构:id(INT(1)),主题(varchar(25)),第二个表(文档表)结构:id(INT 3),title(varchar(100)),id_主题(INT(1)),summary(TEXT)。当我试图使用您的解决方案时,它给我一个警告:致命错误:对非对象调用成员函数result()