Codeigniter分页列出数据库表中的所有记录,而不是列表限制

5t7ly7z5  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(102)

我已经使用下面的代码codeigniter分页。但它列出了所有reocrd从数据库表。分页不工作

$SQL = "SELECT * FROM list AS a ".$search_condition."  "; 
$query = $this->db->query($SQL); 
$finaldata=array();
$finaldata['num_results']= $query->num_rows();      
//how many blogs will be shown in a page
$limit = $finaldata['num_results'];
$start_limit = ($offset-1) * $limit;

echo $SQL = "SELECT * FROM list AS a   ".$search_condition." LIMIT ".$start_limit.",".$limit." "; 
$query = $this->db->query($SQL); 

if($query->num_rows() > 0)
{   
$finaldata['data']= $query->result_array();              
} 

// load pagination library
$this->load->library('pagination');
$config = array();
$config['base_url'] = site_url("adminuser/list");
$config['total_rows'] = $finaldata['num_results'];
$config['per_page'] = $limit;
//which uri segment indicates pagination number
$config['uri_segment'] = 3;
$config['use_page_numbers'] = TRUE;
//max links on a page will be shown
$config['num_links'] = 5;
//various pagination configuration
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
<! some code here ->
$config['cur_tag_close'] = '</span>';
$this->pagination->initialize($config);
$finaldata['pagination'] = $this->pagination->create_links();
$this->load->view('admin/list',$finaldata);

我试着在列表页中每页列出10条记录。但它不起作用

qlzsbp2j

qlzsbp2j1#

首先,你声明了无用的变量。你的第一个SQL应该包含COUNT(*)
你真的不需要$limit,你只使用它一次,声明下面的一行也是你的缩进,使它几乎不可读,尽量不要使用点连接字符串,使用大括号。

"SELECT * FROM list AS a {$search_condition} LIMIT {$start_limit}, {$limit}";

现在你可以正确地阅读它,我认为它应该解决你的问题。尝试优化你的代码,不要有混乱。最后,在你的配置中,你有per_page限制与total_rows相同。你定义了$limit = $finaldata['num_results'],改变这个值:

$config['per_page'] = $limit;

相关问题