CakePHP Ajax search : Showing same data two times for pagination - cakephp

Here ajax search working fine but the problem is same data is showing two times, because I have given pagination limit 2.If I make it 3, same result is showing 3 times.
Here is my index.ctp
$( "#search" ).keyup(function() {
var value=$('#search').val();
$.get("<?php echo Router::url(array('controller'=>'userTypes','action'=>'search'));?>",{search:value},function(data){
$('.search_data').html(data);
});
})
Here is my search action
public function search()
{
$search=$_GET['search'];
$request=$this->UserType->find('all', array(
'conditions' => array('UserType.name LIKE' => "%$search%")
));
$this->set('usertype',$request);
}
Here is the search.ctp
<?php foreach($usertype as $usertypes) { ?>
<tr>
<td><?php echo $usertypes['UserType']['id']; ?></td>
<td><?php echo $usertypes['UserType']['name']; ?></td>
<td><?php echo $usertypes['UserType']['description']; ?></td>
</tr>
<?php } ?>
Pagination limit In appcontroller
parent::beforeFilter();
$this->Paginator->settings = array(
'limit'=>2
);
Result is showing me like this
May anybody help me for solve it ?

If you want to use pagination, your search method should look like this:
public function search()
{
if(isset($this->request->query['search'])){
$search = $this->request->query['search'];
}else{
$search = '';
)
$this->Paginator->settings = array(
'conditions' => array('UserType.name LIKE' => "%$search%"),
'limit' => 2
);
));
$this->set('usertype',$this->Paginator->paginate());
}

Related

Cakephp application form

