我目前在Codeigniter 2.2的分页方面遇到了问题。 我想要的是显示项目列表,如果它包含在另一个表格中。
为了表达我的意思,这将是逻辑。
<?php
$x = 1;
$this->db->group_by("item_id");
$query = $this->db->get('item_inventory_inhouse');
foreach ($query->result() as $row4)
{
$this->db->where('item_id', $row4->item_id);
$query = $this->db->get('items');
foreach ($query->result() as $row)
{
echo '<tr>';
echo '<td>' . $x++ . '</td>';
echo '<td><a href="">' . $row->item_id . '</a></td>';
echo '<td><a href="">' . $row->item_name . '</a></td>';
echo '<td>' . $row->item_category . '</td>';
echo '<td>' . $row->item_costprice . '</td>';
echo '<td>' . $row->item_retailprice . '</td>';
echo '<td>' . $row->item_tax . '%</td>';
echo '</tr>';
}
}
?>
因为我正在做分页。我不能写上面的代码。
好吧,我试着制定一些代码。 这是我的控制器:
function pharmacy()
{
if($this->session->userdata('logged_in'))
{
$total_row = $this->pharmpagination_model->record_count();
$config['num_links'] = 2;
$config['base_url'] = base_url().'index.php/inventory/pharmacy/';
$config["total_rows"] = $total_row;
$config['per_page'] = 30;
$config['full_tag_open'] = '<nav><ul class="pagination">';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['full_tag_close'] = '</ul></nav>';
$config['next_tag_open'] = '<li><span aria-hidden="true">';
$config['next_link'] = '»';
$config['next_tag_close'] = '</span></li>';
$config['last_link'] = 'Last';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['first_link'] = 'First';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_tag_open'] = '<li><span aria-hidden="true">';
$config['prev_link'] = '«';
$config['prev_tag_close'] = '</span></li>';
$this->pagination->initialize($config);
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}
else{
$page = 1;
}
$data["results"] = $this->pharmpagination_model->fetch_data($config["per_page"], $page);
$str_links = $this->pagination->create_links();
$data["links"] = explode(' ',$str_links );
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$data['title'] = 'Pharmacy Inventory';
$this->load->view('inventory/pharmacy', $data);
}
else
{
$this->session->set_flashdata('message', 'Oops! You have to Login');
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
这是模型
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class PharmPagination_Model extends CI_Model {
function __construct() {
parent::__construct();
}
// Count all record of table "contact_info" in database.
public function record_count() {
return $this->db->count_all("item_inventory_inpharmacy");
}
// Fetch data according to per_page limit.
public function fetch_data($limit, $id) {
$this->db->group_by("item_id");
$mainquery = $this->db->get('item_inventory_inpharmacy');
foreach ($mainquery->result() as $row4)
{
$this->db->limit($limit, $id-1);
$this->db->where('item_id', $row4->item_id);
$query = $this->db->get("items");
}
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
?>
它实际上有效,但它只显示1行,实际上它应该显示5行,因为数据库中有5行。
希望有人可以提供帮助。感谢
答案 0 :(得分:0)
由于您尚未提供数据快照,因此很难说明为什么它会显示一行。我假设你在items表中有不同的item_id,如果你查看你的模型代码,那就是问题所在(你正在查询每个项目id的项目表,但只返回最后一项的$ data数组。)
您可以使用join轻松完成此操作:)
public function fetch_data($limit, $id) {
$this->db->group_by("item_inventory_inpharmacy.item_id");
$this->db->from("item_inventory_inpharmacy");
$this->db->join("items","items.item_id=item_inventory_inpharmacy.item_id");
$this->db->limit($limit, $id-1);
$query = $this->db->get();
return $query->result();
}