Gossamer Forum
Home : Products : DBMan : Customization :

I can't fine the Random record mod on JPDeni's website

Quote Reply
I can't fine the Random record mod on JPDeni's website
I am trying to make both "search" and "list all" return with a random list. I think I used to see a random record mod on JPDeni's website, but I can't find it now, and when I search this forum, I don't seem to get a complete code and instruction. Any suggestions? Thanks a lot
Quote Reply
Re: [smeempress] I can't fine the Random record mod on JPDeni's website In reply to
I somehow find this from the Forum

----------------------------------- code ----------------------------


if ((!$in{'nh'}) && ($in{'sb'} == 25)) {
open (DATE, "<$db_script_path/random.txt") or &cgierr("error in random routine. \nReason: $!");
if ($db_use_flock) { flock(DATE, 1); }
$old_date=<DATE>;
$today = &get_date;
unless ($old_date eq $today) {
srand( time() ^ ($$ + ($$ << 15)) ); # try this and see if it works for you. If not, delete it.
open (DB, "<$db_file_name") or &cgierr("error in modify_records.
unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;
LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; }
if ($line =~ /^#/) { $output .= $line; next LINE; }
chomp $line;
@data = split /\|/, $line;
$data[25] = int(rand(1000)) + 1;
$output .= join '|',@data;
$output .= "\n";
}
open (DB, ">$db_file_name") or &cgierr("error in delete_records.
unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!");
}
print DB $output;
close DB;
open (DATE, ">$db_script_path/random.txt") or &cgierr("error in random routine. \nReason: $!");
if ($db_use_flock) {
flock(DATE, 2) or &cgierr("unable to get exclusive lock on date file.\nReason: $!");
}
print DATE $today;
close DATE;
}
}

------------------------------- code ------------------------

Is this the random display code I need?

Last edited by:

smeempress: Jan 23, 2006, 2:04 PM
Quote Reply
Re: [smeempress] I can't fine the Random record mod on JPDeni's website In reply to
I'm not sure what you want to do. The code that you have inserts a random number in one of the fields (field number 25 in this case), at most once per day.

If you use this, you'll need to be sure you have a file called random.txt. It can have anything (or even nothing) in it to start with. Once you run the script, the right number will be written to the file.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] I can't fine the Random record mod on JPDeni's website In reply to
Hi, Jpdeni, Thanks for the reply.
I guess let's forget about the code I've posted.
All I want is:
Every time people do search or go list all, the result comes out randomly. Because we have members record listed in our database, and some of members don't want to always stay at the last page when their record shows up in the search.
Any idea how to do it?
Quote Reply
Re: [smeempress] I can't fine the Random record mod on JPDeni's website In reply to
The problem with displaying records in random order comes in when there are multiple pages returned. It's really hard to explain, maybe because I just got up and my brain is still asleep. :-) I'll give it a try, though.

The way DBMan works is that when there are multiple pages returned from a search, each time the user goes to another page, the script does the search all over again. It doesn't remember anything from one click to the next. If the search results are displayed randomly on the first page, it's likely that some of the same records would be displayed on the second page.

The code that you showed seems to be the best way to do it. I think I remember working on that before, although it was years ago. The reason that it only randomizes once a day is that it takes some time to do and if you had a lot of people visiting your site it would take too much of the available resources.

It seems to me that the code you posted would be the best to use. You wouldn't get a different random order every time, but your members would all have an equal chance of being on the first page for a day. You would probably have to add a field for the random number, which would mean adding the field to all of the current records. It's a pain, but you'd only have to do it once.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] I can't fine the Random record mod on JPDeni's website In reply to
So basicly I will create a empty txt file called random.txt in the dbman folder, and paste the code above into db.cgi right after "Local(%sortby);" in subquery. I will aslo have to add a number field to my database definition, and match the field number to the number "25" above in the code, is that right? Do I have to do anything else to the html.pl and default.cfg file?
Any instructions on how to do this?
Thanks in Advance.

Last edited by:

smeempress: Jan 24, 2006, 8:41 AM
Quote Reply
Re: [smeempress] I can't fine the Random record mod on JPDeni's website In reply to
Quote:
I will create a empty txt file called random.txt in the dbman folder

Right.

Quote:

paste the code above into db.cgi right after "Local(%sortby);" in subquery

Right.

Quote:

I will aslo have to add a number field to my database definition, and match the field number to the number "25" above in the code, is that right?

Right. And you will have to add a place for the field to each record. If you add the field at the end of the list of fields in your .cfg file (which is the best way to do it, you'll just have to add an extra delimiter to the end of each record. For example, if your delimiter is the |, and you have a record like:

23|John|Doe|New York|New York|Godzilla|purple

add an extra delimiter so that it looks like

23|John|Doe|New York|New York|Godzilla|purple|

Quote:

Do I have to do anything else to the html.pl and default.cfg file?

In html.pl, sub search options, you might want to remove

Code:

Sort By:
<SELECT NAME="sb">
<OPTION>---
~; for (my $i =0; $i <= $#db_cols; $i++) { print qq~<OPTION VALUE="$i">$db_cols[$i]</OPTION>\n~ if ($db_form_len{$db_cols[$i]} >= 0); } print qq~
</SELECT>
Sort Order:
<SELECT NAME="so">
<OPTION VALUE="ascend">Ascending
<OPTION VALUE="descend">Descending
</SELECT>


and add

Code:

<input type=hidden name="sb" value="25">


Be sure to change 25 to the actual number of your randomizing field.

This will make every search have the random order display.

Also, change

Code:

print qq!| <A HREF="$db_script_link_url&view_records=1&$db_key=*">List All</A> ! if ($per_view);


to

Code:

print qq!| <A HREF="$db_script_link_url&view_records=1&$db_key=*&sb=25">List All</A> ! if ($per_view);


again, changing the 25 to match your field number.

I think that should do it.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] I can't fine the Random record mod on JPDeni's website In reply to
I tried it and it worked, at least the record didn't show up in order

However, when I took a look in the Default.db file, the records has been changed from:

1|Frank fishing camp|http://www.wizzm.com|EH|MH|BH|SG|DUH|
2|ruth fishing camp|http://www.ruth.com|FIL|RIL|BIO|FO|TP|M|DEH|MH|BH|SG|DUH||BH|SG|
3|Mike's Fishing Camp|http://www.bilsbarrow.com|RIL|BIO|FO|TP|M|DEH|MH|BH|SG|DUH|
4|wow total fish|http://www.ebay.com|AP||DIO|DV||||BIO|FO|||||BH||DUH|
5|Loop stuff|http://www.20093871728464.com|MAP|||BIL||RIL|BIO||TP|M|||||DUH|

TO:

1|Frank fishing camp|http://www.wizzm.com|EH|MH|BH|SG|DUH|
2|ruth fishing camp|http://www.ruth.com|FIL|RIL|BIO|FO|TP|M|DEH|MH|BH|SG|DUH||BH|SG||791
3|Mike's Fishing Camp|http://www.bilsbarrow.com|RIL|BIO|FO|TP|M|DEH|MH|BH|SG|DUH|
4|wow total fish|http://www.ebay.com|AP||DIO|DV||||BIO|FO|||||BH||DUH|
5|Loop stuff|http://www.20093871728464.com|MAP|||BIL||RIL|BIO||TP|M|||||DUH|

How come only one record has been assigned a random number? And it has two delimiters before the number 791 ?

Btw, the first time I did the list all it shows up in the order: 2,4,1,5,3,
after 5-10 minutes, I did couple more searches and they have the order: 2,3,5,4,1, about 5-10 minutes later I did another search and the order now became: 2,4,5,3,1, and stay that order. I thought I have to wait for another day to see the changes. But I noticed that record number 2 always stay at the top. Any idea how that happened? Maybe it is just a matter of percentage for a record to stay at the top, since its doing randomizing not rotating, is that right?

Thanks.

Last edited by:

smeempress: Jan 24, 2006, 1:42 PM
Quote Reply
Re: [smeempress] I can't fine the Random record mod on JPDeni's website In reply to
Record 2 is first probably because it's the only one that has a number in that field.

There are some problems with your .db file, though, even before making the changes. Record 1 has 8 fields. Record 2 has 17 fields. Record 3 has 13 fields. It's too hard to count the rest. All of your records must have the same number of fields. Basically, they all need to have the same number of delimter characters. I'm pretty sure the code will work if you fix the records.

You know what, though? I just realized that you don't need to add the delimiter at the end. I'm sorry. I wasn't thinking clearly. The script will add it as it did with the second record.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] I can't fine the Random record mod on JPDeni's website In reply to
By the way, I have my field defined as below:

'RANDOM' => [24, 'alpha', 20, 255, 0, '', ''],

The Form-length is 20 and the Maxlength is 255, should I set them to 0 and 3? and make it not a null field? or maybe it doesn't matter.

Quote:
Record 1 has 8 fields. Record 2 has 17 fields. Record 3 has 13 fields.
Yes, I deleted some of the field just to make it short, sorry.
Anyways, the .DB file now looks like:


1|Frank fishing camp|http://www.wizzm.com|wowowo@hotmail.com|frank|Yes|HK|AP|MAP|DIO|DV|BIL|FIL|RIL|BIO|FO|TP|M|DEH|MH|BH|SG|DUH|821
2|ruth fishing camp|http://www.ruth.com|ruth@freemap.ca|ruth|Yes|HK|AP|MAP|DIO|DV|BIL|FIL|RIL|BIO|FO|TP|M|DEH|MH|BH|SG|DUH|900
3|Mike's Fishing Camp|http://www.bilsbarrow.com|mike@freemap.ca|mike|Yes|HK|AP|MAP|DIO|DV|BIL|FIL|RIL|BIO|FO|TP|M|DEH|MH|BH|SG|DUH|434
4|wow total fish|http://www.ebay.com|we'll call you|admin|Yes||AP||DIO|DV||||BIO|FO|||||BH||DUH|627
5|Loop stuff|http://www.20093871728464.com|phone, website, etc``1-800-dee-pfry|admin|Yes|||MAP|||BIL||RIL|BIO||TP|M|||||DUH|365

record number 2 have the highest number 900, I am just think what will happen if I mannually change it to... say 100?
Quote Reply
Re: [smeempress] I can't fine the Random record mod on JPDeni's website In reply to
Don't make the field "not null." That would mean that users would have to fill in a number when they add a record. The size of the field won't matter.

You should add a hidden field to sub html_record_form. Sorry, I forgot about it before.

Quote:
I am just think what will happen if I mannually change it to... say 100?

Then that field will sort in a different order.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] I can't fine the Random record mod on JPDeni's website In reply to
Thanks a lot, JPDeni. You are super helpful.
Thank you.
Quote Reply
Re: [JPDeni] I can't fine the Random record mod on JPDeni's website In reply to
Well, Here we have something new
The records didn't change their order for over a day for some reason
I took a look at the Database file default.db
It looks like:

1|Frank fishing camp|http://www.wizzm.com|wowwowowo@hotmail.com|frank|Yes|HK|AP|MAP|DIO|DV|BIL|FIL|RIL|BIO|FO|TP|M|DEH|MH|BH|SG|DUH|821|25
2|ruth fishing camp|http://www.ruth.com|ruth@freemap.ca|ruth|Yes|HK|AP|MAP|DIO|DV|BIL|FIL|RIL|BIO|FO|TP|M|DEH|MH|BH|SG|DUH|900|54
3|Mike's Fishing Camp|http://www.bilsbarrow.com|mike@freemap.ca|mike|Yes|HK|AP|MAP|DIO|DV|BIL|FIL|RIL|BIO|FO|TP|M|DEH|MH|BH|SG|DUH|434|787
4|wow total fish|http://www.ebay.com|we'll call you|admin|Yes||AP||DIO|DV||||BIO|FO|||||BH||DUH|627|507
5|Loop stuff|http://www.20093871728464.com|website, etc``1-800-dee-pfry|admin|Yes|||MAP|||BIL||RIL|BIO||TP|M|||||DUH|365|199

It seems that its adding another set of unmbers after my last field (random field) instead of replacing them,
the database still tries to sort the records in the order of that random field, so its not really working, I wounder why.
Any idea?
Thanks
Quote Reply
Re: [smeempress] I can't fine the Random record mod on JPDeni's website In reply to
I took a look at the code I inserted in the db,cgi file

------------------------------------
if ((!$in{'nh'}) && ($in{'sb'} == 24)) {
open (DATE, "<$db_script_path/random.txt") or &cgierr("error in random routine. \nReason: $!");
if ($db_use_flock) { flock(DATE, 1); }
$old_date=<DATE>;
$today = &get_date;
unless ($old_date eq $today) {
srand( time() ^ ($$ + ($$ << 15)) ); # try this and see if it works for you. If not, delete it.
open (DB, "<$db_file_name") or &cgierr("error in modify_records.
unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;
LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; }
if ($line =~ /^#/) { $output .= $line; next LINE; }
chomp $line;
@data = split /\|/, $line;
$data[25] = int(rand(1000)) + 1;
$output .= join '|',@data;
$output .= "\n";
}
open (DB, ">$db_file_name") or &cgierr("error in delete_records.
unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!");
}
print DB $output;
close DB;
open (DATE, ">$db_script_path/random.txt") or &cgierr("error in random routine. \nReason: $!");
if ($db_use_flock) {
flock(DATE, 2) or &cgierr("unable to get exclusive lock on date file.\nReason: $!");
}
print DATE $today;
close DATE;
}
}

------------------------------------------

There are two 25 in the code, and I just replaced one of them with 24, which is my random field number.

Is that causing the problem?
Quote Reply
Re: [smeempress] I can't fine the Random record mod on JPDeni's website In reply to
Quote:
Is that causing the problem?

Yes. And you may need to go delete the extra field from all of your records. I'm not sure what will happen if you leave it there.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] I can't fine the Random record mod on JPDeni's website In reply to
Thanks a lot, JPDeni