I saw a filter example here on Stackoverflow and I'd try to implement it on my codeigniter but it doesn't filter it when I click the go button. It returns me always no user found what is the problem with this?
Controller
public function filter($page=0,$offset=5,$search=''){
$user = $this->session->userdata('user_id');
$position = $this->session->userdata('user_position');
if($position =='admin'){
$search = $this->input->post('search');
$row = $this->m_user->getAllFilterUsers($offset,$page,$search);
$data['usersTable'] = $row->result();
$data['pagination'] = $this->m_user->getUsersFilterPages($search);
$data['offset'] = $offset;
$data['page'] = $page;
$data['search'] = $search;
$data['title'] = 'Manage Users';
$this->load->view('vadminuserfilter',$data);
}
else{
$this->session->set_flashdata('error','Page Not Found.');
redirect('cuser/displayClientPage');
return;
}
}
Model
public function getAllFilterUsers($offset,$count,$search){
if($search!=''){
$this->db->where('user_position','client');
$this->db->where('user_status',$search);
}
$this->db->where('user_position','client');
$this->db->where('user_status','active');
$this->db->order_by('user_id', 'desc');
$UsersQuery = $this->db->get('tb_user',$offset,$count);
if($UsersQuery->num_rows>0){
return $UsersQuery;
}
else{
$this->session->set_flashdata('message','No User Found');
redirect('cuser/filter','refresh');
}
}
public function getUsersFilterPages($search){
$this->db->where('user_position','client');
$this->db->where('user_status',$search);
$Pagesquery = $this->db->get('tb_user');
$config['base_url'] = site_url('cuser/filter');
$config['total_rows']= $Pagesquery->num_rows();
$config['per_page'] = 5;
$config['first_link'] = 'First';
$config['prev_link'] = 'Previous';
$config['next_link'] = 'Next';
$config['last_link'] = 'Last';
$this->pagination->initialize($config);
return $this->pagination->create_links();
}
View
<?php echo form_open('cuser/filter'); ?>
Filter By:
<?php $dropdown = array('active'=>'active','inactive'=>'inactive');?>
<?php echo form_dropdown('search', $dropdown); ?>
<input name="Submit" type="submit" class="button" <?php echo form_submit('submit','Go');?>
<?php echo form_close(); ?>
Just check getAllFilterUsers function of your model. The value of $offset is 5 and value of $count is 0.
But you use..
this->db->where('user_position','client');
$this->db->where('user_status','active');
$this->db->order_by('user_id', 'desc');
$UsersQuery = $this->db->get('tb_user',$offset,$count);
that means..
Select * from tb_user where user_position = 'client' and 'user_status' = 'active' order by user_id desc limit 5,0
This query always return empty set
Your query should be..
Select * from tb_user where user_position = 'client' and 'user_status' = 'active' order by user_id desc limit 0,5
So, Your code snippet should be
this->db->where('user_position','client');
$this->db->where('user_status','active');
$this->db->order_by('user_id', 'desc');
$UsersQuery = $this->db->get('tb_user',$count,$offset);
Finally the function is...
public function getAllFilterUsers($offset,$count,$search){
if($search!=''){
$this->db->where('user_position','client');
$this->db->where('user_status',$search);
}
this->db->where('user_position','client');
$this->db->where('user_status','active');
$this->db->order_by('user_id', 'desc');
$UsersQuery = $this->db->get('tb_user',$count,$offset);
if($UsersQuery->num_rows>0){
return $UsersQuery;
}
else{
$this->session->set_flashdata('message','No User Found');
redirect('cuser/filter','refresh');
}
}
Thats it
Related
Friends I'm Unable to get open positions Sum. here is my code. I am getting 1(one) instead of total sum. Help me to solve this issue.
Controller:
function index(){
$userId = $this->phpsession->get("userId");
$userType = $this->phpsession->get('userType');
$date = date("Y-m-d");
if(!$userId && !$this->phpsession->get("userType")){
redirect(base_url().'user');
}
$config['base_url'] = base_url().'requirement/index';
$config['total_rows'] = $this->requirement_model->GetRequirement(array("count"=>true));
$config['per_page'] = 5;
$config['cur_tag_open'] = '<a>';
$config['cur_tag_close'] = '</a>';
$this->pagination->initialize($config);
$options['offset'] = $this->uri->segment(3);
$options['limit'] = $config['per_page'];
$options['join']=true;
$data['clients'] = $this->client_model->ajaxclient();
$data['requirements'] = array(""=>"Choose requirement");
$data['requirement'] = $this->requirement_model->GetRequirement($options);
$data['links'] = $this->pagination->create_links();
$data['totalactive']=$this->requirement_model->GetRequirement(array("find"=>true));
$data['totalrequirement']=$this->requirement_model->GetRequirement(array("totalreq"=>true));
$data['openpositions']=$this->requirement_model->GetRequirement(array("openpos"=>true));
//print_R($data['openpositions']);exit;
//echo "<pre>"; print_R($this->db->last_query()); exit;
$data['page_title'] = "Requirement Details";
$this->layout->view("requirement/index",$data);
}
This is my model function
Model:
function GetRequirement($options = array()){
if(isset($options['requirementId']))
$this->db->where('requirementId',$options['requirementId']);
if(isset($options['clientName']))
$this->db->where('clientName',$options['clientName']);
if(isset($options['limit']) && isset($options['offset']))
$this->db->limit($options['limit'], $options['offset']);
else if(isset($options['limit']))
$this->db->limit($options['limit']);
$this->db->order_by("activateddate", "DESC");
if(isset($options['join'])){
$this->db->select('r.*,c.clientName as cName');
$this->db->from('requirement as r');
$this->db->join('clients as c','c.clientId=r.clientName');
$query=$this->db->get();
if(#$options['requirementId']) return $query->row(0);
return $query->result();
}
if(isset($options['find'])){
$this->db->select('distinct(clientName)');
$this->db->from('requirement');
$this->db->where('(clientName) and (noofpositions > 0) ');
$this->db->count_all();
$query=$this->db->get();
return $query->num_rows();
}
if(isset($options['totalreq'])){
$this->db->select('requirementName');
$this->db->from('requirement');
$this->db->where('(noofpositions > 0) ');
$this->db->count_all();
$query=$this->db->get();
return $query->num_rows();
}
if(isset($options['openpos'])){
$this->db->select_sum('openPos');
$this->db->from('requirement');
$this->db->where('(closedPos = 0) ');
$this->db->count_all();
$query=$this->db->get();
return $query->num_rows();
}
$query = $this->db->get('requirement');
if(isset($options['count'])) return $query->num_rows();
if(#$options['requirementId']) return $query->row(0);
return $query->result();
}
This is my View page
View:
<div class="inner">
<h3><?php echo $openpositions; ?></h3>
<p>Total Positions Opened</p>
</div>
You are using sum which is an aggregate function and with out group by it will take whole table as one group in if(isset($options['openpos'])){ ... } part of code of your model your are returning num_rows() which returns the no. of rows so in your case there will be one row with the value of sum therefore you are getting result as 1 change your
if (isset($options['openpos'])) {
$this->db->select_sum('openPos');
$this->db->from('requirement');
$this->db->where('(closedPos = 0) ');
$query = $this->db->get();
return $query->row()->openpos;
}
I think the mysql statement has an error.
Change the following line:
$this->db->where('(closedPos = 0) ');
To
$this->db->where('closedPos', 0);
remove the following line: (this will count all rows and return the value, which you do not want)
$this->db->countall();
If this does not solve your problem you could try outputting the mysql statement by adding exit($this->db->last_query()); to try and find the problem, like this:
if(isset($options['openpos'])){
$this->db->select_sum('openPos');
$this->db->from('requirement');
$this->db->where('(closedPos = 0) ');
$this->db->count_all();
$query=$this->db->get();
// output last query
exit($this->db->last_query());
return $query->num_rows();
}
I have a problem with pagination and codeigniter. I have a quick_searh view from witch I am submitting the information to a index controller function and there setting the pagination and calling the quick_search method to get the data I want. It just doesnt work . I've spent more then 5 hours rewriting those methods and even starting with quick_search and then passing to index function but nothing worked, please help.
public function index(){
// search parameters config
$lawyer_name = $this->input->post('lawyer_name');
$kanzlei = $this->input->post('kanzlei');
$area_of_expertise = $this->input->post('area_of_expertise');
$post_code = $this->input->post('post_code');
$city = $this->input->post('city');
$result = $this->quick_search(
$this->uri->segment(3),
$lawyer_name,
$kanzlei,
$area_of_expertise,
$post_code,
$city);
if(isset($result)){
// pagination config
$this->load->library('pagination');
$this->load->library('table');
$config['total_rows'] = count($result);
$config['base_url'] = 'http://localhost/anwalt/index.php/search/index';
$config['per_page'] = 5;
$config['num_links'] = 5;
$this->pagination->initialize($config);
$data['search_result_array'] = $result;
$data['main_content'] = 'pages/quick_search_results';
$this->load->view('templates/home_body_content', $data);
}
}
the quick_search function:
public function quick_search($offset, $lawyer_name, $kanzlei, $area_of_expertise, $post_code, $city){
// no input in the quick search
if( empty($lawyer_name) && empty($kanzlei) && empty($area_of_expertise)
&& empty($post_code) && empty($city))
{
$result = 'nothing';
} else {
$this->load->model('quick_search_model');
$result = $this->quick_search_model->get_search_results(
$offset,
$lawyer_name,
$kanzlei,
$area_of_expertise,
$post_code,
$city
);
}
return $result;
}
the sql is like this:
$sql = "SELECT users.user_id, users.canonical_name, first_name, last_name, city, phone_number, kanzlei
from users
inner join user_normal_aos
on users.user_id = user_normal_aos.user_id
inner join normal_areas_of_expertise
on user_normal_aos.normal_areas_of_expertise_id = normal_areas_of_expertise.normal_areas_of_expertise_id
where ".implode(" AND ", $where);
if(empty($offset)){
$offset = 0;
}
$sql = $sql." LIMIT ".$offset.", 4";
The data are displayed but I dont see the pagination in there .. and even when I want to change the url for segmenting it says I dont have any data.
The view is like:
<h1>Quick search results</h1>
<?php
if($search_result_array == "nothing"){
echo "<h3>You havent inputed anything</h3>";
} else {
echo $this->table->generate($search_result_array);
}
echo $this->pagination->create_links();
As per your search variables you can use this:
$lawyer_name = $this->input->post('lawyer_name');
$kanzlei = $this->input->post('kanzlei');
$area_of_expertise = $this->input->post('area_of_expertise');
$post_code = $this->input->post('post_code');
$city = $this->input->post('city');
/*pagination start*/
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/index/lawyer/'.$lawyer_name.'/kanzlei/'.$kanzlei.'/area_of_expertise/'.$area_of_expertise.'/post_code/'.$city.'/page/';
$config['total_rows'] = $this->model->count_all_results(); ###implement this function to count all the vodeos as per the search variables, just use the same function as "quick_search" but without the limit clause
$config['per_page'] = count($result);;
$config['uri_segment'] = 10;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Prev';
$config['cur_tag_open'] = '<span class="active_page">';
$config['cur_tag_close'] = '</span>';
$this->pagination->initialize($config);
/*pagination end*/
You can not use $this->pagination->create_links(); method in view.
Use $data['pagination'] = $this->pagination->create_links(); in controller just before loading view
and echo $pagination in view
hope this will help you.
I have a controller with a method that looks something like this:
public function browsecategory($category_id)
{
//find any subcategories for this category
$this->load->model('category/category_model');
$this->load->model('category/product_category_model');
$records['categories'] = $this->category_model->find_all_by('parent_id', $category_id);
//add some product data too.
$records['products'] = $this->product_category_model->find_all_by('category_id', $category_id);
Template::set('records', $records);
Template::render();
}//end browsecategory
All the examples I've seen for the codeigniter pagination "stuff" is using one query.
I need to combine two data sets and serve on one view.
Any suggestions?
EDIT 1
I've tried to follow MDeSilva's suggestion below. And although the pagination object is correctly calculating the number of links to create, all items appear on all pages.
Here's the code in the model that gets the data:
public function get_categories_and_products($limit=12, $offset=0, $category_id=null)
{
print '<BR>the function got the following offeset:'.$offset;
$query = "(SELECT cat.category_id, cat.title, cat.image_thumb, cat.deleted, cat.display_weight ";
$query = $query."FROM bf_categories cat ";
$query = $query."WHERE cat.parent_id=".$category_id;
$query = $query." AND cat.category_id <>".$category_id;
$query = $query.") UNION (";
$query = $query."SELECT p.product_id, p.name, p.image_thumb, p.deleted , p.display_weight";
$query = $query." FROM bf_product p ";
$query = $query."Inner join bf_product_category cp ";
$query = $query."on p.product_id=cp.product_id ";
$query = $query."Where cp.category_id=".$category_id.")";
$this->db->limit($limit, $offset);
$catsandprods= $this->db->query($query);
return $catsandprods->result() ;
}
And here's the code in the controller:
public function browsecategory($category_id, $offset=0)
{
$this->load->library('pagination');
$total = $this->product_model->get_cats_prods_count($category_id);
$config['base_url'] = site_url('/product/browsecategory/'.$category_id);
$config['uri_segment'] = 4;
$config['total_rows'] = $total;
$config['per_page'] = 5;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$offset = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0;
print $offset;
//Call the model function here to get the result,
$records= $this->product_model->get_categories_and_products(5,$offset,$category_id);
//add to breadcrumb trail
$this->build_bread_crumb_trail($category_id);
$breadcrumbs = $this->breadcrumbs->expand_to_hyperlinks();
Template::set('currentcategory',$category_id);
Template::set('breadcrumbs', $breadcrumbs);
Template::set('records', $records);
Template::render();
}
I've debugged and I can see that the line of code "$this->db->limit($limit, $offset);" in the model is not working. It always returns the full record set...
Can you tell me what I'm missing?
Thanks.
This is the way to generate pagination links in CI, for your requirement have a query with a join,
public function index($offset = 0) {
$language_id = 1;
$artwork_id = null;
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$artwork_id = $this->input->post('serach_artwork_id', TRUE) ? $this->input->post('serach_artwork_id', TRUE) : null;
$data['artwork_id'] = $artwork_id;
}
$this->load->library('pagination');
$limit = 10;
$total = $this->Artwork_model->get_artwork_count($language_id, $artwork_id);
$config['base_url'] = base_url().'artwork/index/';
$config['total_rows'] = $total;
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['first_link'] = '<< First';
$config['last_link'] = 'Last >>';
$config['next_link'] = 'Next ' . '>';
$config['prev_link'] = '<' . ' Previous';
$config['num_tag_open'] = '<span class="number">';
$config['num_tag_close'] = '</span>';
$config['cur_tag_open'] = '<span class="current"><a href="#">';
$config['cur_tag_close'] = '</a></span>';
$this->pagination->initialize($config);
//Call the model function here to get the result,
$data['artworks'] = $this->Artwork_model->get_artworks($language_id, $limit, $offset, $artwork_id);
$this->template->write('title', 'Artwork : Manage Artwork');
$this->template->write_view('content', 'artwork/index', $data);
$this->template->render();
}
Here is an example for query with multiple joins in the model
public function get_artworks($language_id = 1, $limit = 10, $offset = 0, $arwork_id = null)
{
$this->db->select('a.id, a.price, a.is_shop, at.title,at.status,at.date_added,ats.name as artist_name');
$this->db->from('artworks a');
$this->db->join('artwork_translations at', 'a.id = at.artwork_id');
$this->db->join('artists ats', 'a.artist_id = ats.id');
$this->db->where('at.language_id', $language_id);
if(!is_null($arwork_id) && !empty($arwork_id) && $arwork_id != 'all')
{
$this->db->where('a.id =', $arwork_id);
}
$this->db->order_by('a.id DESC');
$this->db->limit($limit, $offset);
$artworks = $this->db->get();
return $artworks->result();
}
In the View
<?= $this->pagination->create_links(); ?>
The pagination class doesn't care about the how the data source is constructed, just so you hand it the data result object it wants. So you would just pass limits & offsets into your data queries, or else pull all your data and slice it up afterwards.
However, I don't really understand how you are thinking to combine these different bits of data into a single result to display - are you trying to display all categories, then paginate the products? If so, you are set up incorrectly
Simply use PHP function array_merge
<?php $beginning = 'foo'; $end = array(1 => 'bar');
$result = array_merge((array)$beginning, (array)$end);
$this->load->view('view.php', $result);
?>
and extract according to keys for array used.
It is really simple & working
I have used join query to fetch the data, when some data is deleted from the table which is joined then it creating problem with pagination
controller code
function index($msg='',$offset = 0)
{
$data = array('title'=>'Towns','message'=>'', 'link_add'=>site_url('manage/town/add'), 'edit_link'=>site_url('manage/town/edit'), 'tbl'=>'towns' );
$uri_segment = 4;
$offset = $this->uri->segment($uri_segment);
// load data
$value=('towns.Id,towns.Name as TownName,city.Name as CityName,city.Status,towns.Status,towns.TaxAmount');
$data['list_records'] = $this->admin_model->get_joinlist($data['tbl'],$value,'city','city.Id = towns.cityId','left outer','towns.Id','asc',array('towns.Status !='=>'Delete','city.Status'=>'Enable'),$this->limit, $offset)->result();
if($msg=='m')$data['message'] = 'New Town has been added successfully!';
// generate pagination
$this->load->library('pagination');
$config['base_url'] = site_url('manage/town/index/');
$this->total = $this->admin_model->**count_all**($data['tbl'],array('Status !='=>'Delete'));
$config['total_rows'] = $this->total;
$config['per_page'] = $this->limit;
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['j'] = 0 + $offset;
$data['total_rows']= $this->total;
// load view
$this->load->view('manage/includes/header', $data);
$this->load->view('manage/town', $data);
$this->load->view('manage/includes/footer');
}
<?php
class Admin_model extends CI_Model {
//listing with join
public function get_joinlist($table,$value,$table2,$condi,$join_type,$order_by,$order,$where,$limit, $offset)
{
$this->db->select($value);
$this->db->join($table2,$condi,$join_type);
$this->db->order_by($order_by,$order);
$this->db->where($where);
return $query= $this->db->get($table, $limit, $offset);
}
//For pagination
function count_all($table,$where)
{
return $this->db->where($where)
->count_all_results($table);
}
function num_rows($table)
{
return $this->db->affected_rows($table);
}
Do something like this:
public function get_joinlist($table,$value,$table2,$condi,$join_type,$order_by,$order,$where,$limit, $offset)
{
$this->db->start_cache();
$this->db->select($value);
$this->db->join($table2,$condi,$join_type);
$this->db->order_by($order_by,$order);
$this->db->where($where);
$this->db->stop_cache();
$query['num_rows']=$this->db->get($table)->num_rows();
$query['results']=$this->db->get($table, $limit, $offset);
$this->db->flush_cache();
return $query;
}
This way, your model will return an array with the the total rows of the join and the results limited by offset and limit rows
I've used codeigniter to make an easy search, no ajax or something, and I want to integrate the pagination class, I have read some examples and they use a library like this $this->table->generate($results); and I need in each result a button to edit that entry. I already did, but now with that way of showing the pagination I have a big problem, please, could anyone help me with this issue? I just need a clue or something to find in CI
I put here my model, controller and view, just in case
controller
public function searchdescription()
{
//$keyword = $this->input->post('keyword');
$keyword = $_POST['keyword'];
$this->load->model('searchdesc/searchdesc_model');
$data['retorno'] = $this->searchdesc_model->querydb($keyword);
$this->load->view('searchdesc/searchresults_view', $data);
}
model
function querydb($data,$num, $offset)
{
$queryresult = $this->db->query("select * from data where deleteflag=0 and title like '%$data%' or text like '%$data%' LIMIT $num, $offset ");
return $queryresult->result_array();
}
view
foreach ($retorno as $val)
{
echo $val['id'];
echo $val['title'];
echo $val['text'];
...here are some forms autocreated in each row that i need to keep making it
}
Here is an example (not using a model)
public function big()
{
$this->load->library('pagination');
$this->load->library('table');
$config['base_url'] = base_url().'/site/big/';
$where = "bot = '2' OR bot = '0'";
$this->db->where($where);
$config['total_rows'] = $this->db->count_all_results('visitors');
$config['per_page'] = 15;
//$config['num_links'] = 20;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$where = "bot = '2' OR bot = '0'";
$this->db->where($where);
$this->db->select('id, ip, date, page, host, agent, spammer, country, total, refer');
$this->db->order_by("date", "DESC");
$data['records'] = $this->db->get('visitors', $config['per_page'], $this->uri->segment(3));
$this->table->set_heading('Id', 'IP', 'Date', 'Page', 'Host', 'Agent', 'Spam', 'Country', 'Total', 'Referer');
$this->db->select_sum('total', 'trips');
$query = $this->db->get('visitors');
$data['trips'] = $query->result();
$this->load->view('site_view', $data);
}
In the view where I want the table:
<?php echo $this->table->generate($records); ?>
<?php echo $this->pagination->create_links(); ?>
to add buttons in the rows do something like this
foreach($records as $row){
$row->title = ucwords($row->title);
$this->table->add_row(
$row->date,
anchor("main/blog_view/$row->id", $row->title),
$row->status,
anchor("main/delete/$row->id", $row->id, array('onClick' => "return confirm('Are you sure you want to delete?')")),
anchor("main/fill_form/$row->id", $row->id)
);
}
$table = $this->table->generate();
echo $table;
Of course you will modify to fit your needs