我将从一开始就开始,我正在使用代码点火器创建小型CMS,但我遇到的问题很少。我是初级使用php框架,所以请不要生我的气。
现在,我有数据库中的车辆型号列表,而且列表很大。所以我从CodeIgniter得到了错误:
目前,我已经注释了代码点火器输出配置文件中的代码行,向我显示了此错误。我有一个明确的值(value)观 list ,我想拥有。但是它太多了。所以我想到了分页。
您的意见是什么,对我有帮助吗?也许您有更好的建议/建议?
因此,我试图降低该错误并创建分页,但失败了。
如果是“列表”,则这是我的 Controller :
public function lists(){
//get models
$data['models'] = $this->Model_model->get_models('id', 'DESC');
//get brands
$data['brands'] = $this->Model_model->get_brands('id', 'DESC');
//get brands
$data['brand'] = $this->Model_model->get_brands('id', 'DESC');
//get categories
$data['categories'] = $this->Model_model->get_categories('id', 'DESC');
//get Users
//$data['users'] = $this->User_model->get_users('id', 'DESC', 5);
//View
$data['main_content'] = 'admin/models/lists';
$this->load->view('admin/layouts/main', $data);
}
,这是模型文件中的代码:
<?php
class Model_model extends CI_Model
{
/*
* Get vehicle models
*
* @param - $order_by (string)
* @param - $sort (string)
* @param - $limit (int)
* @param - $offset (int)
*
* */
public function get_models($order_by = null, $sort = 'DESC', $limit = null, $offset = 0)
{
$this->db->select('vm.*, vb.v_brand_name as v_brand_name, vc.category_name as category_name, u.first_name, u.last_name');
$this->db->from('vehicle_models as vm');
$this->db->join('vehicle_brands as vb', 'vb.id = vm.vehicle_brand_id');
$this->db->join('vehicle_category as vc', 'vc.id = vb.vehicle_category_id', 'vb.vehicle_category_id = vm.vehicle_category_id', 'left');
$this->db->join('users as u', 'u.id = vb.user_id', 'left');
if ($limit != null) {
$this->db->limit($limit, $offset);
}
if ($order_by != null) {
$this->db->order_by($order_by, $sort);
}
$query = $this->db->get();
return $query->result();
}
/*
*
*Get Menu Items
*
* */
/* public function get_models(){
$this->db->where('id', 1);
$this->db->order_by('id');
$query = $this->db->get('vehicle_models');
return $query->result;
}*/
/*
*GET SINGLE ARTICLE
* */
public function get_model($id)
{
$this->db->where('id', $id);
$query = $this->db->get('vehicle_models');
return $query->row();
}
/*
* Get vehicle Categories
*
* @param - $order_by (string)
* @param - $sort (string)
* @param - $limit (int)
* @param - $offset (int)
*
* */
public function get_categories($order_by = null, $sort = 'DESC', $limit = null, $offset = 0)
{
$this->db->select('*');
$this->db->from('vehicle_category');
if ($limit != null) {
$this->db->limit($limit, $offset);
}
if ($order_by != null) {
$this->db->order_by($order_by, $sort);
}
$query = $this->db->get();
return $query->result();
}
/*
* Get vehicle Brands
*
* @param - $order_by (string)
* @param - $sort (string)
* @param - $limit (int)
* @param - $offset (int)
*
* */
public function get_brands($order_by = null, $sort = 'DESC', $limit = null, $offset = 0)
{
$this->db->select('*');
$this->db->from('vehicle_brands');
if ($limit != null) {
$this->db->limit($limit, $offset);
}
if ($order_by != null) {
$this->db->order_by($order_by, $sort);
}
$query = $this->db->get();
return $query->result();
}
/*
*
*Insert Model
*
* */
public function insert($data){
$this->db->insert('vehicle_models', $data);
return true;
}
/*public function insert_models($data){
$models = implode(',',$_POST['v_model_name']);
$this->db->insert('vehicle_models', $data);
$this->db->into('vehicle_models', $data);
$this->db->values($model);
}*/
public function update($data, $id){
$this->db->where('id', $id);
$this->db->update('vehicle_models', $data);
return true;
}
public function did_delete_row($id){
$this -> db -> where('id', $id);
$this -> db -> delete('vehicle_models');
return true;
}
/* --------------------------- PAGINATION ---------------------------------*/
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("vehicle_models");
}
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("vehicle_models");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
/*-------------------------- AND OF PAGINATION ---------------------------------------*/
}
这是我的 View 文件:
<?php foreach($categories as $category) :?>
<?php
$this->load->helper("url"); /
$postid = $this->uri->segment(4);
if ($category->id == $postid):
?>
<!-- ************************* ****************************************-->
<div class="table-responsive col-md-12">
<h2><?php echo $category->category_name; ?></h2>
<table class="table table-striped">
<thead>
<tr>
<th>Model (+View)</th>
<th>Brand</th>
<th>Category</th>
<th></th>
<th>Moves</th>
</tr>
</thead>
<tbody>
<?php foreach ($models as $model) : ?>
<?php if($model->vehicle_category_id == $postid) :?> <!-
<tr>
<td>
<a href="#" id="<?php echo $model->id; ?>"><?php echo $model->v_model_name; ?></a>
</td>
<?php foreach($brands as $brand) :?><?php if($model->vehicle_brand_id == $brand->id) :?>
<td>
<a href="<?php echo base_url(); ?>admin/brands/edit/<?php echo $brand->id; ?>" id="<?php echo $brand->id; ?>"><?php echo $brand->v_brand_name; ?></a>
</td>
<?php endif; ?>
<?php endforeach; ?>
<td>
<a href="#" id="<?php echo $model->vehicle_category_id; ?>"><?php echo $model->category_name; ?></a>
</td>
<td>
</td>
<td>
<a href="<?php echo base_url(); ?>admin/models/edit/<?php echo $model->id; ?>" class="btn btn-primary">Edit</a>
<a href="<?php echo base_url(); ?>admin/models/delete_row/<?php echo $model->id; ?>" class="btn btn-danger">Delete</a>
</td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
</div>
<!-- ************************* ****************************************-->
<?php endif;?>
<?php endforeach; ?>
**更多问题**
因此,也许有人可以帮助我创建分页?
public function lists1(){
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/test/admin/Models/lists/';
$config['per_page'] = 5;
$config['num_links'] = 5;
$config['total_rows'] = $this->db->get('vehicle_models')->num_rows();
/*$config['total_rows'] = $this->db->get_models('v_model_name');*/
$this->pagination->initialize($config);
$data['query'] = $this->db->get('vehicle_models', $config['per_page'],$this->uri->segment(4)); /*segment*/
$this->load->view('admin/models/lists', $data); /*nuoroda*/
echo $this->pagination->create_links();
$data['main_content'] = 'admin/models/lists';
$this->load->view('admin/layouts/main', $data);
分页会帮助我处理许多 list 项目吗?
也许有人可以帮助我编写代码?谢谢。
最佳答案
您的代码点火器代码首先将整个列表存储在数组中。之后,您尝试构建屏幕。也许您可以从SQL连接开始,并在该连接中使用limit来限制行数。另外,发送您的脚本页码以进行分页。
<?php
$current = $_GET['page'];
$rowCount = $_GET['length'];
$sql = "select * from model m
inner join brand b
on m.vehicle_brand_id = b.id
limit ".($current - 1)*($rowCount).",".$rowCount;
$query = $this->db->query($sql);
$content['tableList'] = $query->result();
这样,您将不会提取整个数据,这最终会导致内存耗尽错误。
关于php - CodeIgniter。允许的内存大小错误和分页,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39993454/