Gossamer Forum
Home : Products : DBMan : Customization :

Random script--pulls from field, only if populated

Quote Reply
Random script--pulls from field, only if populated
Do you know if a "Random" script exists that would pull from a field from the database, only if data exists in that field? (In other words, I am working on a "Featured Member" script that will feature members on a home page with a 25 word or less description. Only about 20% of the members have these descriptions, so I only want it to randomly pull from these members if they have entered a short description...

Do you know of any mods that exist for this? I've tried searching the FAQ but haven't been able to find anything so far.

Thanks!
Kelly Cotton

Quote Reply
Re: Random script--pulls from field, only if populated In reply to
Give this a go (paste into the area you want to print the member):
Code:
my (@populated, @line);
open (DB, "<$db_file_name") or
&cgierr(" unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
while (<DB>) {
@line = &split_decode($_);
if ($line[FIELD_NUMBER_OF_DESCRIPTION]) { push(@populated, $_); }
}
close DB;
my %member = &array_to_hash(0,$populated[rand(scalar(@populated))]);
You should then be able to use $member{'FIELD'} (similar to using $rec{'FIELD'}) to print out the random member's record details.

I haven't tested it, so let me know how it goes. Smile

- Mark


Astro-Boy!!
http://www.zip.com.au/~astroboy/