2016-10-12 22 views
0

我将从一开始就开始,我使用代码点火器创建小型CMS,并且遇到了几个问题。我大部分时间都在使用PHP框架,所以请不要生我的气。CodeIgniter。允许的内存大小错误和分页

现在我已经从数据库中车辆型号列表和一个较大。所以我就从错误笨:

一个PHP错误遇到

严重性:错误消息

:允许内存大小的

在这个时刻我已经在评论来自代码点火器输出配置文件的代码行,它显示我这个错误。我有一个明确的价值清单,我想拥有。但它太多了。所以我想到了PAGINATION。

你的意见是什么,它可以帮助我吗?也许你有更好的建议/建议?

所以我试图把这个错误下来,并创建一个分页,但我失败了。

这是我的控制器,如果 “清单”:

 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 ---------------------------------------*/ 
} 

这是我的看法文件:

<?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; ?> 

This how the list looks like

**更多问题** 所以也许有人可以帮助我创建PAGINATION?

我试图把这个代码为“列表”控制器功能,但它didnt 工作:

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); 

威尔分页会帮我处理很多列表项? 也许有人可以帮我用代码?谢谢。

回答

0

您的codeigniter代码首先将整个列表存储在数组中。之后,您正在尝试构建屏幕。也许你可以从一个SQL连接开始,并在该连接中使用限制来限制行数。此外,发送您的脚本页码进行分页。

<?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(); 

以这种方式,您不会拉动整个数据,最终导致内存耗尽错误。

相关问题