Why pagination not working correctly? - php

I have a problem. My Kohana pagination script not working correctly. Must have as follows : [http://127.0.0.1/?page=1], but I have the following - [http://127.0.0.1/index.php/?page=1] and records from database in home page are 2 (total records is 2, i set items_per_page to 1), but must have a 1 record only. Where is the problem?
Controller:
public function action_index()
{
$pagination = Pagination::factory(array(
'total_items'=> Model::factory('index')->get_count(),
'items_per_page' => 1,));
$articles_ = Model::factory('index')->do_magic();
$this->template->page_title = 'Sākums';
$this->template->site_name = Kohana::$config->load('common')->get('site_name');
$this->template->content = View::factory('index/index')
->set('query', $articles_)
->set('pagjinaacija', $pagination->render());
$this->template->styles[] = 'index/index';
}
View
<?php
foreach($query as $row) {
echo '<h2>'.$row['title'].'</h2>';
echo '<p style="margin:0">'.$row['content'].'</p>';
}
echo $pagjinaacija;
?>
And model
Class Model_Index Extends Model {
public function get_count() {
return DB::query(Database::SELECT, 'SELECT COUNT(*) AS count FROM posts')->execute()->get('count');
}
public function do_magic() {
$query = DB::query(Database::SELECT, 'SELECT * FROM posts ORDER By id DESC')->execute()->as_array();
return $query;
}
}

There are 2 problems:
You're not using url rewrite rules to rewrite /index.php?page=2 into /?page=2
You're not actually using the page query parameter to filter your records returned from the db, so it'll display them all.
The pagination object is only used to render a pagination control, not perform the actual filtering of db records.

you maybe set bootstrap.php:
Route::set('index_page','yourcontroller/index(/<page>)', array('page' => '[0-9]+'))
->defaults(array(
'controller' => 'yourcontroller',
'action' => 'index',
));

Related

cakephp pagination with advance Filter

I am using cake php scroll pagination to paging products.
I am using group of check boxes in view part to set manual condition by user to paginate.
I used following code to set a condition after ajax request.
class SearchController extends AppController {
public $helpers = array('Html', 'Form', 'Session','Paginator','Js' => 'Jquery');
public $components = array('RequestHandler','Session','Cookie','Paginator');
public function index() {
$this->loadModel("Bangsworkinghrs");
$agetworkinghrs = $this->Bangsworkinghrs->getworkinghrs();
$this->set('agetworkinghrs', $agetworkinghrs);
$this->loadModel("Gig");
$options = array('Gig.subcategory' => 'Logo Design');
if(!empty($this->request->data['filter']['workinghrs'])) {
$options = array('Gig.subcategory' => 'Logo');
}
$this->Paginator->settings = $options;
$agetGigsItem = $this->Paginator->paginate('Gig', $options);
$this->set('agetGigsItem', $agetGigsItem);
}
}
via this code i am able to change condition of paging but server explodes only default 20 records next records is not exploding by server and there is no error message my cakephp version is 2.6.8
$('#filter').submit(); try submit form and remove ajax refresh data from your jquery part replace filter with your form name.
You are missing 'page' and 'limit' attribute of Cakephp default pagination.
In your options array you need to pass page number and no of records per page.
Something like this but in dynamic way.
$options = array('conditions' => array('Gig.subcategory' => 'Logo Design'),'page'=>2,'limit'=>20);
using action="get" in the form where $this->request->query['filter']['workinghrs'] data is come.
Coding:
public $paginate = array(
'limit' => 20
);
public function index()
{
$this->loadModel("Gig");
$options = array('Gig.subcategory' => 'Logo Design');
if(!empty($this->request->query['filter']['workinghrs'])) {
$options = array('Gig.subcategory' => 'Vector Design');
}
$this->Paginator->settings = $options;
$agetGigsItem = $this->Paginator->paginate('Gig', $options);
$this->set('agetGigsItem', $agetGigsItem);
}

Laravel take out first result from collections

I have the following method in a controller
public function artikel(){
$breadcrumb = array(
'Home' => URL::to('/'),
'Artikel' => ''
);
$this->layout->title = "Egoji";
$artikels = Posting::orderBy("created_at","desc")->where("postings.tipe","=","artikel")->paginate(5);
$this->layout->content = View::make("frontend.artikel.index",array("artikels"=>$artikels, 'breadcrumb'=>$breadcrumb))->render();
}
in the view frontend.artikel.index i am trying to takeout first artikel using $artikels->shift(). i get the first artikel, but it doesnt remove from the collection, it still there when i loop the rest.

cakephp pagination and thin controller

i am developing with cakephp (2.4.7) and i have problems with organizing my controllers and models to use pagination.
So far i put the most logic into the models (thin controller, big model). There i returned the results to the controller where i set the variables to display it on the view.
But now i want to use pagination. This break my concept because i can not use pagination inside the models.
Whats the best solution to solve this problem? I do not want to reorganzie my whole structure, because i need pagination in a lot of different actions and models.
For example:
Controller Users, action friends
public function friends($userid = null, $slug = null) {
$this->layout = 'userprofile';
$this->User->id = $userid;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid User'));
}
$this->set('friends', $this->User->getFriendsFrom($userid));
}
User Model, function getFriendsFrom($user_from).. i need this method in different actions.
public function getFriendsFrom($user_from) {
$idToFind = $user_from;
$data = $this->FriendFrom->find('all',
array(
'conditions'=>array(
'OR'=> array(
array('user_to'=> $idToFind),
array('user_from'=> $idToFind)
),
'AND' => array(
'friendship_status' => 1
)
),
'contain' => array('UserFrom.Picture', 'UserTo.Picture')
)
);
$friendslist = array();
foreach ($data as $i) {
if ($i['FriendFrom']['user_from'] == $idToFind){
$friendslist[] = $i['UserTo'];
}
elseif ($i['FriendFrom']['user_to'] == $idToFind){
$friendslist[] = $i['UserFrom'];
}
}
return $friendslist;
}
Whats the best way to design this concept to use pagination?
Thanks
in Controller Users use cakephp Paginator
var $helpers = array('Paginator');
Now you call the following method
function index() {
$result = array(
'recursive' => -1,
'conditions' => array(...),
'contain' => array(...),
'limit' => '2'
);
// you can write the above code in your model
$this->paginate = $result;
$users = $this->paginate('User');
// Re-arrage $users
$this->set(compact('users'));
}
If any problem, let me know.

