笨。允许的内存大小错误和分页

时间:2016-10-12 08:00:31

标签: php codeigniter error-handling pagination frameworks

我将从一开始就开始,我正在使用代码点火器创建小型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?

  

我尝试将此代码放入&#34; list&#34;控制器功能,但它没有   工作:

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

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

1 个答案:

答案 0 :(得分:0)

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

int i, x;

i = 2;
x = ++i;
// now i = 3, x = 3

i = 2;
x = i++; 
// now i = 3, x = 2

这样,您就不会拉出整个数据,这最终会导致内存耗尽错误。