TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-29-2008, 04:40 PM   #1 (permalink)
The Visitor
 
Join Date: Mar 2008
Posts: 3
Thanks: 0
Marvin is on a distinguished road
Default Friendlier alternative to rand()

Hey all, wondering if you could offer some advice to someone with very little experience!

I am displaying some pictures from different albums with the following:
PHP Code:
$lpictures $db->query("
  SELECT albumpicture.*, album.*, user.username, user.usergroupid,
  IF(displaygroupid=0, user.usergroupid, displaygroupid) AS displaygroupid 
  FROM " 
TABLE_PREFIX "albumpicture AS albumpicture
  LEFT JOIN " 
TABLE_PREFIX "album AS album ON(album.albumid = albumpicture.albumid)
  LEFT JOIN " 
TABLE_PREFIX "user AS user ON(user.userid = album.userid)
  WHERE album.state = 'public' 
  ORDER BY dateline DESC LIMIT 0, 
$displayrecords  
  "
); 
This displays the pictures in terms of date, the last x pictures uploaded to the gallery. I want to display a random selection of pictures.

I achieved this by changing
PHP Code:
ORDER BY dateline DESC LIMIT 0$displayrecords 
PHP Code:
ORDER BY rand() DESC LIMIT 0$displayrecords 
Using rand() seems to be frowned upon by developers, and a total dead-end when you try to use it on a large table. I clearly want anything to run as smoothly as possible, but don't know any alternatives besides rand() maybe you could help me out?

Thanks!
Marvin is offline  
Reply With Quote
Old 03-30-2008, 10:53 AM   #2 (permalink)
The Contributor
RegEx Guru 
 
Join Date: Dec 2007
Location: Belgium
Posts: 60
Thanks: 6
Geert is on a distinguished road
Default

You may want to read this article: ~jk ORDER BY RAND()

It gets rather complex, but if you look all the way at the bottom at the performance you win, it may be worthwhile.
__________________
Kohana - PHP5 framework
Geert is offline  
Reply With Quote
Old 03-30-2008, 12:49 PM   #3 (permalink)
The Visitor
 
Join Date: Mar 2008
Posts: 3
Thanks: 0
Marvin is on a distinguished road
Default

Thanks Geert, that has thrown me in at the deep end a little - but i'll have a close re-read and try and get to grips with it.

Anyone else feel free to share your input too.
Marvin is offline  
Reply With Quote
Reply



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

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:08 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design