cakephp pagination with advance Filter - php

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);
}

Related

How to create a function in controller and use it in template/view in cakephp 3.xxx

I need to use the function in my view/template page.
My code:
public function getAppNumber($id = null){
$aPPLICATIONINFO = $this->AppEstLto->APPLICATIONINFO->find('all', [
'fields'=>['application_number'],
'conditions'=>['application_id'=>$id],
'limit' => 200
]);
foreach($aPPLICATIONINFO as $aPPLICATIONINFO){
$aPPLICATIONINFOx = $aPPLICATIONINFO;
}
return $aPPLICATIONINFOx;
}
You can use set() to use the function variables in your view as given in cookbook:https://book.cakephp.org/3/en/views.html#setting-view-variables
public function get_app_number($id = null){
$applicationInfo = $this->AppEstLto->APPLICATIONINFO->find('all',
[
'fields'=>['application_number'],
'conditions'=>['application_id'=>$id],
'limit' => 200
]);
//Create an array
$applicationArray = new array();
//Store all results in array
foreach($applicationInfo as $application){
$applicationArray = $application;
}
// Pass the array to view
$this->set(compact('applicationArray'));
}
Now, you can use it in your view:
get_app_number.ctp:
<?php
foreach($applicationArray as $application)
{
echo $application['application_number'];
}
?>
You should be doing something like this in your routes.php file inside your Config folder:
Router::connect('/get-app-number', array('controller' => 'yourController', 'action' => 'get_app_number'));
That way you will be able to connect the url that will be used for your view.
Your action corresponds and sends data to your view.
The action is the function that is inside your controller in which you developed for setting the data and variables.
The example of the slug which would be generated:
http://localhost/get-app-number

CakePHP: Value from $this->request->data being lost on pagination postback

I have an application that takes a value from a dropdown list and uses it to filter an index view. Here is the controller code for that:
public function browse() {
$this->loadModel('ReqMissionId');
$missions = $this->ReqMissionId->find('list', array('fields' => array('mission_id', 'mission_id')));
$this->set(compact('missions'));
}
This is the Controller code for the index:
public function index() {
if($this->request->is('post')) {
$options = array('Requirement.mission_id' => $this->request->data['Requirement']['mission_id']);
$this->set('mission_id', $this->request->data['Requirement']['mission_id']); //Used to display the mission_id on the index page.
$this->Paginator->settings = $this->paginate;
$this->set('requirements', $this->Paginator->paginate('Requirement', $options));
}
}
The dropdown in the View is simple:
<?php
echo $this->Form->input('mission_id', array('label' => 'Mission ID'));
echo $this->Form->submit(__('View'));
echo $this->Form->end();
?>
My problem is pagination. Since Cake does a postback when it sorts the columns, the value of 'Requirement.mission_id' gets lost and I get a "Call to member function sort() on null" error. I feel like I can take care of this with a session variable, but I'm unclear on the implementation. Is that correct, or is there something else I should be doing?
Check the session for the model that you're paginating. Try something like this:
Try something like this:
if(!empty($this->request->data['Requirement']))
$this->Session->write('Requirement', $this->request->data['Requirement']);
else
$this->request->data['Requirement'] = $this->Session->read('Requirement');
To get it into your $options. Try use array merge like this:
$options = array_merge($options, $this->getOptions($this->request->data));
Implement getOptions under:
function getOptions($data = null){
$options = array();
if(!empty($data['Requirement']['mission_id']))
$options = array_merge($options, array('mission_id' => $data['Requirement']['mission_id']));
return $options;
}

Zend Form getValues() doesn't work

I'm trying to create simple form with Zend, I need to use this form in most part, so I create the default form then in controller i modify it for the occurrence with private function. But I have two problems:
the form getValues() doesn't take the value of text element.
I put render at the end of the form action, but it doesn't render to the right page.
The form consists of a text field and the sumbit button
Here is the code of my controller:
That is for customize the form
private function getSearchForm($action = '', $name, $type, $placeholder)
{
$urlHelper = $this->_helper->getHelper('url');
$this->_searchForm = new Application_Form_Admin_Search_Search();
$this->_searchForm->setName($name);
$text = $this->_searchForm->getElement('ricerca');
$text->setLabel('Ricerca '.$type);
$text->setName($type);
$text->setAttrib('placeholder', $placeholder);
$this->_searchForm->setAction($urlHelper->url(array(
'controller' => 'admin',
'action' => $action),
'default'
));
return $this->_searchForm;
}
there are the actions:
public function pneumaticoAction()
{
$this->_searchForm = $this->getSearchForm('pneumaticosearch', 'search', 'pneumatico', 'Ricerca per: modello, marchio o codice');
$this->view->searchForm = $this->_searchForm;
}
public function pneumaticosearchAction()
{
if (!$this->getRequest()->isPost()) {
$this->_helper->redirector('index', 'public');
}
$form=$this->_searchForm;
if (!$form->isValid($this->getRequest()->getPost())) {
$this->render('pneumatico');
}
$values = $form->getValues();
$this->view->assign(array(
"pneumatici" => $this->_modelAdmin->searchPneumatici($values['pneumatico'])
));
$this->render('pneumatico');
}
First question, whenever you get routed to pneumaticosearch action, you do not set $this->_searchForm but you have it as:
$form=$this->_searchForm;
Should be something like this:
$form = $this->getSearchForm('pneumaticosearch', 'search', 'pneumatico', 'Ricerca per: modello, marchio o codice');
And the second question. When you run render, it is similar to pass $this->view parameters to .phtml. I don't see your view files, but I guess you need to set view first:
$this->view->searchForm = $form

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.

Categories