Gossamer Forum
Home : Products : DBMan : Customization :

RRRrrandom!

Quote Reply
RRRrrandom!
I know this horse is getting pretty whipped.

Here's where I'm at. I want to create the return of a single random record in long form from a link.

In sub query, under local (%sortby); I've included the code previously mentionned:
if ($in{'random'})
{
srand();
$values[11]=int(rand(1000)) + 1;
$in{'sb'} = 11;
}

The last (11th) field is left blank, and is defined in my .cfg file, I've added a "|" (pipe) at the end of each record.

The URL I'm using is

db.cgi?db=default&uid=default&random=1&view_records=View+Records

I get a "No matching request". Even though I've read all the related threads, I still can't get it.

Suggestions? Thank!
Quote Reply
Re: RRRrrandom! In reply to
[Deleted]

[This message has been edited by Frank0000001 (edited October 03, 1999).]
Quote Reply
Re: RRRrrandom! In reply to
You have the code in the wrong place.

Look for:

Code:
# Now we go through the database and do the actual searching.

Add your code after

Code:
@values = &split_decode($line);

You also have to do a search for something. This code puts the search results in random order. It does not search for anything.


------------------
JPD





Quote Reply
Re: RRRrrandom! In reply to
Thanks! I was editing my original when you replied. Here it is...

Here's the goal: To create the return of one single random long record (using the long/short mod) via a URL.

I found one post that seemed to suite my needs:

http://www.gossamer-threads.com/scripts/forum/resources/Forum12/HTML/000674.html

I have followed the instructions I think quite precisely, and I'm going amiss. Can someone tell me if I've missed something?

This is what I've included in the sub query under the correct lines, seems right, as I've read it exactly this way in a few places now. (My random field is the last, and number 11.

if ($in{'random'})
{
srand();
$values[11]=int(rand(1000)) + 1;
$in{'sb'} = 11;
}

In html.pl, under

sub html_home {
# --------------------------------------------------------
# The database manager home page.

$page_title = "Main Menu";
&html_page_top;

# < -- Start page text -- >

#
# First one here, right?
#

if ($status eq "ok") {
for (0 .. $numhits - 1) {
print "<P>";
&html_record (&array_to_hash($_, @hits));
}
}

#
# End first one
#

print qq|
<blockquote>
<$face size="1"><b>
Permissions:
|;

#
# And this is the second one
#
if ($status eq "ok") {
for (0 .. $numhits - 1) {
print "<P>";
&html_record (&array_to_hash($_, @hits));
}
}

#
# End second one
#
print " View " if ($per_view);
print " Add " if ($per_add);
print " Delete " if ($per_del);
print " Modify " if ($per_mod);
print " Admin " if ($per_admin);
print " None " if (!($per_view &#0124; &#0124; $per_add &#0124; &#0124; $per_del &#0124; &#0124; per_mod));
print qq|</b></font>
<p><$font>This database has been set up so any user can view information.</font></p>
</blockquote>
|;
# < -- End page text -->

&html_footer;
&html_page_bottom;
}

At this point, it should return a random record, should it not? It isn't returning one though. Sure would appreciate any suggestion. I'm stumped.
Quote Reply
Re: RRRrrandom! In reply to
But you don't have a search, Frank.

I don't understand what you mean by "the first one" and "the second one."

This is what I would do. You have the code in the correct place in sub query. Here's what you need for sub html_home:

Code:
sub html_home {
# --------------------------------------------------------
# The database manager home page.

$page_title = "Main Menu";
&html_page_top;

# Get random record
$in{$db_key} = "*";
$in{'mh'} = 1; # change this to the number of records you want to show
$in{'random'} = 1; # Did I forget this before?
my ($status, @hits) = &query("view");
my ($numhits) = ($#hits+1) / ($#db_cols+1);

# Print out record
if ($status eq "ok") {
for (0 .. $numhits - 1) {
print "<P>";
&html_record_long(&array_to_hash($_, @hits));
}
}

# < -- Start page text -- >
print qq|
<blockquote>
<$face size="1"><b>
Permissions:
|;

print " View " if ($per_view);
print " Add " if ($per_add);
print " Delete " if ($per_del);
print " Modify " if ($per_mod);
print " Admin " if ($per_admin);
print " None " if (!($per_view &#0124; &#0124; $per_add &#0124; &#0124; $per_del &#0124; &#0124; per_mod));
print qq|</b></font>
<p><$font>This database has been set up so any user can view information.</font></p>
</blockquote>
|;
# < -- End page text -->

&html_footer;
&html_page_bottom;
}

Looks like I made a couple of errors in what I wrote before. That's why I really hope people will come back and tell me one way or the other whether the code worked or not.

I've gone back in and fixed the previous one, though.

------------------
JPD





Quote Reply
Re: RRRrrandom! In reply to
Thank you. (Do you ever sleep? Smile) It's currently 3:39:37 AM. I'll get back to you later in the day and report. I guess these are all being saved up for a revision, eh?
Quote Reply
Re: RRRrrandom! In reply to
I was up pretty late last night. Smile

Quote:
I guess these are all being saved up for a revision, eh?

I wouldn't expect that many of the mods posted in the forum will be seen in the next revision of DBMan. If every modification that anyone might want is included, the script will become too large to be of much use to anyone. It really is better to have a basic script that can be modified.

Then again, I don't have anything at all to do with what is in future revisions. Smile


------------------
JPD





Quote Reply
Re: RRRrrandom! In reply to
OK. I'm feeling a little frustrated, and stupid. Let me see if I can review things clearly and logically. Sorry if I'm sounding repetitious, and for the length of this post. I'm trying to reason this though as clearly as I can.


I want to have a url that when clicked returns a single random record in long form (with the image).

I'm using the long/short mod, and the file-upload mod.

In order to get a record, one must perform as search; the showing of one or more records, in long or short form are the results of a query, and it's being processed though a sub rotine in html.pl

A query is performed by passing parameters to the sub query routine in db.cgi, via a url, or form action such as

Code:
&city=Brampton ...&View+Records (tells the db which html subroutine to push it through)

Now, the snippet

Code:
.
.
.
@values = &split_decode($line);

# Random sort

if ($in{'random'})
{
srand();
$values[11]=int(rand(1000)) + 1;
$in{'sb'} = 11;
}

Says "If the query string contains 'random" make the sort order random.

Therefor, when one want a group of records returned in a random order, one uses a url that contains

Code:
&city=Brampton&sb=11 (sorting by the field that is random)


The above, will return every record matching &city=Brampton in a random order, sorted by the values of field #11 but it will choose the first record, always.

One would think that everytime this query is performed, that because the sort order is different, that the first record would change everytime. This doesn't seem to be the case.

So my next move is/was to include the following code, under the sub html_home page. This is the routine that when you use no parameters in a url, that will bring you to the log in screen. I did this because I thought that the original poster (see above conversations) had it working. It don't.

Code:
# Get random record
$in{$db_key} = "*";
$in{'mh'} = 1; # change this to the number of records you want to show
$in{'random'} = 1; # Did I forget this before?
my ($status, @hits) = &query("view");
my ($numhits) = ($#hits+1) / ($#db_cols+1);

# Print out record
if ($status eq "ok") {
for (0 .. $numhits - 1) {
print "<P>";
&html_record_long(&array_to_hash($_, @hits));
}
}

Brain flash! Because I need it to return a full record, with an image, I must put it under sub html_record, under

Code:
$long_url = "$db_script_url?$long_url&nh=$record_number&mh=1";

This is the only place that will generate a link that will properly display an image in the long record form. I don't mind sending them though the short form first.

Current Results: If I do a regular search, I get all the results returned, but with a long form record above every short record.

If I understand the this correctly

Code:
# Get random record
$in{$db_key} = "*";
$in{'mh'} = 1; # change this to the number of records you want to show
$in{'random'} = 1; # Did I forget this before?
my ($status, @hits) = &query("view");
my ($numhits) = ($#hits+1) / ($#db_cols+1);


the above says:

[some value (what is $in ?)] equals any content of the field set as the database key field, max hits = 1, [something] random = 1 and use the "view" subroutine of sub query. take the results and...

Code:
# Print out record
if ($status eq "ok") {
for (0 .. $numhits - 1) {
print "<P>";
&html_record_long(&array_to_hash($_, @hits));
}
}


if permissions say it's OK for me to see a record, for the amount of times in Max Hits (1 in this case), print <p> then the contents of what's in the long form.

Here's where I get lost:

How do I actually implement this? Where do I put it so that I can through a url bring it up in the long form with the image (from file-upload) showing correctly, and what does the URL I want look like?

Quote Reply
Re: RRRrrandom! In reply to
I had a really nifty random record mod all ready to go. And then I realized that you want to return a random record from within a search.

Code:
&city=Brampton

Back to the drawing board.


------------------
JPD





Quote Reply
Re: RRRrrandom! In reply to
You know what? I think we should go about this a completely different way. Understand that I wrote the code you're using about a year ago when I had very little understanding of the workings of DBMan.

Let me work on it just a bit today and I'll get back to you. (It's a little more than I can do while I'm sitting online.)


------------------
JPD





Quote Reply
Re: RRRrrandom! In reply to
I've been trying to figure out how to do this, but I just don't think I can. I can give you the code to pull one or more random records from the .db file, but not to give a random record from the results of a search.

It may be possible to do, but at this point I can't do it.


------------------
JPD





Quote Reply
Re: RRRrrandom! In reply to
Oh! I don't care *how* I get the random record returned, I just thought that a search was necessary due to the way that you were explaining it to me. If I can click on a link, and get *any* record that's different every time, I don't care *how* we go about it.

And by the way, it's really obvious that you are putting time an engergy into this for a complete stanger. Sure wish I had an appropriate way to thank you.
Quote Reply
Re: RRRrrandom! In reply to
 Smile Well, it becomes a crusade for me once I get started. (We'll talk about thanking me later! Wink )

I think this should work.

In db.cgi, sub main, with all the other "elsif" statments, add

Code:
elsif ($in{'random_record'}) { if ($per_view) { &html_random_record; } else { &html_unauth; } }

Add a new subroutine to db.cgi:

Code:
sub get_random {
# --------------------------------------------------------

my ($random) = $_[0];
my (@hits,$line,@lines,@rlines,$random_number,@values,$tmp,$j);
open (DB, "<$db_file_name") or &cgierr("error in get_random.
unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;

foreach $line (@lines) {
if ($line =~ /^$/) { next; }
if ($line =~ /^#/) { next; }
push (@rlines,$line);
}
srand( time() ^ ($$ + ($$ << 15)) );
$rvalue = scalar(@rlines);

$random_number = int(rand $rvalue);
@values = &split_decode($rlines[$random_number]);
push (@hits, @values);

return (@hits);
}

In html.pl sub html_footer (or wherever you want to print the link to a random record) add:

Code:
print qq!| <A HREF="$db_script_link_url&random_record=1">Random Record</A> ! if ($per_view);

In html.pl, add a new subroutine:

Code:
sub html_random_record {
# --------------------------------------------------------
# This page displays the results of a successful search.
# You can use the following variables when displaying your
# results:
#
# $numhits - the number of hits in this batch of results.
# $maxhits - the max number of hits displayed.
# $db_total_hits - the total number of hits.
# $db_next_hits - html for displaying the next set of results.
#

my (@hits) = &get_random;
$db_total_hits = 1;

$page_title = "Random Record";
&html_page_top;

&html_record_long(&array_to_hash(0, @hits));

&html_footer;
&html_page_bottom;

}

I have tested out the get_random code, but I haven't tested the rest of it within DBMan. Seems like it should work, though.


------------------
JPD





Quote Reply
Re: RRRrrandom! In reply to
And this, ladies and gentlemen, concludes our exciting episode of "As the Random Record Turns"

Thank you, JPD, this solution has worked very well, and was very easy to implement. It is precisely what I was looking for, and I learned a lot to boot.

There is no feeling quite like that of seeing something long sought after coming to fruition.

Thanks again!
Quote Reply
Re: RRRrrandom! In reply to
Two questions.

1) Is the last message from JPD the entire solution to random display?

2) If I simply change $db_total_hits = 1; to $db_total_hits = 5; will I get 5 records or do I need to change something else as well to get 5 random records.

Thanks!
Adam
Quote Reply
Re: RRRrrandom! In reply to
No. You would probably have to change

Code:
&html_record_long(&array_to_hash(0, @hits));

to

Code:
for (0 .. 4) {
&html_record (&array_to_hash($_, @hits));
}

You wouldn't want to use &html_record_long for more than one hit.

Then again, I'm not sure if this would work or not. I haven't looked at this code in a long time. But you can give it a try and see what happens.


------------------
JPD





Quote Reply
Re: RRRrrandom! In reply to
I have a question regarding this. I do not have the short/long display mod in place. How can I use this code without it?

Thanks,
Frank
Quote Reply
Re: RRRrrandom! In reply to
Alright, I figured out how to do it. Just had to remove a couple of the subroutine calls, and change html_record_long to html_record.

One thing I'm noticing, and maybe it's just how I'm setup... the value of last field I have defined in my .cfg file doesn't seem to show up in any random ad.

Any ideas?

Thanks,
Frank
Quote Reply
Re: RRRrrandom! In reply to
You may have already fixed this, from what you said in the other topic. If so, don't worry about responding here.


------------------
JPD