|
|
|
|
|
|
![]() |
#1 |
Aw, Dad, you've done a lot of great things, but you're a very old man, and old people are useless
|
Random Selection through PHP
To start with, I am finally learning the right way to build sites using .php rather than the FP that us old lazy guys relied on. I am planning on completely redoing my sites from scratch.
Question is this. I am planning on setting up a php-file (moviegalleries.php) that will contain hundreds of movie galleries - in this case, asian content. I am planning on calling that php file into different areas of the asian site but only want to show 10 randomly selected galleries from the moviegalleries.php to be inserted at any one time. So, this would go into where I want the placement: <?php include("moviegalleries.php"); ?> To be perfectly honest, I have read through the PHP manual and W3C Schools and they seem to talk around what I want to do but do not seem to get there ... or perhaps it is simply an aging mind. How do I actually set up the moviegalleries.php file so it randomly selects the galleries? Thanks tons. Mav |
![]() |
![]() |
![]() |
#3 |
Aw, Dad, you've done a lot of great things, but you're a very old man, and old people are useless
|
I am planning on storing them in an array in the php file each line being html code with a <br> at the end of each line.
|
![]() |
![]() |
![]() |
#4 |
a.k.a. Sparky
Join Date: Sep 2004
Location: West Palm Beach, FL, USA
Posts: 2,396
|
Code:
<pre> <?php $links = array( "arrayitem1", "arrayitem2", "arrayitem3", "arrayitem4", "arrayitem5", "arrayitem6", "arrayitem7", "arrayitem8", "arrayitem9", "arrayitem10", "arrayitem11" ); $randomized_links=array_rand($links,3); print_r($randomized_links); foreach ($randomized_links as $linknumber) { print $links[$linknumber] . "\n"; } shuffle($links); print_r(array_slice($links,0,4)); ?> With SQL (and I really hate when this is done on a live site) you can do something like: Code:
select linkname,linkurl from tablename where category='teen' order by rand() limit 10;
__________________
SnapReplay.com a different way to share photos - iPhone & Android |
![]() |
![]() |
![]() |
#6 |
a.k.a. Sparky
Join Date: Sep 2004
Location: West Palm Beach, FL, USA
Posts: 2,396
|
It boils down to performance really. No matter what, when you do order by rand(), you force mysql to create a temporary file with all of the results and order it. If your query is well indexed, and you have 15 results, its not a big deal. order by rand() can never be optimized, so, can never take advantage of the mysql query cache. What's worse is getting 2300 results and doing order by rand() limit 10. Coupled with select * and a join and you've got the makings of half of the "professional" scripts out there.
select * is bad for a few reasons. First, you probably don't need every field returned, and if someone later alters that table to add a text field or other unneeded data for its current use, you are buffering it and retrieving that data regardless. If you structure your data in a particular way, you could actually never touch the mysql data file and get your results from the mysql index file. Second, when you do select *, from a code maintenance standpoint you don't immediately know what the variables are that are present in the table forcing you to keep a copy of the database schema handy. For small quick projects, these things seem like overkill, but, when you come back in 6 months to fix something or add a new feature, you'll end up saving time.
__________________
SnapReplay.com a different way to share photos - iPhone & Android |
![]() |
![]() |
![]() |
#7 |
I want to set the record straight - I thought the cop was a prostitute
Join Date: Jun 2008
Posts: 293
|
Would it not be more efficient however, to let MySQL run the function rand() instead of having it return your results only to let PHP perform a similar task?
I can see what you are saying (and select * is such a pussy way out, gets on my tits when I see it!) but the only alternative I can think of is to return all the results, then randomise them, which is going to mean possibly huge amounts of information being brought in for all but 10 records to be chucked out. I did toy with the idea of making a random script that would generate say 10 IDs, and only return those from the database, but that assumes you have a table nicely ordered and no data ever gets removed from the table. |
![]() |
![]() |
![]() |
#8 |
a.k.a. Sparky
Join Date: Sep 2004
Location: West Palm Beach, FL, USA
Posts: 2,396
|
no question that it is better to do order by rand() limit 10 than to take 2300 results and do a shuffle/array_slice. Its also better do select the 2-3 fields you need rather than select * if you're going to do an order by rand() since mysql will have to create a temporary table to contain the results before doing the order. If you had to do an order by rand() on 2300 results, its possible a subquery would handle the results a little faster if structured properly. I don't know as I haven't tested it, but, knowing how the query optimizer works with indexing, I can see some potential for it being a little quicker.
It also depends on how often you do that query. Does the page need to be generated each time the page is loaded? I've seen linklists that generate the page on every pageview where the results only change once a day or after reviews are done. Its all about handing the page to the surfer as quickly as possible, and, each time you do an SQL query, you slow down page generation a fraction and have the potential for losing your customer. order by rand() just has so much potential for abuse -- that's why I dislike it.
__________________
SnapReplay.com a different way to share photos - iPhone & Android |
![]() |
![]() |
![]() |
#9 |
If there is nobody out there, that's a lot of real estate going to waste!
Join Date: Dec 2003
Posts: 2,177
|
Not sure about SQL but on the bigger toys I normally work with I pull a record count & then generate X random selects between 0 & count to pull actual data.
But then I haven't really seen PerformancePartitioning & DatabaseFragmentation used on any PHP scripts either. |
![]() |
![]() |
![]() |
#10 |
a.k.a. Sparky
Join Date: Sep 2004
Location: West Palm Beach, FL, USA
Posts: 2,396
|
amazing that in 25 years we go from massively deploying RDBMS back to sharding.
__________________
SnapReplay.com a different way to share photos - iPhone & Android |
![]() |
![]() |
![]() |
|
|