print no of items fetched using pagination in cakephp 3 - cakephp

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

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.

Strange behavior of ng-repeat filter

I am using following ng-repeat statement
ng-repeat="instrument in Instruments | filter : (!systemTypeCatgory?'': {CategoryTypeId : systemTypeCatgory})"
There is a dropdown in which systemTypeCatgory is selected and based on that selection next dropdown is populated using above ng-repeat statement.
Everythinng was working fine till I had CategoryTypeId from 1 to 9 in the database. Recently new category is added with CategoryTypeId = 10 and now whenever I selected category with Id = 1 in the first dropdown, along with items from category=1 it includes items from ategory=10 also in the second dropdown.
But the vice-versa is not happening, meaning when I select category = 10 it shows items from category = 10 only.
It is only failing when I select category = 1.
Not sure, somewhere if it is comparing 1-1 in the start or what. Somewhere it is considering 1=10 and so the items are coming from both the categories
I have tried printing the values of CategoryTypeId and systemTypeCatgory ,the code is picking them correctly.
Not getting why it is failing, any clue?
Edit
Tried converting everything to string and tried this harcoding also just to check
ng-repeat="aiisInstrument in AiisInstruments | filter : { CategoryTypeId : '1'}"
It is still selecting category=10 items!! Super strange behavior :(

Retrieve specific index from arraycollection

I can't seem to work this out. I want to display a list that starts with a specific position in an arraycollection, without changing the array. The goal is to have a set of full screen images that the user can swipe through (like in an ebook), and for the chapter index i want to start with a specific indexnumber in the arraycollection.
Is there a simple way to tell the Itemrenderer to start at a specific indexnumber in the arraycollection.
My code that starts at index 0:
<s:List id="pagedList"
width="100%" height="100%"
verticalScrollPolicy="off" horizontalScrollPolicy="on"
pageScrollingEnabled="true">
<s:layout>
<s:TileLayout orientation="rows" requestedRowCount="1"
columnWidth="{pagedList.width}" rowHeight="{pagedList.height}"
verticalGap="0" horizontalGap="0"/>
</s:layout>
<s:ArrayCollection id="contentData">
<s:Image source="assets/cover.jpg"/>
<s:Image source="assets/introduction.jpg"/>
<s:Image source="assets/H1/40_days.jpg"/>
</s:ArrayCollection>
<s:itemRenderer>
<fx:Component>
<s:ItemRenderer>
<s:Group>
<s:Image source="{data.source}" horizontalAlign="center"/>
</s:Group>
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
AFAIK there is no way to do this. You'll have to sort the dataprovider so that it matches the order in which you want to display the items.
If you don't want to modify the original collection, you can create a ListCollectionView, pass in the original collection to the constructor and add a sort to the ListCollectionView. You can then assign the collection view as the dataprovider of your list.
It all starts with your data. The data itself will need to contain info about a chapter offset. Typically you will store a collection of the offset seek points.
For example:
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Chapters: 1 2 3 4 5
A chapter object will hold a reference to which page to start from and its total length. A set of seek buttons will iterate through a chapter collection, telling your list to start from the appropriate offset (ie chapter 2, starts on index 4 and is 4 items long).
Your page objects can either be a single image, or a collection of images per page. In either case, what you're really building from a data point of view is a hierarchical collection.
if your goal is to show a particular image first why not set the selectedIndex for the List itself. I mean give u'r dataobjects in the dataprovider some id and pass on the index to the list.
hope this help, let me know if this is not what is intended
Might the ensureIndexIsVisible method on the Spark List work?
Note: You may need to click the "Products" checkbox at the top of the following html page to actually see this method.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/spark/components/List.html#ensureIndexIsVisible()

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!

Cakephp LImiting Pagination Counter

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.

Resources