简体   繁体   English

Codeigniter->从(多个表)

[英]Codeigniter ->from(multiple tables)

Hello stackoverflow people :) I want to get data from, let's say, 2 tables. 您好stackoverflow人们:)我想从2个表中获取数据。 I dont want any specific 'where' option, so that's my model code: 我不想要任何特定的“ where”选项,所以这是我的模型代码:

$this->db->from('table1, table2');      
$this->db->order_by('table1.date, table2.date', "desc");
$query = $this->db->get('', $num, $offset);

return $query->result_array();

This way i get only information from 'table2', which contains only 1 article, and this article echoing several times on page. 这样,我仅从“ table2”中获取信息,“ table2”仅包含1条文章,并且该文章在页面上多次出现。 These two tables have the same structure and i hoped that writing 'order_by' this way would help, but no. 这两个表具有相同的结构,我希望以此方式编写“ order_by”会有所帮助,但没有帮助。 If i do any other manipulation i get information only from one table. 如果我进行任何其他操作,我只会从一张表中获取信息。 Order by date is important to keep. 按日期排序对于保持很重要。 Hopefully anyone would find this question interesting and would help aswell. 希望任何人都会发现这个问题很有趣并且也会有所帮助。 Thanks in advance! 提前致谢!

Edited part: Function: 编辑部分:功能:

$this->db->get("table1");

$comm_numbers = $this->db->count_all_results("table1");

$config['base_url'] = base_url();
$config['total_rows'] = $comm_numbers;
$config['per_page'] = '5';
$config['uri_segment'] = 1;
$this->pagination->initialize($config);

$this->load->model("model_get_all");
$data["results"] = $this->model_get_all->getData1($config['per_page'], $this->uri->segment(1));
$this->load->helper('url');

Try this: 尝试这个:

$this->db->query('SELECT id, title, text, views, date, image FROM table1 UNION SELECT id, title, text, views, date, image FROM tablee2');

Update: Query with CodeIgniter Pagination: 更新:使用CodeIgniter分页查询:

$this->db->query("SELECT id, title, text, views, date, image FROM table1 UNION SELECT id, title, text, views, date, image FROM tablee2 LIMIT $offset, $num");

Update 2: Ok, I change only the place when $offset goes null . 更新2:好的,我只更改$offset null时的位置。

$this->db->get("table1");

$comm_numbers = $this->db->count_all_results("table1");

$config['base_url'] = base_url();
$config['total_rows'] = $comm_numbers;
$config['per_page'] = '5';
$config['uri_segment'] = 1;
$this->pagination->initialize($config);

$page = ($this->uri->segment(1)) ? $this->uri->segment(1) : 0;

$this->load->model("model_get_all");
$data["results"] = $this->model_get_all->getData1($config['per_page'], $page);
$this->load->helper('url');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM