Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Databasing
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 07-21-2009, 11:31 AM
  #1
erme
Adept (Level 5)
 
erme's Avatar
 
Join Date: Oct 2005
Location: Devon, UK
Posts: 42
iTrader: (0)
erme is an unknown quantity at this point
Selecting random records and holding for pagination

Hi,

I'm building a business directory. When a user searches for a business I want the results to display randomly, as it would be unfair to order by date or alphabetically.

This is simple enough. Except I need to be able to save the random selection for pagination. This way there is no repeat of records and the user can view all the records available.

Is this possible? And if so, can anyone help with some strategies?

Thanks.
erme is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 07-21-2009, 11:38 AM
  #2
Vege
master of bloody obvious
 
Vege's Avatar
 
Join Date: Sep 2004
Location: Finland
Posts: 3,546
iTrader: (0)
Vege is just really niceVege is just really niceVege is just really niceVege is just really niceVege is just really nice
Quote:
Originally Posted by erme View Post
Hi,

I'm building a business directory. When a user searches for a business I want the results to display randomly, as it would be unfair to order by date or alphabetically.

This is simple enough. Except I need to be able to save the random selection for pagination. This way there is no repeat of records and the user can view all the records available.

Is this possible? And if so, can anyone help with some strategies?

Thanks.
What database are you using?
What SSL?
If using MySQL i would say there is nothing easy about random
__________________
http://setala.info
Vege is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 07-21-2009, 12:09 PM
  #3
erme
Adept (Level 5)
 
erme's Avatar
 
Join Date: Oct 2005
Location: Devon, UK
Posts: 42
iTrader: (0)
erme is an unknown quantity at this point
Sorry, MySQL and PHP. Would it be a lot of work to get it right?
erme is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 07-21-2009, 01:23 PM
  #4
Mandarin
Lord (Level 16)
 
Mandarin's Avatar
 
Join Date: Jul 2009
Posts: 518
iTrader: (0)
Mandarin is on a distinguished road
I assume you're doing something like this right now:
Code:
SELECT * FROM tbl_name ORDER BY RAND() LIMIT 0,10;
According to the MySQL documentation, the RAND() function can take an integer "as the seed value, which produces a repeatable sequence of column values." I tested this functionality to see if you could generate a random integer in PHP, store the unique value in a session variable for each user, and use that to reseed each query as you populate each results page. It seems to work fine. Sample queries for the first three results pages might look something like this:
Code:
SELECT * FROM tbl_name ORDER BY RAND(1621423) LIMIT 0,10;
SELECT * FROM tbl_name ORDER BY RAND(1621423) LIMIT 10,10;
SELECT * FROM tbl_name ORDER BY RAND(1621423) LIMIT 20,10;
Mandarin is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 07-22-2009, 07:56 AM
  #5
scoutt
Mister Admin to you
 
scoutt's Avatar
 
Join Date: Jul 2001
Posts: 30,864
iTrader: (0)
scoutt is a jewel in the roughscoutt is a jewel in the roughscoutt is a jewel in the rough
I don't think you can get around not showing the same business on other pages. if by random it will not remember what was on the last page. but it is the best you can get I believe
__________________
Have a Script or Snippet you want to share?

WWW Standards: HTML 4.01, CSS2.1, CSS3, XHTML 1.0
PHP Standards: PHP Standards
scoutt is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 07-22-2009, 12:07 PM
  #6
paul_norman_81
Guru (Moderator)
 
paul_norman_81's Avatar
 
Join Date: Nov 2005
Location: Preston, England, UK
Posts: 1,044
iTrader: (0)
paul_norman_81 is a jewel in the roughpaul_norman_81 is a jewel in the roughpaul_norman_81 is a jewel in the rough
Pretty easy I would have thought, and just as Mandarin has said... for each user you generate a random seed once and store it in a session variable ($_SESSION['seed'] = time(); perhaps, remembering to start your session on each page of course). You then pass this different seed to your mysql query. The results are then 'random' for each user, but you can paginate through them.
__________________
"Aut viam inveniam aut faciam" - I'll either find a way or make one

First Tutors | First Tutors: Music | First Tutors: I.T. | First Tutors: Languages | First Tutors: Business | First Tutors: Arts & Crafts | First Tutors: New Zealand
paul_norman_81 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 07-23-2009, 02:05 AM
  #7
Vege
master of bloody obvious
 
Vege's Avatar
 
Join Date: Sep 2004
Location: Finland
Posts: 3,546
iTrader: (0)
Vege is just really niceVege is just really niceVege is just really niceVege is just really niceVege is just really nice
As far as i know order by rand() even with limit will fetch all data from a table to a hash table, then apply the limit.

So keep that in mind for tables with lots of data.
__________________
http://setala.info
Vege is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote

Reply
KEEP TABS
SPONSORS
 
Boxedart



 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 06:52 AM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.