Cakephp LImiting Pagination Counter - cakephp

I want to limit the counter that is visible in pagination in cakephp? What should I do...? Currently its default showing as 1-9 as pages link. I tried limit but as it limits my post per page. I want to limit the counter not post per page. Please help me.
Modified...
I tried modulus according to answers..
I want my Pagination should be this way. If it is on first Page then.
1 2 3 ... Last
If it is on Middle of page then..
First ... 6 7 8 ... Last
If it it on last then.
First ... 11 12 13
My tried code is.
<?php echo $this->Paginator->numbers(array('modulus' => '2', 'tag' => 'span','first'=>'First','ellipsis'=>'...', 'separator' => ' ', 'last'=>'Last' )); ?>

Check out the documentation for the pagination helper: http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html
I think want you want is "modulus - how many numbers to include on either side of the current page, defaults to 8."
So in your View, you'd have something like:
// Limit numbers to 4 either side of current page
echo $this->Paginator->numbers(array('modulus' => 4));
Hope that helps.

Related

Getting unexpected result if using pagination on random find query in cakephp 4?

I am trying to find all products in random order using pagination. But getting an unexpected result.
This is my controller code:
$this->paginate = ['maxLimit' => 20];
$articlesData = $this->Products->find('all');
$articlesData->order('RAND()');
$articles = $this->paginate($articlesData);
So I get random result every time, this is the demand of the page. I have 200 products. Suppose first item is item-12 on page one then if I go to the next page and again go to the previous page then the first item should be item-12 but it changed randomly. Is there any solution for this?
I need pagination because products number going to be 1000 soon. And need random results because I don't want that user to see the same first product on the first page. The first product should be on the first page but not on the same location every time. Is there any way that I make paginated array of 20 items in random order?
All suggestions are welcomed.
Example of how to rearrange results in view template. In the controller select from the latest Articles, but in the template display these 20 results in a different place each time.
#Controller
$this->paginate = ['maxLimit' => 20];
$articlesData = $this->Products->find();
$articlesData->orderDESC('creates');
$articles = $this->paginate($articlesData);
$this->set(compact('articles'));
#template
foreach(shuffle($articles->toArray() as $article) {
echo $article['name'];
}
https://www.w3schools.com/php/func_array_shuffle.asp
When you order them randomly, they are ordered randomly. Every page load will be a new random order. See this for an example of how to make the order predictable; you might seed the generator with the user ID for example, then the order will always be the same for a given user. Or with some combination of user details and the date to give them a different random order tomorrow. Or randomly generate a seed and then save that in the session to control the duration more precisely.

print no of items fetched using pagination in cakephp 3

I want to display count of no of items fetched per page using pagination like
Displaying 10 items
Although limit is set to pagination to 20 but It will not always fetch 20 items when there are only 15 items or 10 items or 25 items.
In these cases it will either have one page result for items count 15 and 10 or two pages result for items count 25.
I want to print the no of items showing on each page. Like for total items 25.
It will show Displaying 20 items on first page and Displaying 5 items on second page.
How is it possible in CakePHP 3.2 ?
This is how I get it working in CakePHP 3.2
There are two ways you can do it.
Using Helpers
create a file paginator-counter.php in /config directory with your tokens and string and add a line in /src/view/AppView.php
public function initialize()
{
$this->loadHelper('Paginator', ['templates' => 'paginator-counter']);
}
This will load the file located at config/paginator-templates.php
And then use it in your view.
For more : http://book.cakephp.org/3.0/en/views/helpers/paginator.html
Using direct string
Just print in view where you want to print the counter
<?= $this->Paginator->counter('Showing {{current}} results out of {{count}}') ?>
More tokens can be found Here : http://book.cakephp.org/3.0/en/views/helpers/paginator.html#creating-a-page-counter
I'm using 2nd way to print counter ie., using sting directly

What is the difference between the way data in input and hidden form types are handled?

Finally, a simple, straight-answer question :D
Background: My company has changed the way that that they handle when certain sites are used. Previously, certain sites were only delivered to on certain weeks; now, every site delivers every week. They have asked me to get rid of the weeks field on their "add new site" form. The link between week and site is still necessary for the code to work, however, so I am trying to hide the field and populate it with every single week.
Auto-populating it is simple. I just do this:
echo $this->Form->input('Week', array('value'=>array('1','2','3','4','5')));
And when I debug the data getting sent from the form, I get the following, which is exactly the way I want it to work.
'Week' => array(
'Week' => array(
(int) 0 => '1',
(int) 1 => '2',
(int) 2 => '3',
(int) 3 => '4',
(int) 4 => '5'
)
So the next step is simply hide that input. Easy, right? I just do one of the following two things to the form:
echo $this->Form->hidden('Week', array('value'=>array('1','2','3','4','5')));
or
echo $this->Form->input('Week', array('value'=>array('1','2','3','4','5'), 'type'=>'hidden'));
All I have done is change the type to hidden. But now, the data returned from debugging the data from the form looks like this:
'Week' => array(
'Week' => '1 2 3 4 5'
So my question is, what is the difference in the way data is handled between a normal "input" field and a "hidden" field. Why does that difference occur, and why is it important? And for this particular issue, how do I get hidden data to behave in the same way as normal input data?
As you said in the comments, Week is a multiple choice input, so that's why the difference in value handling. If the input is something that allows multiple choice (like multiple select or checkboxes), then it's logical all values comes as an array. But the hidden type is basically a hidden text input (ok, maybe not, but I'm simplifying). Look, if you put
echo $this->Form->input('Week', array('type'=>'text', 'value'=>array('1','2','3','4','5')));
you'll get the same string as you have with the input field, so it isn't a matter of if it's hidden or not.
Now, for your case, you can add the week values as an string and change the way you handle the value on post, as a string instead of an array.
But, if by some reason that's not a possibility (too much code to change, etc), then keep the same code you have now, and hide it with css. Something like
echo $this->Form->input('Week', array('value'=>array('1','2','3','4','5'),
'div'=>array('style'=>'display:none')));
should do the trick (maybe a few more styles needs to be added, try with just that first).
I don't like the css solution that much, browsers can interpret css different ways and etc etc... But it fits your needs and you don't have to change anything else.
EDIT
On second though, it is possible to achieve the same array output with just hidden fields. It isn't pretty though. You have to change the name of every input to tell php it's an array, like this
echo $this->Form->input('Week1', array('type'=>'hidden', 'value'=>1, 'name'=>'data[Week][Week][]'));
echo $this->Form->input('Week2', array('type'=>'hidden', 'value'=>3, 'name'=>'data[Week][Week][]'));
echo $this->Form->input('Week3', array('type'=>'hidden', 'value'=>4, 'name'=>'data[Week][Week][]'));
The important part is the name option for every input, see the array reference? Specially the last empty []. This is actually how cakephp sends all data as an array. Have you tried to create a form without the FormHelper in cake? You wouldn't get the post data in $this->request->data['form_name'] unless you set the input names for all your form fields.
Cake sends all post data inside the $data array by naming all the inputs with <input name="data[ModelName][field_name]" and for multiple inputs is <input name="data[ModelName][field_name][]" (with the empty array at the end, so it gets numerically indexed). You can see that using any html inspector on any other form created with the formhelper in cake.
I hope it's clear... I'll stick with the css solution here. The multiple hidden inputs seems messier.

CakePHP1.3: incrementing numbers in a paginated view

I'm displaying a search result obtained via $this->paginate() for the view. Pagination works well, and I want to display the row number of the search result for each row on the view for the user. I simply do <?php echo $i; ?>, where $i is my counter, but on every page, the number starts from 1. So, if I'm on page one of the search result, it will show 1 ~ 10 (10 rows of data per page). I move to page two, and it will still show 1 ~ 10, and so on.
How can I make this so that it shows 1~10 on page one, 11~20 on page two, etc. on the fly? And why does it reset, even though I'm going through my search result array elements one by one and doing $i++ in every iteration?
You want to use counter() as part of the Paginator helper.
$start = $this->Paginator->counter(array('format' => '%start%'));
foreach( $records as $record ) {
$start++;
...
}
See if that helps!

Pagination by conditions in cakephp

i am having a doubt in using pagination like thing in my application..
where mytable looks like
Entry Firstname Last Name Residential Address Degree Gender Submitter
1 Aruna Chinnamuthu MDu BE Female aruna#gmail.com
2 Nisha Durgaeni MS BE Female nisha#gmail.com
3 Nathiya N Mumbai BE Female nathiya#gmail.com
In my view i am trying to bring the First entry at first and on clicking the next link in the View it must show the Entries for the second submitter and goes on ..
I have seen the pagination concepts in book.cakephp.org. But in all those they have mentioned only to paginate by using limit and order and not by using conditions.. how to do so..
It is possible ??
My submitters is in the array as
function view($formid){
$submitters=$this->Result->find('all',array('conditions'=>array('Result.form_id'=>$formid),'group'=>array('Result.submitter_id')));
foreach($submitters as $submitter)
{
echo $submitter['Result']['submitter_id'];
}
}
That is i am trying to paginate by SUbmitters of my form .. Is it possible in cakephp??Please suggest me..
I think you might be looking to use the Containable behavior. That will let you specify conditions that will apply to the model's data retrievals.
I can't quite discern the rest of your code, as the formatting didn't make it from the OP very well, but I think your conditions are limited to form_id=$formid, right? I'd suggest (before that line) dynamically attaching a Containable behavior that limits the forms to just $formid.
I'm not 100% that this is what you're after, but I might be understanding the question incorrectly.
I am unsure of the CakePHP version that you are using, but in CakePHP 1.2, the Paginate method takes a second parameter. This is the conditions array:
$this->paginate('Result', array( 'id' => 1, 'someOtherCondition' => 'condition' ) );

Resources