cakephp pagination next and previous not showing - cakephp

I am using cakephp 1.3 and i am receiving strange problem with pagination.I have imnplemented pagination for I have working pagination code in more than one controller .But my rest of the controller not showing next and previous button and numbers for pagination is working fine
I am using this code in my view
<?php
if(!empty($zipcodes)){
echo "<div class='pagination'>";
echo #$this->Paginator->prev('« Previous', null, null, array('class' => 'disabled'));
echo #$this->Paginator->numbers();
echo #$this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
echo '<div style="float:right;padding:5px;color:#000">'.$this->Paginator->counter().'</div>';
echo "<div class='clear'></div>";
echo "</div>";
}
?>
And in my controller i have used pagination controller as i am paginating custom query results .I included pagination code in app_model.php.
My ontroller code is
var $paginate = array(
'Zipcode' => array('limit' => 5,
'order' => array('id' => 'desc'),
)
);
my query and operation
$tsql = " SELECT Zipcode.* ".
" FROM zipcodes AS Zipcode ".
" WHERE Zipcode.region_id=0 ";
$conditions = array(
'tsql'=>$tsql,
);
$tmp = $this->paginate('Zipcode',$conditions);
$this->set('zipcodes', $tmp);
can any one point out me what i am doing wrong.??And why pagination is working for only some controller .??? THanks in advance

First, change your paginate in the top of the controller. Remove Zipcode from it. It should just be:
var $paginate = array(
'limit' => 5,
'order' => array('Zipcode.id' => 'desc'),
);
Second, I am not sure what you are trying to accomplish with $tsql, but as far as I know, you cannot pass a SELECT statement as a condition. You need to write ORM specific conditions like this:
$this->paginate = array(
'conditions' => array('Zipcode.region_id' => 0),
);
Then to set zipcodes you can do this:
$this->set('zipcodes', $this->paginate('Zipcode'));
or this
$data = $this->paginate('Zipcode');
$this->set(compact('data'));
As a side note, echo #$this->Paginator->prev should be echo $this->Paginator->prev and all the # should be removed from those lines. That is poor coding practice. I'm not certain if you have the ignore in there for a reason, but it's bad.

Related

how to get fetch value in cakephp

$map=$this->Sessiondetails->find("all");
$this->set("map",$map);
foreach($map as $maps){
echo $maps['Sessiondetails']['latitude'];
}
I want to fetch only 3rd row values . How to do it in cakephp. I am using cakephp 2x.
How would you do it in SQL? Think about it and then use cake syntax
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#retrieving-your-data
$map = $this->Sessiondetails->find(
"all"
array(
'offset' => 2
'limit' => 1
)
);
of course if you want to run a query that retrieves all the records but then just show the 3rd record you can simply do
echo $map[2]['Sessiondetails']['latitude']
You can use 2way
If you use Paginator component you can use this :
$this->paginate = array('limit' => 10);
else :
$map = $this->Sessiondetails->find(
"all",
array(
'offset' => 2
'limit' => 1
)
);
This will work for you, Here you just need to pass the limit..
<?php
$map=$this->Sessiondetails->find("all",array('limit'=>'2,1'););
$this->set("map",$map);
foreach($map as $maps)
{
echo $maps['Sessiondetails']['latitude'];
}
?>

How to implement pagination with search in CakePHP

My pagination is working, even the search is also working. But the problem that I'm having is when i click on next page link in the pagination links. The search is not working for the next page of the pagination. Also I need to know how I send other parameters through the url, and use them in query of pagination. I need help on this as I am novice in CakePHP.
In controller page I have used this code:
class StatesController extends AppController {
public $components = array('Paginator');
public $paginate = array(
'limit' => 2,
'fields' => array('State.id', 'State.state','State.code'),
'order' => array(
'State.state' => 'asc'
)
);
public function admin_index() {
$this->layout = false;
$this->layout = 'adminlayout';
//****** pagination starts
$search=$this->request->data('State.search');
$this->Paginator->settings = $this->paginate;
// similar to findAll(), but fetches paged results
$stateListAr = $this->Paginator->paginate('State',
array('State.state LIKE' => "%".$search."%")
);
$this->set('stateListAr', $stateListAr);
//****** pagination ends
$this->set('stateListAr',$stateListAr);
$this->render('admin_index');
}
}
In view page I have used this code:
<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->numbers(array('first' => 'First page')); ?>
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
i have got solution to my problem , here it is . I have used a code in view page , here $search variable i have setted data from controller.
$search);
$this->Paginator->options(array(
'url' => $urlParamAr
));
echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled'));
echo $this->Paginator->numbers(array('first' => 'First page'));
echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
?>

virtual field are not working in cakephp

