为了学习 php 和 codeigniter,我已经开始创建测试网站......类似于博客。但这不是重点。我正在使用 codeigniter 分页来显示我所有博客的帖子。但是......当我的数据库表(帖子)有超过 14k 条目时,它开始变慢......而且我不需要像这样的回答:“如果它是博客,你永远不会写这么多帖子,那就不要”不去想那个“......我需要真正解决如何加快所有这些东西的方法。
这是控制器:
function all()
{
// Pagination $config
$config = $this->m_posts->getPaginationConfig('all', $this->m_posts->getPostsCount());
$this->pagination->initialize($config);
$data['posts'] = $this->m_posts->getAllPosts($config['per_page'], $page);
$data['pagination'] = $this->pagination->create_links();
$this->template->build('posts/posts', $data);
}
模型:
function getPaginationConfig($base_url, $total_rows)
{
$config['base_url'] = base_url() . 'posts/'. $base_url;
$config['total_rows'] = $total_rows;
// ----
$config['per_page'] = 10;
$config['num_links'] = 5;
$config['use_page_numbers'] = TRUE;
$config['uri_segment'] = 3;
return $config;
}
function getPostsCount($limit)
{
$this->db->order_by('id', 'desc');
$q = $this->db->get('posts');
return $q->num_rows();
}
function getAllPosts($limit = 0, $start = 0)
{
$this->db->order_by('id', 'desc');
$this->db->where('active', 1);
// Get LATEST posts -> pagination
$q = $this->db->get('posts', $limit, $start);
$array = $q->result_array();
$data = $this->createPostsArray($array);
return $data;
}
function createPostsArray($array)
{
foreach ($array as $key => $row)
{
$array[$key]['usr_info'] = $this->user->getUserData($row['usr_id'], 'nickname');
$this->load->helper('cat_helper');
$array[$key]['cat_ids'] = explodeCategories($row['cat_ids']);
foreach ($array[$key]['cat_ids'] as $numb => $value)
{
$array[$key]['categories'][$numb] = $this->getCategoryName($value);
}
}
return $array;
}