I created an application form using cakephp 2.
Now I want to know, How can users view only their application details using their user id. Here is the Form controller, the application form and the table for displaying the form
//Form controller
public function index() {
$this->set('posts', $this->Post->find('all'));
}
public function view($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post', $post);
}
public function add() {
if ($this->request->is('post')) {
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Flash->success(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Flash->error(__('Unable to add your post.'));
}
}
//Create form
echo $this->Form->create('Post');
echo $this->Form->input('esta',['label'=>'New or Estabilished']);
echo $this->Form->end('Save Post');
//Form display
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $this->Html->link($post['Post']['describe_idea'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
</td>
<td><?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($post); ?>
</table>
You said that you are using cakephp 2.x please find below code to find record
for Single Record
$posts = $this->Post->find('first', array(
'conditions' => array('id' => 1)
));
For Multiple record
$posts = $this->Post->find('all', array(
'conditions' => array('id' => 1)
));
Add filter in your action
CakePHP 3.x
$posts = $this->Posts->find()
->where([
'Posts.user_id' => $this->Auth->user('id')
]);
CakePHP 2.x
$posts = $this->Posts->find('all', [
'user_id' => $this->Auth->user('id')
]);
Note: Make sure to login user to set Auth data.

cakephp pagination controller

what shoud i write in the controller to have pagiation
this is my index
<?php foreach ($bien['Servicebien'] as $servicebien): ?>
<tr> <td><?php echo $servicebien['dateServiceBienDu']; ?>
<td><?php echo $servicebien['dateServiceBien']; ?></td>
<td><?php echo $servicebien['montantServiceBien']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($servicebien); ?>
</table>
<div>
<?php echo $this->Paginator->counter(array('format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}'))); ?>
Paginator->prev('<< Previous', null, null, array('class' => 'disabled'));
echo $this->Paginator->numbers();
echo $this->Paginator->next(' Next >> ', null, null, array('class' => 'disabled'));
?>
$data = $this->Paginator->paginate('model_name');
$this->set('data', $data);
Try this
class FeedsController extends AppController {
public $components = array( 'Search.Prg');
public function beforeFilter() {
parent::beforeFilter();
}
function indes(){
$this->Prg->commonProcess();
$this->{$this->modelClass}->data[$this->modelClass] = $this->passedArgs;
$parsedConditions = $this->{$this->modelClass}->parseCriteria($this->passedArgs);
$this->paginate = array(
'conditions' => array(),
'limit' => 10,
'fields' => array(),
'order' => 'id DESC'
);
$result = $this->paginate();
}
}

how give pagination to controllers

in controller
public function home($id=null){
$this->loadModel('Usermgmt.User');
if(isset($id)){
$groups=$this->User->findAllByuser_group_id($id);
$this->set('groups', $groups);
} else{
echo 'no id';
$users=$this->User->find('all');
$this->set('users', $users);
}
}
here i geeting value which matches the user_group_id and prints here i am geeting more then 10 users but i need to print only 5 on one page and need to give pagination how to give pagination here
view
<?php
if (!empty($groups)) {
// print_r($groups);
$sl=0;
foreach ($groups as $row1) {
//print_r($row1);
$sl++; ?>
<div style="width:100%;display:inline-block;">
<div style="float:left">
<?php
//echo $row1['id'];
echo $this->Html->link($this->Html->image('../files/user/photo/'.$row1 ['User']['photo_dir'].'/'.$row1 ['User']['photo'], array('width' => '180', 'height' => '180')),
array('controller'=>'Profiles','action'=>'index',$row1['User']['id']),
array('escape' => false));
?>
</div>
<div>
<?php echo h($row1['User']['first_name'])." ".h($row1['User']['last_name'])."</br>";
echo h($row1['User']['username'])."</br>";
echo h($row1['User']['email'])."</br>";
echo h($row1['User']['mobile'])."</br>";
echo h($row1['UserGroup']['name'])."</br>";
?></div>
<div style="clear:both;"></div>
</div>
<?php }
}?>
Call paginate
The equivalent paginate call to this:
$groups=$this->User->findAllByuser_group_id($id);
(incidentally - that should be findAllByUserGroupId) is this:
$groups = $this->paginate('User', array('user_group_id' => $id));
Changing default options
To achieve:
i need to print only 5 on one page
Modify the paginate property of your controller, e.g.:
class UsersController extends AppController {
public $paginate = array('limit' => 5);
function home($id = null) {
...
$conditions = array();
if ($id) {
$conditions['user_group_id'] = $id;
}
$groups = $this->paginate('User', $conditions);
...
}
}
View Modifications
In your view, the pagination helper, and the pagination information will automatically be available by calling paginate as shown above. To get pagination links use the pagination helper:
echo $this->Paginator->numbers();
See the documentation for more information and examples of using pagination.
As in cakephp pagination can be done using pagination helper of cakephp
In controller action use $this->paginate . From Model return all parameters like fields, conditions and so on in array format , so now in controller you will write
$data = $this->ModelName->functionname();
$currentPage = isset($_REQUEST['currentpage']) ? $_REQUEST['currentpage'] : '';
$this->paginate = array(
'fields' => $data['fields'],
'conditions' => $data['conditions'],
'order' => $data['order'],
'page' => $currentPage,
'limit' => 25
);
$currentPage is fetched from $_REQUEST['currentpage'];
and finally pass this data to Paginate Helper
$data = $this->paginate('ModelName');
Now in view you will use Pagination functions as shown below
<?php echo $this->Paginator->prev('revious'); ?>
<?php echo $this->Paginator->numbers(); ?>
<?php echo $this->Paginator->next('Next'); ?>

How to get and show data from different models in Cakephp

I have 3 tables: orders, discounts and products in the way that product has many discounts (discount times). Discount has many orders. in other words, it looks like: Product > Discount > Order.
How can I get all data from 3 table, put it into one order view (do it in Order controller) and show it out (in Order view).
That's what I did, but It didnt work:
//Order controller
public function index() {
$this->Order->recursive=1;
$this->set('orders', $this->Order->find('all'));
}
// Order view
<?php foreach ($orders as $order): ?>
<tr>
<td><?php echo $order ['Order']['order_id']; ?></td>
<td><?php echo $this->Html->link($order['Product']['product_name'], array('controller' => 'products', 'action' => 'view', $order['Product']['product_id'])); ?></td> //I showed it here
<td><?php echo $order['Order']['order_date']; ?></td>
<td><?php echo $order['Order']['payment']; ?></td>
<?php endforeach; ?>
<?php unset($order); ?>
Please help me! thanks in advance.
Since you mention that you are in Order controller you can do any of these:
$this->Order->Behaviors->load('Containable');
$orders = $this->Order->find('all', array(
'contain' => array('Discount' => 'Product')));
$this->set(compact('orders'));
Now, in your view you can iterate over $orders variable.
PS: Of course, I am assuming here that Discount Belongs to Product.
You can do this easy way and different functionality
public function index() {
$this->set('datas',
$this->Order->find('all',
array (
'conditions' => array('Site_id' => $site_name),
'fields' => array('Date_time','Ac_1_Status','Tempin'),
'order' => array('id' => 'desc'),
'limit' => 200
)
)
);
}

cakephp 2x pagination of a different model

I'm trying to paginate Workers which belong(s)To Job within a JobsController.
class JobsController extends AppController {
var $name = 'Jobs';
var $helpers = array('Html', 'Form', 'Js');
var $paginate = array(
'Worker' => array(
'limit' => 5,
'recursive' => 0,
'model' => 'Worker',
'order' => array('age' => 'ASC')
),
);
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Job.'));
$this->redirect(array('action'=>'index'));
return;
}
$this->Job->id = $id;
$workers = $this->paginate('Worker', array('Worker.job_id' => $id));
if ($workers) {
$this->set('workers', $workers);
}
}
In view.ctp:
<?php
$this->Html->script(array('jquery.min'), array('inline' => false));
$this->Paginator->options(array(
'update' => '#content',
'evalScripts' => true,
));
?>
<?php if (isset($workers)): ?>
<?php echo $this->Paginator->numbers(array('model' => 'Worker')); ?>
<table>
<tr>
<th>Age</th>
<th>Info</th>
</tr>
<?php foreach ($workers as $worker): ?>
<tr>
<td>
<?php echo $worker['Worker']['age']; ?>
</td>
<td>
<?php echo $worker['Worker']['info']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $this->Paginator->numbers(array('model' => 'Worker')); ?>
<p>
<?php
echo $this->Paginator->counter(array(
'model' => 'Worker',
'format' => __('Page %page% of %pages%, showing %current% records out of %count% total.')
));
?></p>
<?php endif; ?>
<?php echo $this->Js->writeBuffer(); ?>
I'm getting the correct list of workers. But the links generated by numbers are not working. They look like /view/2/page:2/sort:Worker.age/direction:ASC
What am I doing wrong? cakephp version is 2.4.1.
Try this in your view action, by setting the order at run time.
$this->paginate['Worker']['order'] = array('Worker.age' => 'ASC')
Now your function look like this
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Job.'));
$this->redirect(array('action'=>'index'));
return;
}
$this->Job->id = $id;
$this->paginate['Worker']['order'] = array('Worker.age' => 'ASC');
$workers = $this->paginate('Worker', array('Worker.job_id' => $id));
if ($workers) {
$this->set('workers', $workers);
}
}
Hope this helps you.

Resources