Using pagination with a custom model method in CakePHP

I'm setting up pagination to display a list of images belonging to the user in their account. This is what I have in my controller:
class UsersController extends AppController {
public $paginate = array(
'limit' => 5,
'order' => array(
'Image.uploaded' => 'DESC'
)
);
// ...
public function images() {
$this->set('title_for_layout', 'Your images');
$albums = $this->Album->find('all', array(
'conditions' => array('Album.user_id' => $this->Auth->user('id'))
));
$this->set('albums', $albums);
// Grab the users images
$options['userID'] = $this->Auth->user('id');
$images = $this->paginate('Image');
$this->set('images', $images);
}
// ...
}
It works, but before I implemented this pagination I had a custom method in my Image model to grab the users images. Here it is:
public function getImages($options) {
$params = array('conditions' => array());
// Specific user
if (!empty($options['userID'])) {
array_push($params['conditions'], array('Image.user_id' => $options['userID']));
}
// Specific album
if (!empty($options['albumHash'])) {
array_push($params['conditions'], array('Album.hash' => $options['albumHash']));
}
// Order of images
$params['order'] = 'Image.uploaded DESC';
if (!empty($options['order'])) {
$params['order'] = $options['order'];
}
return $this->find('all', $params);
}
Is there a way I can use this getImages() method instead of the default paginate()? The closest thing I can find in the documentation is "Custom Query Pagination" but I don't want to write my own queries, I just want to use the getImages() method. Hopefully I can do that.
Cheers.
Yes.
//controller
$opts['userID'] = $this->Auth->user('id');
$opts['paginate'] = true;
$paginateOpts = $this->Image->getImages($opts);
$this->paginate = $paginateOpts;
$images = $this->paginate('Image');
//model
if(!empty($opts['paginate'])) {
return $params;
} else {
return $this->find('all', $params);
}
Explanation:
Basically, you just add another parameter (I usually just call it "paginate"), and if it's true in the model, instead of passing back the results of the find, you pass back your dynamically created parameters - which you then use to do the paginate in the controller.
This lets you continue to keep all your model/database logic within the model, and just utilize the controller to do the pagination after the model builds all the complicated parameters based on the options you send it.

Implementing pagination in codeigniter, can't get count

I am having difficulties implementing the codeigniter pagination class. I have created my model, view and controller for getting my news articles and data is echoed in the view successfully.
My problem is that when I attempt to implement pagination it seems like I am unable to get the correct count of fields in my database. Can somebody show me what I have done wrong?
The pagination links display perfectly, but the content echoed does not appear to be limited. How can I count the rows of the query?
Required classes for the pagination are autoloaded
Model:
class News_model extends CI_model {
function get_allNews()
{
$query = $this->db->get('news');
foreach ($query->result() as $row) {
$data[] = array(
'category' => $row->category,
'title' => strip_tags($row->title),
'intro' => strip_tags($row->intro),
'content' => truncate(strip_tags( $row->content),200),
'tags' => $row->tags
);
}
return $data;
}
Controller
// load pagination class
$config['base_url'] = base_url().'/news/index/';
$config['total_rows'] = $this->db->get('news')->num_rows();
$config['per_page'] = '5';
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$viewdata['allnews'] = $this->News_model->get_allNews($config['per_page'],$this->uri->segment(3));
View
<?php if (isset($allnews)): foreach ($allnews as $an): ?>
<?php echo heading($an['title'], 2); ?>
<?php echo $an['content']; ?>
<?php endforeach;
else: ?>
<h2>Unable to load data.</h2>
<?php endif; ?>
<?php echo $this->pagination->create_links(); ?>
In your controller, you're passing parameters to your get_allNews method, but your method doesn't make use of those parameters:
$viewdata['allnews'] = $this->News_model->get_allNews($config['per_page'],$this->uri->segment(3));
So you are getting all records, and expecting the limited result set. You need to change the beginning of your get_allNews method like this:
class News_model extends CI_model {
// make use of the parameters (with defaults)
function get_allNews($limit = 10, $offset = 0)
{
// add the limit method in the chain with the given parameters
$query = $this->db->limit($limit, $offset)->get('news');
// ... rest of method below

Categories