5
I’m creating a site type images type Pinterest that, in addition to other things, need to combine the following features:
Infinite Scrool (I’m applying this code here: https://www.youtube.com/watch?v=_D-CPBvqQaU).
A menu to choose in which order to display images.
The problem happens when I choose to display the images randomly: they repeat themselves.
The query I’m using is as follows::
$return = $database->query("
SELECT
lk_post_pic.*,
tb_post.head,
tb_post.created_datetime,
tb_pic.album,
tb_pic.file,
tb_pic.thumbnail
FROM lk_post_pic
JOIN tb_post ON lk_post_pic.fk_post = tb_post.id_post
JOIN tb_pic ON lk_post_pic.fk_pic = tb_pic.id_pic
WHERE tb_pic.thumbnail = 'default'
ORDER BY RAND()
LIMIT 20
OFFSET $offset
")->fetchAll(PDO::FETCH_ASSOC);
Each time the scroll is scrolled to the bottom, this query is again executed and the $Return data is inserted into an html tamplate, through echo, which is received by the jQuery function as a GET sponse and then the append method is executed().
NOTE: I hope I’m not complicating with these ramblings, it’s just to contextualize, maybe I can help.
So, but each time this query is executed, the $offset is added in 20 units, to print the new images, but, as everything is randomized before, the first offset images end up mixing with the second and then the views are repeated.
How to get around problem?
I really liked the second alternative! It’s very simplified! But there’s a problem: The code you wrote always ranks first the last item on the list. For any prime value I choose for $value, the first line will always be 69. How to fix this? Something else: How do I import this list primes numbers pro Mysql?
– Jose Henrique
About importing cousins, or you can make a table of primes and draw one, or you can just use the primes in PHP code, then it’s kind of hard to tell which is the right way
– Bacco
I updated, the module value is quantity + 1, not quantity.
– Bacco
Interesting! But, I have another question regarding the following: "Since $value must be a prime number greater than $quantity_records" What if, after applying some filters in the table in question, the amount of records is different MAX(id_post)? At first, MAX(id_post) = total amount of records, but after applying some joins and aluns wheres, I can have a table with 500 records and MAX(id_post) equal to only 50. Or, MAX(id_post) = 200 and total number of records = 16. In this situation, how to correctly interpret and apply your formula?
– Jose Henrique
If you pick the first about 6500 primes, you will have numbers up to 65000 + -, just draw from the first higher than Count(*) to the last
– Bacco
Note that the 2nd solution is worth for very large quantities of images, if it is to make a pagination with 5 selects, probably the idea of cousins does not justify.
– Bacco
Is it important that the number of records is equal to the number of rows in the table I am looking at? My queries are super complicated, involve up to Queries, make this account as parameter and then do it again in the sub and again in the main will consurmir enough server resource...
– Jose Henrique
Jhenry saw that you accepted my answer, I suggest a good look at the solution of colleague @Pagotti, maybe in your case it will be simpler to implement. If it works well for you, it is even lighter for the server. (But use a higher value of Rand(), 1000 is little. see my comment in his post).
– Bacco