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

Alex Gates alex at gandrpublishing.com
Mon Feb 19 13:52:56 MST 2007


Thanks for your suggestions, Dan - 

This is what I ended up doing:

I made a calculation in my layout to count the number of records in my
portal.

My query:
<?php
$search=new FX($serverIP,$webCompanionPort);
$search->SetDBData('database.fp7','layout', '1');
$search->SetDBPassword('xxxxxx','xxxxxx');
$searchResult=$search->FMFind();
$searchData = current($searchResult['data']);
?>

Then:

<?php
$total = $searchData['TotalInPortal'][0];
$first = rand(0, $total);
if($first < $total)
{
$second = $first + 1;
}
else{
$second = $first - 1;
}
?>

Then:

<?php 
echo $searchData['layout::field1'][$first];
echo $searchData['layout::field2'][$first];
?>
<img src="FX/image_proxy.php?FXimage=<?php echo
vignereEncryptURL($searchData['layout::image'][$first]); ?>"
height="122">
<?php
echo "<br />";
echo $searchData['layout::field'][$second];
echo $searchData['layout::field'][$second];
?>
<img src="FX/image_proxy.php?FXimage=<?php echo
vignereEncryptURL($searchData['layout::image'][$second]); ?>"
height="122">


This obviously brings the two records that exist next to each other in
the database - but this is ok - and it prevents the possibility of
displaying the same record twice.

I'll have right around 100 records in this portal at all times - so I'm
not worried about it getting too far out of hand... 

So far it seems to work pretty well...


Alex P. Gates


-----Original Message-----
From: fx.php_list-bounces at mail.iviking.org
[mailto:fx.php_list-bounces at mail.iviking.org] On Behalf Of DC
Sent: Monday, February 19, 2007 1:51 PM
To: FX.php Discussion List
Subject: Re: [FX.php List] FMFindAny to pull two random records

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
> 
_______________________________________________
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