CodeCharge Studio
search Register Login  

Visual Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 Random SQL Function

Print topic Send  topic

Author Message
webtotaltoday

Posts: 46
Posted: 05/01/2008, 1:57 AM

Hi,

I am using the order by: RAND() function which works great but i have 500 records, over about 20 pages, 50 per page, but i find that when i go on to the next page it uses the rand again so that i can get the same results as the first page.

This means that over the 20 pages, maybe only 300 records will show as some will show on more than one page

Is there a way of doing this so that rand will not start on each page request?
View profile  Send private message
maxhugen

Posts: 272
Posted: 05/02/2008, 11:12 PM

I do the same thing myself - deliberately bringing back records in random order - but I hadn't thought of the implications you point out!

I'm really guessing here... could you dump a randomised query to a temp table first, and then get the records from this temp table? I guess it would need its own counter-type field, so they maintain their random order.

I'm thinking that when you first call a page, you run this 'pre-query' which loads the temp table, and then the normal query method kicks in.

When the second page is requested, the url will have a parameter in it, something like:

Client.php?ClientListPage=2

If the php finds this param, it would NOT run the pre-query, and just continue returning records from the temp table.

What do you think?
_________________
Max
www.gardenloco.com | www.eipdna.com | www.chrisarminson.com
View profile  Send private message
DonB
Posted: 05/03/2008, 1:30 PM

Add a column to the table and populate it with RAND() when inserted.

--
DonB



"maxhugen" <maxhugen@forum.codecharge> wrote in message
news:5481c02594b02e@news.codecharge.com...
>I do the same thing myself - deliberately bringing back records in random
>order
> - but I hadn't thought of the implications you point out!
>
> I'm really guessing here... could you dump a randomised query to a temp
> table
> first, and then get the records from this temp table? I guess it would
> need its
> own counter-type field, so they maintain their random order.
>
> I'm thinking that when you first call a page, you run this 'pre-query'
> which
> loads the temp table, and then the normal query method kicks in.
>
> When the second page is requested, the url will have a parameter in it,
> something like:
>
> Client.php?ClientListPage=2
>
> If the php finds this param, it would NOT run the pre-query, and just
> continue
> returning records from the temp table.
>
> What do you think?
> _________________
> Max (99.3% CCS Newbie)
> "I have a dream..." that CCS provided a php debugger.
> ---------------------------------------
> Sent from YesSoftware forum
> http://forums.yessoftware.com/
>

wkempees


Posts: 1679
Posted: 05/04/2008, 4:48 AM

@DonB
played around with the above, and yours looks like very viable to me.
Any other is mangled because of the paging mechanism.

Walter
_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
maxhugen

Posts: 272
Posted: 05/04/2008, 10:46 PM

Don, won't this leave the ORDER in exactly the same 'random order' every time?
_________________
Max
www.gardenloco.com | www.eipdna.com | www.chrisarminson.com
View profile  Send private message
Benjamin Krajmalnik
Posted: 05/07/2008, 2:10 PM

I solve this in a different way, because in my case I have the same query
going to multiple objects onm the page, and I need them to be synchronized.
In my case I use PostgreSQL, so I have pgagent running in the background
every 15 minutes and reseeding a field with a random value, which I use as
the order by column.

Benjamin Krajmalnik
Posted: 05/07/2008, 2:12 PM

Yes it will. Look at my reply. I have a background scheduled task which
reseeds them.

maxhugen

Posts: 272
Posted: 05/07/2008, 5:10 PM

Benjamin, yes, I think your suggestion of 'reseeding' the randomised column that Don suggested at set intervals would be the most practical.

Cheers
_________________
Max
www.gardenloco.com | www.eipdna.com | www.chrisarminson.com
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.