my controller .
$times=$this->Time->find('list',array('fields'=>$this->Time->virtualFields['name_price'] ));
$this->set('time',compact($times));
and i added below line in my model Time
public $virtualFields = array('name_price' => 'concat(Time.varaddress1, "-", Time.varaddress2)');
and my ctp file is below
<?echo $this->Form->input('intaddressid', array(
'options' => $time,'label'=>false,'empty' => '(Select Information)'
,'class' => 'form-control border_none'
));?>
but now in output i got nothing in select filed.!!
and when i echo $time i got `Array ( )
and i want in select field
<option>varaddress1,varaddress2</option>
You need to change your controller code like as :
$times=$this->Time->find('list',array('fields'=>array('Time.id','Time.name_price')));
$this->set(compact('times'));

Ajax-pagination triggers only once

i implemented the pagination-components according to the official documentation:
http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html
I also included ajax-support. Basically everthing works fine except one thing: If i switch from page 1 to page 2 the ajax-request will work, after that i switch from page 2 to 3 instead a ajax-request a normal Post-Request will be performed. So the ajax-thing works only once.
My controller looks like this (short form):
public $components = array('Paginator', 'RequestHandler');
public $helpers = array('Js', 'Categorie');
....
$this->Paginator->settings = array(
'limit' => '15'
);
No the more important thing is my view: (jquery are included in my layout...)
$this->Js->JqueryEngine->jQueryObject = 'jQuery';
$this->Paginator->options(array(
'update' => '#paginate',
'evalScripts' => true,
'before' => $this->Js->get('#busy-indicator')->effect('fadeIn', array('buffer' => false)),
'complete' => $this->Js->get('#busy-indicator')->effect('fadeOut', array('buffer' => false))
));
?>
<?php
echo $this->Html->image('indicator.gif', array(
'id' => 'busy-indicator'
));
?>
<div id="paginate">...display data...</div>
<?php
echo $this->Paginator->numbers();
echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled'));
echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
echo $this->Paginator->counter();
?>
That's all... as mentioned before: Switch Page 1->2 will work, Switch Page 2-3 will do a normal POST-Request, Switch 3-4 will work and so one...
A bit strange, so any idea? Btw. i tried different jquery-versions without any effect (currently i am using 2.0.3...)
Thanks - Best regards
Teyhouse
In this case, you just needed to also have:
<?php echo $this->Js->writeBuffer(); ?>
In your app/View/Layouts/ajax.ctp file.

CakePHP treated comments with pagination

Is there a way to paginate comments in cakePHP with a tree behavior? Should I use tree behavior anyway or to make my own code to view the comments?
Sorry, this has been asked before. I found an article: Managing Hierarchical Data in MySQL
...and I will post my solution.
Nope, I didn't find a good solution. I don't want to reinvent the wheel, I do want to make it in the cake way.
I wrote a helper for recursively print the comments:
# view/helpers/comments
class CommentsHelper extends AppHelper{
public function printComments($comments = array(), $params = array()){
if (empty($comments) || !is_array($comments)) return false;
echo '<ul id="comments-'.$comments[0]['Forum']['id'].'">';
if (is_array($comments)){
foreach($comments as $comment):
?>
<li id="<?php echo $comment['Forum']['id']; ?>">
<div class="inner">
<h4><?php echo $comment['Forum']['title']; ?></h4>
<div class="meta">
<?php echo $comment['User']['first_name']; ?>
</div>
<div class="content">
<?php echo $comment['Forum']['content']; ?>
</div>
</div>
<?php
if (isset ($comment['children']))
if (is_array($comment['children'])){
if (!empty($comment['children'])) $this->printComments($comment['children']);
}
echo '</li>';
endforeach;
}
else{
echo '<li>'.$comment['Forum']['title'].'</li>';
}
echo '</ul>';
}
Here is how I retrieve the data from the database:
# controllers/forums.php
$this->set('tree', $this->Forum->find('threaded'););
The problem is how to paginate the comments?
Currently I'm using 2 tables, one for the root and 2nd for the threaded comments. I didn't find a solution.
You have to do it manually in controller by paginate.
The key is properly order mysql query to find comment not by id by firstly by parent_id and then id (columns are just example) - that would work for 2 level comments:
$this->paginate = array(
'recursive' => -1,
'conditions' => array(
'Comment.blog_id' => 0,
),
'order' => array('parent_id', 'id'),
'limit' => 10
);
$this->set('comments', $this->paginate('Comment'));
You can't. What I did is a link which switched between full threaded comments view AND flat paginated list.
IMHO: do you think that paginate comments is a good idea? just minimize space comments take on the page, and put them into one float. don't make your user to click too many links...
You can achieve this as
Your modal code:
var $hasMany = array(
'Pages' => array(
'className' => 'Page',
'foreignKey' => 'parent_id',
)
);
Your Controller code:
$this->paginate = array(
'Page' => array(
'contain' => array(
'Pages'
),
'conditions' => array(
'Page.parent_id' => null
)
)
);
$pages = $this->paginate();
You should use tree behavior and order by lft.

Resources