Random Order in a Paged Query

I have a paged query in which I set the formatter to .randomize on the client side at the repeater.

Problem is it only picks/randomizes the same first three records. I would like the first three records (page) to pull random records from the whole table. It’s only randomizing the first page. Each other page is always the same.

You can see it here if that helps.

I’m seeing a duplication of files NOT FOUND

https://koparadmin.ca/dmxConnect/api/success/successlist.php?limit=3 is the result of your randomizer?

So are you trying to show 3 random records on the first page only or show 3 random records on each page or?

Right now there is seven records. There will be more. But I need those seven records being in random order. but show three on a page.

Maybe I have to do this server side and randomize the records before it gets to the client side?

You have two options:

  1. Make a database view which returns the records in random order and then use it with a paged query
  2. Use a regular query and the data view component for the paging on the front end. You can randomize it there.

I’d personally prefer 1 :slight_smile:

1 Like

So would I :wink:

I can make a view but how do I randomize the order? Is there a way in Wappler or do I have to custom code it?

Use mysql rand for your view :slight_smile:
Example:

CREATE VIEW `test`.`my_rand_view` AS
SELECT * FROM mytable
ORDER BY rand()

Great. Thank you so much. That looks simple enough. :slight_smile:

Hmmmm, I can create a view in Navicat but it is not available to Wappler for some reason. And I can’t create the view in Wappler either. There is an error.

Brad, you cannot create views in Wappler.
You can just refresh schema on database level and on views level to see the newly created view in your database.

Yeah, it’s not there though. See screenshot above. Other connections on that project are showing views no problem. Just on this connection there is an error.

Got it … needed Direct Connection set. All good now!

Thank you once again for your time and help. :beers:

1 Like

If you use a view with a random order then you shouldn’t use it for paging, the results are random with each select so the next page doesn’t have the same order as the previous.

2 Likes

I see that Patrick. So there is no way to have paging with random order?

Using client-side paging or if you have a way that it only randomizes the query per client session, so that the order is the same for the connected client when he queries it again to get the next page.

1 Like

Isn't that what the pagination generator is?

No, for client side paging you use a regular query and display data using the data view component. You define there the records per page etc.
Then you create paging based on it (data view).

1 Like

Thanks Teodor,

I will explore that option. Might be above my skills level. Might have to come up with another way to display the data.

It’s the same as using paged query, just use the data view as a source for the paging and repeat :slight_smile:

1 Like

So I would

  • create a regular query and use the formatter to order the records in random order?
  • Add a data view to the page and set the number of records to show?
  • Use the pagination generator as usual but use the data view as the source?

I will explore further. Maybe I will learn something useful today. :slight_smile: