How to get all records when using pagination - cakephp

Let's say I set a limit of 10 per page on a set of records retrieved with $results = $this->paginate('ModelName'). The total number of records matching the paginate/find parameter is 30, so 10 records show up on each of the pages. When I look at $results, it contains only 10 records displayed on the page.
Is there a way to get all 30 records in the view without having to separately do something like $allResults = find('all', $params) followed by $this->set('everything', $allResults)?

I ended up passing some of the pagination parameters that can be used to retrieved all of the 30 records from the view via a form to an action.
So, the user clicks on a form on the view that submits hidden variables to an action, where the whole set of 30 records are retrieved and desired operation is performed. Then, the page is redirected to the previously paginated view.

Related

Sending multiple productID with URL (client-side)

React-SPA (client-side-routing)
I have this scenerio .I have a table with many products. And a table have an ID column. When user select multiple ID for example : 023653,023654,023655,023656 (basically they can select as many as they want )
The product URL is : localhost:3000/products
When the user selects multiple products and they trigger the button for example: calculate products .
The user will endup in this URL : localhost:3000/products/023653&023654&023655&...
As you can image if user selects more than 10 items the url would be awfully long.
The reason I want to do it this way is I want the userA to be able to send their selected page to the userB for example (localhost:3000/products/023653&023654&023655&). And once the user B open this page they will see everything the userA can see. What is the ideal way to solve it ?
Thank you in advanced :)

smart-table - custom pagination

I am using smart-table plugin for pagination.
My requirement is- i am fetching 100 records from database while loading and records per page is 10.
So, number of pages would be 10 in pagination.
Now, i want when i click on the page number 10 (from pagination) then i want to fetch the another 100 records from database & then those new 100 records will append in the table ( total records in table would be 200 and page number would be 1 to 20 in the pagination) and same for when i click on page number 20, another 100 records fetch from database and so on.
What I understood from your question is that you want the table rows to stack on one another instead of being replaced when you navigate on the paginator.
First of all, you don't need to use the default pagination, you can put your own custom markup in the <tfoot> tags
Second, if you want to show different amounts of rows on click, you could use the limitTo filter on the ng-repeat
Here's a plunk showing how I've added buttons that change the amount of rows being displayed on the table. you can use this logic (and functions) on your own template, just fetch the page number you going for, multiply by 10, and set that number as the amount you want to see

Paging with a data set that can be changing?

I'm sure there is something out there about this topic but I just can't figure out how to word a search for it.
I have a table of records that gets loaded into a paging grid in the UI. The user has the ability to update/modify these records..also multiple users can use the system at once all hitting the same data. I have a filter on the paging grid allowing the user to see only X type of records.
When the user first enters with filter X selected they see items 1-25 on page 1 of 2. They page to the second page where the items should be 26-50..but before they paged lets say 25 records on the first page had their type changed by another user, now they don't appear when selecting that filter. So now we have 25 less items to page through which means items that were 26-50 before are now items 1-25 and what was page 2 is now page 1 and there is no page 2...
You can probably see the issue I'm getting into, I'm passing an offset to the query to get the next page of results..but now that offset is so high it returns a blank page of records confusing the user and our record processing.
There isn't really an easy solution to this problem. Even GMail/Google doesn't show the exact number of messages/pages found when searching something.
The first thing you can do (if you use a DataGrid/CellTable) is set the boolean exact as false when you call updateRowCount, and give it your current number of records instead of your total number of records. This will make the pager display "1 - 25 of over 25" instead of "1 - 25 of 50".
The next possibility is to update the row count regularly (using RPC polling to check for new/deleted records - or using server push techniques, see GWTEventService and ServerPushFAQ).
You can also check if your request returns items or not, and cancel the call/update the row count if it doesn't.

managing View Count of webpage

I am creating a jsp page where a single news item is shown, just like the single question on this StackOverflow page. I need to store the view count in a database.
I can simply increment the view count in the database this way:
`UPDATE table SET views=views+1 WHERE newsId=4458;`
But what if a single user hits that page again and again and again, say 50 times... In this case, my view count will not represent unique visitors to the site. How can I avoid this?
I want something like StackOverflow's view count system.
well one solution could be you make a new table (lets say its called views) with the following columns 'viewid' 'newsid' 'ipaddress' (obviously newsid is the foreign key)
every time someone visits ur website u check if his ip is already in that table for that news item.
Select count(*) AS count FROM views WHERE newsid=1234 AND ipaddress=123.456.125
if count equals 0 you insert like this
INSERT INTO views('newsid', 'ipaddress') VALUES(1234, 123.146.125)
Now when you want to fetch the viewcount:
Select count(*) AS count FROM views WHERE newsid=1234
you can enhance this by adding another column called 'count' and 'lastviewed' (type datetime) to the table views.
Lets say you allow every ip to be counted another time if the day doesnt match.
This way you will get a pretty accurate viewcount :)
to fetch the new viewcount you can simply use SUM
Select SUM(count) AS count WHERE newsid=1234
Good luck :)
You may want to reference spencer7593's answer here, where he basically suggests having both (1) a view count column, for quickly generating your webpages and (2) a separate views table, which records the user id and timestamp pertaining to each view, for the sake of analytics.
I would suggest that this views table could also be used to query if a viewing has occurred for a particular user with a particular piece of content in the last day (or week) when determining if the view count column should be incremented. By checking if a view of a certain item has occurred only in the last day (or week), the DB queries should be substantially less expensive while still protecting the integrity of your view count.
i say use a counter in Javscript . Increment the variable on body onclick. eg:
<script language=”javascript”>
function pageCounter()
{
var counter = 0;
counter = counter++;
}
</script>
<body onclick = “pageCounter()”>
and you can update the value with onclose ..

How do I store this in Redis?

I have many products (product_id). Users (user_id) view the products.
I want to query which users viewed whatever product in the last 24 hours. (In other words, I want to keep a list of user_ids attached to that product_id...and when 24 hours is up for a user, that user pops off that list and the record disappears)
How do I store this in Redis? Can someone give me a high-level schema because I'm new in Redis.
For something similar I use a sorted set with values being user ids and score being the current time. When updating the set, remove older items with ZREMRANGEBYSCORE as well as updating the time score for the current user.
Update with code:
Whenever a new item is added:
ZREMRANGEBYSCORE recentitems 0 [DateTime.Now.AddMinutes(-10).Ticks]
ZADD recentitems [DateTime.Now.Ticks] [item.id]
To get the ids of items added in the last 10 minutes:
ZREVRANGEBYSCORE recentitems [DateTime.Now.Ticks] [DateTime.Now.AddMinutes(-10).Ticks]
Note that you could also use
ZREVRANGE recentitems 0 -1
if you don't mind that the set could include older items if nothing has been added recently.
That gets you a list of item ids. You then use GET/MGET/HGET/HMGET as appropriate to retrieve the actual items for display.
If you want redis keys to drop off automatically then you'll probably want to use a redis key for every user_id-to-product_id map. So, you would write by doing something like redis.set "user-to-products:user_id:product_id", timestamp followed by redis.expire "user-to-products:user_id:product_id" 86400 (24hrs, in seconds).
To retrieve the current list you should be able to do redis.keys "user-to-products:user_id:*"

Resources