I will start from very beginning, I am creating small CMS with code igniter and I faced with few problems. I am junior in working with php frameworks, so please do not be angry at me.
Right now I have vehicles models lists from database and that one is large. So I got Error from CodeIgniter:
A PHP Error was encountered
Severity: Error
Message: Allowed memory size of
At this moment I'm already commented the line of code from code igniter output configuration file, which shows me this error. And I have a clear list of values, that I want to have. But its too many. So I thought about PAGINATION.
What its your opinion, can it help me? maybe you have some better advice/suggestion?
So I tried to turn that error down and create a pagination, but I failed.
This is my controller if "lists":
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);
}
This is code from model file:
<?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 ---------------------------------------*/
}
And this is my view file:
<?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; ?>
** MORE QUESTIONS** So maybe somebody can help me to create PAGINATION?
I tried to put this code to "list" controller function but its didnt work:
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);
Will pagination will help me to deal with to many list items? Maybe somebody can help me with code? Thanks.
Your codeigniter code is storing the entire list in array first. After that you are trying to build the screen. Perhaps you can start with a SQL join and use limit in that join to restrict the number of rows. Also, send your script page number for pagination.
<?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();
In this way, you will not pull entire data, which eventually causes memory exhaustion error.