I recently upgraded my FuelPHP to v1.5. Now I'm trying to set up the pagination class.
This is my code:
$data['news'] = Model_News::find()->where('font', '>', -1);
$count = count($data['news']);
$config = array(
'pagination_url' => 'http://localhost/public/mobile/barbecue/',
'total_items' => $count,
'per_page' => 12,
'uri_segment' => 3,
);
$pagination = Pagination::forge('mypagination', $config);
$data['news'] = Model_News::find()->where('font', '>', -1)->order_by(array('date' => 'desc'))->limit($pagination->per_page)->offset($pagination->offset)->get();
$data['pagination'] = $pagination->render();
$this->template->title = "Teams";
$this->template->content = View::forge('news/index', $data);
I made a copy of the core/config/pagination.php to app/config/pagination.php.
And on my view news/index.php the pagination is seted: <?php echo $pagination ?>
Result:the information obtained by the query are displayed normally, but the string $ pagination returns nothing. Fuel displays no error.
What I do? Thanks.
In new version of Fuelphp, do this changes in view:
echo html_entity_decode($pagination);
And update the pagination config file.
Below are some change given.
'previous-marker' => "<",
//and
'next-marker' => ">"
In your view, print the pagination with:
<?php echo \Pagination::create_links(); ?>
instead of
<?php echo $pagination ?>
For FuelPHP v1.5+, that would be
<?php echo $pagination->render(); ?>
Related
I'm relatively new in Code Igniter and I'm trying to use the Code Igniter pagination class for the first time. I'm trying to get some rows in my database table, depending on the state, lga and category selected by the user. These three are passed to the function as parameters. I have followed the process I found on SO and some other sites to implement pagination on the page, and it seems to work, except that it doesn't return all the rows that has the specified parameters.
This what I have:
Model - Posts_model.php
public function count_lga_cat_posts($state, $lga, $category) {
return $this->db->get_where('lga_posts', array('state' => $state, 'lga' => $lga, 'category' => $category, 'display' => 'true'))->num_rows();
}
public function fetch_lga_cat_data($limit, $start) {
$this->db->limit($limit, $start);
$this->db->order_by("id", "desc");
$query = $this->db->get("lga_posts");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Controller - Lga_posts.php
public function selected_state_lga_posts_category($state, $lga, $category)
{
//config for pagination
$config = array();
$uri_seg = 5;
$config["base_url"] = base_url('state2/'.$state.'/'.$lga.'/'.$category); //as specified in routes
$config["total_rows"] = $this->posts_model->count_lga_cat_posts($state, $lga, $category);
$config["per_page"] = 3;
$config["uri_segment"] = $uri_seg;
$config['use_page_numbers'] = TRUE;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['first_link'] = 'Back to first page';
$config['next_link'] = 'View more posts';
$config['prev_link'] = 'Previous posts';
$config['last_link'] = 'Jump to last page';
$config['display_pages'] = FALSE;
$this->pagination->initialize($config);
$page = ($this->uri->segment($uri_seg))? $this->uri->segment($uri_seg) : 0;
$this->db->where(array('state' => $state, 'lga' => $lga, 'category' => $category, 'display' => 'true'));
$data["results"] = $this->posts_model->fetch_lga_cat_data($config["per_page"], $page);
$str_links = $this->pagination->create_links();
$data["links"] = explode(' ',$str_links );
$data['state'] = $state;
$data['lga'] = $lga;
$data['category'] = $category;
$this->load->view('posts/lga/selected_state_lga_posts_category', $data);
When I load this page, everything seems to work fine, the pagination links are working as expected, but some rows in the table are not pulled, even though they have the same parameters as the ones that were pulled and displayed. If I change per_page config to 1, the last page gives an error "invalid argument in foreach" or something similar. I wonder if it has something to do with my query, since I do not want to get all the rows in the table, but the ones that have the selected parameters i.e. state, lga and category.
Can someone please point out to me what I'm doing wrong?
This is my pagination function under which pagination code is written and i have called this pagination function(abc(offset=0)) in another function login_process in else if part, and passed abc function value to view admin_view inside the function login_process and in my login_view i have used pagination code and in that fetched data is shown with pagination but when i click the link 1 2 or any then comes error The requested URL /terse/welcome/abc/1 was not found on this server.and pagination is not implimented
my pagination function code:
public function abc($offset = 0){
$this->load->model('insert_model');
//$data ['query'] = $this->insert_model->view_data();
$this->load->library('table');
// Load Pagination
$this->load->library('pagination');
// Config setup
$config['base_url'] = base_url().'/welcome/abc';
$items= $config['total_rows'] = 20;
$perpage=$config['per_page'] = 1;
// I added this extra one to control the number of links to show up at each page.
$config['num_links'] = 5;
// Initialize
$this->pagination->initialize($config);
// Query the database and get results
// Here we add the limit and the offset
// The second parameter is the limit, since we are showing
// 10 per page, the limit should be 10
$data['books'] = $this->db->get('register',5, $offset);
// Create custom headers
$header = array('id','username','lastname','email' , 'password', 'image','status');
// Set the headings
$this->table->set_heading($header);
// Load the view and send the results
return $data['books'];
}
my login_process function in which i have called abc and passed its value to admin_view(only see its elseif part)
public function login_process()
{
$this->load->model('insert_model');
$data['users'] = array(
'fname' => $this->input->post('fname'),
'pass'=> $this->input->post('pass'),
);
$status= $this->insert_model->login_test($data);
$st['ss']=$this->insert_model->stat($data);
//print "<pre>"; print_r($status); die();
if($status == true && $st['ss'] [0] ['status'] == 0)
{
$ses = array(
'username' => $this->input->post('fname'),
'password' => $this->input->post('pass'),
);
$this->session->set_userdata($ses);
$im['pic']=$this->insert_model->image_fetc();
$this->load->view('header_view',$im);
$this->load->view('navside_view');
$this->load->view('user_view');
}
elseif($status == true && $st['ss']['0'] ['status'] == 1)
{
$ses = array(
'username' => $this->input->post('fname'),
'password' => $this->input->post('pass'),
);
$this->session->set_userdata($ses);
$im['pic']=$this->insert_model->image_fetc();
// print_r($aaa); die();
// $data['aa'] = "sd";
$data['books'] = $this->abc();
$this->load->view('header_view',$im);
$this->load->view('navside_view');
$this->load->view('admin_view', $data);
}
else
{
$this->load->view('header_view');
$this->load->view('navside_view');
$this->load->view('center_view_login');
}
}
my admin_view code
echo $this->table->generate($books); ?>
<?php echo $this->pagination->create_links(); ?>
please check what i have done wrong..
I had a similar problem and it was solved when I added the path to the controller function in my appplication/config/routes.php file
something similar to:
$route['terse/welcome/abc/(:num)'] = 'terse/welcome/abc';
Customize it. Hope it helps you.
I'm using codeigniter
i want to show some data taken form a database by querying as following.
$this->db->where('sex !=', $iam);
$this->db->where('sex', $searching_for);
$this->db->where('Age >=' , $age_from);
$this->db->where('Age <=' , $age_to);
if($Province != 1){
$this->db->where('Province' , $Province);
}
$this->db->limit($limit, $start);
$query = $this->db->get("members");
return $query->result_array();
The $iam,$searching_for, $age, $age_to is provided by user and I'm passing them from conttroller file using session array.
$search_info=array(
'iam' => $this->input->post('iam'),
'searching_for' => $this->input->post('searching_for'),
'age_from' => $this->input->post('age_from'),
'age_to' => $this->input->post('age_to'),
'country' => $this->input->post('country'),
'Province' => $this->input->post('Province')
);
$this->session->set_userdata(array("search_info" => $search_info));
and my pagination function is also in controller file and it is like this
public function pagination(){
$this->load->library("pagination");
$config = array();
$config["base_url"] = base_url() . "controller_search/index";
$this->load->model('models_search');
$config["total_rows"] = $this->models_search->search();
$config["per_page"] = 1;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
//$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;3
//echo $this->uri->segment(3);
//echo ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;3;
$page = $this->uri->segment(3);
$data["search_result"] = $this->models_search->fetch_categories($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$data['error'] = '';
$this->load->view('home_header.php');
$this->load->view('search/search_result',$data);
}
But the thing is when i click the page numbers it didn't show any thing, i tried commenting all the where clues in the query and then it works.So i think the error is in session array and i tried to var_dump the session_all so then it shows array(0){}
Can anyone help me in this case ?
Use this code:
$this->session->set_userdata("search_info" => $search_info);
code to set session
Set your session data by this code:
$this->session->set_userdata("sess_data", "mydata");
Retrive this session data by this code:
$this->session->userdata("sess_data");
I think you are accessing session array wrong way.
Instead of
$iam
use
$this->session->userdata('iam');
Or
$this->session->userdata('$iam');
Whichever works in your case....
I just took all the posted values to variables in index function and then i put them in to session array as
$search_info=array(
'iam' => $iam,
'searching_for' => $searching_for,
'age_from' => $age_from,
'age_to' => $age_to,
'country' => $country,
'Province' => $Province
);
$this->session->set_userdata(array("search_info" => $search_info));
then i redirect it to pagination function. then it works neatly. before it was overwrite the post items and the session array when i moving towards pages. now because of the redirect it was stoped. as i think. any way it is working now very well
than you every one for helping me.
specially Mr.John Blake thank you very much sir.
check if you have load the session library.If not either load in your controller as
$this->load->library('session')
or you may auto load session in library of config/autoload.php
i have a page where books are to be display either with "grid" view or with "list" view.
So my current url is
http://localhost/thebestbookfinder.com/viewallbooks/books/pgn/grid/8
and if i click on "list" link from my view page then my current page should load with only changing this part /grid/ to /list/ and other same.
i.e http://localhost/thebestbookfinder.com/viewallbooks/books/pgn/list/8
I tried this but this is not helpful. Please help me to solve this issue.
inMyViewPage.php
<td width="10%" style="font-size:12px;color: red;">
<?php
echo "".anchor('#', 'Grid', array('id' => ''))."";
echo "".anchor(base_url().'viewallbooks/books/pgn/'.$this->uri->segment(4),' List', array('id' => ''))."";
?>
</td>
Instead of link if form submition solve my problem then please help me with that
Controller.php
function books()
{
$config = array();
$config['base_url'] = base_url().'viewallbooks/books/pgn/'.$this->uri->segment(4);
$config["total_rows"] = $this->Booksmodel->record_count_for_secondtopBooks('3');
$config['uri_segment'] = 5;
$config['per_page'] = 8;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
$data["query"] = $this->Subjectmodel->get_subjects();
$data["query1"]= $this->Editionmodel->get_edition();
$data["query2"]=$this->Booksmodel->get_all_book_list_atHomeTop('3',$config["per_page"], $page);
$viewType = ($this->uri->segment(4)) ? $this->uri->segment(4) : 'list';
if($viewType=='list')
{
$this->load->view('commonfiles/bookslistview',$data);
}
else {
$this->load->view('commonfiles/booksgridview',$data);
}
}
Need to add route in route.php like this
$route['viewallbooks/books/(:any)'] = "viewallbooks/books/$1";
Function books() shold contain 3 params with values by default
Below is more correct varian
function books($img_type = 'png', $view_type = 'list', $page = 0) {
$config = array();
$config['base_url'] = base_url().'viewallbooks/books/'.$img_type.'/'.$view_type.'/'.$page;
$config["total_rows"] = $this->Booksmodel->record_count_for_secondtopBooks('3');
$config['uri_segment'] = 5;
$config['per_page'] = 8;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data["query"] = $this->Subjectmodel->get_subjects();
$data["query1"]= $this->Editionmodel->get_edition();
$data["query2"]= $this->Booksmodel->get_all_book_list_atHomeTop('3',$config["per_page"], $page);
if($viewType=='list')
$this->load->view('commonfiles/bookslistview',$data);
else
$this->load->view('commonfiles/booksgridview',$data);
}
Do this one:
<?php echo "".anchor(base_url().'viewallbooks/books/pgn/grid/'.$this->uri->segment(5),' Grid', array('id' => ''))."";
echo "".anchor(base_url().'viewallbooks/books/pgn/list/'.$this->uri->segment(5),' List', array('id' => '')).""; ?>
And no need to change anything in above controller.
You will have to do either:
Reload the page with the new url (grid) to render that template
OR:
Render both the grid and list view and then dynamically (with some css/javascript) hide content based on what you'll want to see.
OR:
Load the grid-view with AJAX and replacing the list with it.
I have done the pagination in Kohana 3.1 using the following code in my controller page .How can I display the Pagination links in my view page ? Is this the correct way ??? or any issue with my code ?
public function action_imagelist()
{
$per_page =2;
$page_num = $this->request->param('page', 1);
$offset = ($page_num - 1) * $per_page;
$view =View::factory('image/imagelist')->bind('page_links',$page_links)->bind('results', $results)->bind('pagination', $pagination);
// Get the total count of records in the database
$userid = Auth::instance()->get_user()->pk();
$count=ORM::factory('user_image')->where('app_userid','=',$userid)->count_all();
// Create an instance of Pagination class and set values
$pagination = Pagination::factory(array(
'total_items' => $count,
'current_page' => array('source' => 'image/imagelist', 'key' => 'page'),
'items_per_page' => $per_page,
'offset' => $offset,
'view' => 'pagination/basic'
));
// Load specific results for current page
$results = DB::select()->from('user_images')
->where('app_userid','=',$userid)
->order_by('image_id','ASC')
->limit($pagination->items_per_page)
->offset($pagination->offset)->execute();
// print_r($results);
$page_links = $pagination;
$this->template->content=$view->render();
}
To display pagination, in your view print out $page_links.
You should read how stuff is divided in MVC pattern!