How can I use the random mod inside another sub routine.
What I would like to do display random text or html from a seperate db, on a page. I don't need to call it from a link.
I've put sub get_random in the cgi, and I've got the database set up, but I carn't figure out how to call it from within another sub routine, as the mod uses a seperate sub routine to call &html_record_long.
This is the random mod as is:
# --------------------------------------------------------
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);
}
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've tried using
for (0 .. 4) {
&html_adisplay (&array_to_hash($_, @hits));
}from withinside a subroutine, but it turns up blank.
I've also added another display subroutine.
my (%rec) = @_;
print qq|
<TABLE WIDTH="270" CELLPADDING=0 CELLSPACING=0 BORDER=0>
<TR><TD >$rec{'Adhtml'}</TD>
</TR>
</TABLE>
|;
}
thanks
Bob
What I would like to do display random text or html from a seperate db, on a page. I don't need to call it from a link.
I've put sub get_random in the cgi, and I've got the database set up, but I carn't figure out how to call it from within another sub routine, as the mod uses a seperate sub routine to call &html_record_long.
This is the random mod as is:
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);
}
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;
}
Code:
my (@hits) = &get_random; for (0 .. 4) {
&html_adisplay (&array_to_hash($_, @hits));
}
I've also added another display subroutine.
Code:
sub html_adisplay { my (%rec) = @_;
print qq|
<TABLE WIDTH="270" CELLPADDING=0 CELLSPACING=0 BORDER=0>
<TR><TD >$rec{'Adhtml'}</TD>
</TR>
</TABLE>
|;
}
thanks
Bob