Section 1
I have an issue in " Displaying 1 - 5 of 10 records ". I have a piece of code which works only on first page, when i click on second page then it show the same result " Displaying 1 - 5 of 10 records " Instead of " Displaying 10 of 10 records ".
Code In Controller
$total=$config["total_rows"];
$per_page=$config['per_page'];
$curpage=floor(($this->uri->segment(1)/$config['per_page']) + 1);
$result_start = ($curpage - 1) * $per_page + 1;
if ($result_start == 0) $result_start= 1; // *it happens only for the first run*
$result_end = $result_start+$per_page-1;
if ($result_end < $per_page) // happens when records less than per page
{ $result_end = $per_page; }
else if ($result_end > $total) // happens when result end is greater than total records
{ $result_end = $total;}
$data['show']="displaying $result_start to $result_end of $total";
I don't know whats wrong with it, I have tried other code which I find from different websites, but they are not working properly.
Section 2
I have a filter section, where user can filter product by Size, Color and Price, How to achieve this section?
My main/ Index Controller
public function index($page=1)
{
$config = array();
$keyword = $this->input->post('search');
if ($keyword === null){ $keyword = $this->session->userdata('search');}
else{ $this->session->set_userdata('search',$keyword);}
$config["base_url"] = base_url();
$config["total_rows"] = $this->crt->total_items($keyword);
$config['use_page_numbers'] =true;
$config['cur_tag_open'] = '<a class="page-numbers current">';
$config['cur_tag_close'] = '</a>';
$config["per_page"] =5;
$config["uri_segment"] = 1;
$this->pagination->initialize($config);
$page = ($page - 1) * $config['per_page'];
// showing x to y of z records
$total=$config["total_rows"];
$per_page=$config['per_page'];
$curpage=floor(($this->uri->segment(1)/$config['per_page']) + 1);
$result_start = ($curpage - 1) * $per_page + 1;
if ($result_start == 0) $result_start= 1; // *it happens only for the first run*
$result_end = $result_start+$per_page-1;
if ($result_end < $per_page) // happens when records less than per page
{ $result_end = $per_page; }
else if ($result_end > $total) // happens when result end is greater than total records
{ $result_end = $total;}
$data['show']="displaying $result_start to $result_end of $total";
$data['sidebar']=$this->crt->sidebar_cat();
$data['products']=$this->crt->get_product($config["per_page"], $page,$keyword);
$data["links"] = $this->pagination->create_links();
$this->load->view('header');
$this->load->view('index',$data);
$this->load->view('footer');
}
My Model
// Paginitions for Items
function total_items($keyword)
{
//return $this->db->count_all("product");
$this->db->like('product_name',$keyword);
$this->db->from('product');
return $this->db->count_all_results();
}
//Fetching Products
public function get_product($limit,$start,$keyword){
// $this->db->where('Is_Hidden',0);
// $this->db->select('*');
// $this->db->from('product');
$this->db->order_by('product_id', 'DESC');
$this->db->limit($limit, $start);
$this->db->like('product_name',$keyword);
$query = $this->db->get_where('product');
if(!$query->num_rows()>0)
{
echo '<h1>No product available</h1>';
}
else
{
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
}
How I can get the Filter section?
UPDATE 1
Section 1 issue has been fixed by replacing those two lines
$curpage=floor(($this->uri->segment(1)/$config['per_page']) + 1);
if ($result_start == 0) $result_start= 1; //
TO
$curpage=$this->uri->segment(1);
if ($result_start == 0 || $result_start<0) $result_start= 1; //
Update 2
I somehow did the filter section but now I am stuck in the ajax issue. Issue is that When color or size checkbox is empty then it throw error of foreach loop. I only need to control the empty or null section, like if the checkbox is unchecked then it will not send / post the value to the controller...
My Ajax Code is
function clr(){
var selected = new Array();
var size = new Array();
var url="<?php echo base_url('Cart/filt_color');?>";
// alert(url);
$("input:checkbox[name=color]:checked").each(function() {
selected.push($(this).val());
//console.log(selected);
});
// Sizes
$("input:checkbox[name=size]:checked").each(function() {
size.push($(this).val());
//console.log(selected);
});
$.ajax({
url:url,
method:"post",
data:{'colors':selected,'sizes':size},
success:function(data)
{
//
//console.log(data);
$("#mdv").html(data);
}
});
}
I have tried many check like, undefined
, or ==''
or data.length <-1
etc. The data.length
will did some check but i am not able to check the variable separately like, there are two variable I am send in data: color,size
How can I check the variable separately like: if(data.color.length < 0 )
.
Answered is Here, All the details and code are posted in the mentioned link question.