[FX.php List] FMFindAny to pull two random records

DC dan.cynosure at dbmscan.com
Mon Feb 19 12:50:40 MST 2007


thinking about this... it's a nice little problem to chew on.

the fmfindany solution is pretty simple for one record but not really 
suitable for multiple. if you really want random and unique you'll have 
to check if the two records from the two separate fmfindany calls are 
unique. then if it is the same record, you'd have to call fmfindany 
again! you could end up with a cascade of fmfindany calls because the db 
keeps randomly finding the same record.

in general, the fewer calls to FMP, the better. so, for this reason 
don't rely on fmfindany. what if you eventually want to have 3 4 or 5 or 
10 random images? the design becomes too unmanageable and prone to this 
cacsade problem.

here's what i'd try.

if you don't have one in the database already, create a field with FMP4+ 
Status(CurrentRecordNumber) OR in FMP7+ Get(RecordNumber). in a calc. 
this ensures that you'll always have a sequential numbering of records 
with no gaps. remember to make result a number. as long as you know how 
many records there are, you'll always be able to generate proper random 
numbers. making a new field with this calc instead of relying on 
auto-increment serial field is a good idea because sometimes FMP records 
get deleted and then there will be gaps.

then search on that field with a random value range generated with PHP 
(see code below) querying the db using the FMP logical OR operator 
(assuming there are no other components to your search for 'random' 
records). you'll end up with two ids to build the OR search on.

this code is tested.
<?php
// demonstrates how to get any
// number of unique random numbers
// from any range
$min=1;
$max=100;
$cnt=2;
// make an array with values min to max
$a=range($min,$max);
// randomly order it
shuffle($a);
//pull out the first values into a new array
$rand_uniq_array=array_slice($a,0,$cnt);
// pull out the array values into their own variables
$rand1=$rand_uniq_array[0];
$rand2=$rand_uniq_array[1];

echo '<pre>';
print_r($rand1);
echo '<br>';
print_r($rand2);
echo '</pre>';

?>

to be really thorough with this approach (or if you wanted to avoid 
adding an unstored calc field to your db) you'd need to get the range of 
ids from the database first by doing a findAll() and then shuffle that 
array of ids and then pull the random ids only from that selection.

if all your results are really in one portal on one main record it 
should be easy enough to use shuffle() on a copy of that portal array 
and then pull out the first two values. i don't think that's your set up 
though.

my2,
dan



Alex Gates had written:
> Hi everyone-
> 
> I'd like to display two random records on the side of my page as a way
> of having some dynamic content.  Each will consist of an image and also
> some text to be used in a mouseover tooltip.
> 
> Is it advisable to use FMFindAny twice (two separate queries to the
> database) in order to get two randomly returned sets?
> 
> I imagine that this method could theoretically return the same record
> twice... but with 100 or so records in the database, it seems rather
> unlikely... but I imagine it is still possible.
> 
> Does anyone have any experience with this or any advice?
> 
> 
> Alex P. Gates
> 
> 
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list
> 


More information about the FX.php_List mailing list