Gossamer Forum
Home : Products : DBMan : Customization :

Display and image if there is a record in the DB.

Quote Reply
Display and image if there is a record in the DB.
I'm trying to get a number of how many records are in the DB I call toby to show on the master page.
Below is the code that I'm using, the path to "toby.db" is correct but I don't get a number to show.

What my end goal is, is to have and image show on a page (like a red bar) if there is one record or more in the data base
If there are no records then the image will not show.
Records are in this DB for a short time then deleted. The red bar will tell admin that there is a record in the DB.

You may know of a better way than what I'm thinking.

Any ideals?
Ed-

################################################################
cfg
################################################################

# Full path to toby database.
$db_file_toby = $db_script_path . "/../db/toby.db"; # This is the name of the Toby's DB.


################################################################
db.cgi
################################################################

sub db_record_count {
# --------------------------------------------------------
# Displays the number of records.

my $count = 0;
open (DB, "<$db_file_toby") or &cgierr("error. unable to open database:$db_file_toby.\nReason: $!");
LINE: while ( ) {
if (!(/^#/) && !(/^\s*$/)) {
$line = $_;
chomp ($line);
@fields = &split_decode ($line);
if ($per_admin) {
++$count;
}
}
}
close DB;
print $count;
}


################################################################
html.pl
################################################################

This is a test to see if it will print a number: $db_record_count
Quote Reply
Re: [knue] Display and image if there is a record in the DB. In reply to
if you only want the image to show if per_admin, i would put the if ($per_admin) before the code that calls your sub. while you're testing you might want to put the code to call your sub in the page header or page footer so it displays on every page

i'm just showing this code because yours was blank after while; i.e. there was nothing in the (); wanted to be sure you had DB in ()

Code:

LINE: while (<DB>) {
next if /^#/;
next if /^\s*$/;
$line = $_; # don't think you even need this because you don't care what the line is
# and the previous lines skip blank lines
++$count;

Last edited by:

delicia: Dec 30, 2015, 8:13 AM
Quote Reply
Re: [delicia] Display and image if there is a record in the DB. In reply to
in the page header or footer i would put the following code

my ($count);

if ($per_admin) {
$count = &db_record_count;
}
then just put $count somewhere like after the page_title or even down in the footer menu in one of the existing print statements

in sub db_record_count, have it return ($count) instead of printing $count
Quote Reply
Re: [delicia] Display and image if there is a record in the DB. In reply to
So far so good. I'm getting the count to show the record in DB toby.

This is what I have:

sub db_record_count {
# --------------------------------------------------------
# Displays the number of records.

my $count = 0;
open (DB, "<$db_file_toby") or &cgierr("error. unable to open database:$db_file_toby.\nReason: $!");
LINE: while (<DB>) {
next if /^#/;
next if /^\s*$/;
# $line = $_; # I'm going to remove these 3 lines
# chomp ($line); # It works without them
# @fields = &split_decode ($line); #
if ($per_admin) {
++$count;
}
}
close DB;
($count);
}

###################################

Then in the html I have :

sub html_home {
# --------------------------------------------------------
# The database manager home page.
$in{'UserID'} = $db_userid; # change ''UserID'' to the name of your user id field
$in{'Week'} = "*"; # or whatever indicates a validated record
my ($status, @hits) = &query("view");

my ($count);
if ($per_admin) {
$count = &db_record_count;
}

&html_print_headers;
print qq|
<html>


The call will go at the top of the page: $count

#########################################


I was thinking, instead of a image, could I have a table at the top of the page with no border and
a back ground color of white if there are no records and a back ground color of RED if there is a records?

Something like this:

<table border=0 bgcolor="code or something" cellpadding=5 cellspacing=3 width=500 align=center valign=top>
<tr><td>
<p><center><$font_title><b>
Toby
</b></font></center>
</td></tr>
</table>
Quote Reply
Re: [knue] Display and image if there is a record in the DB. In reply to
sounds good! after you do the call to figure the count, right before your table:

my ($color);
if ($count) { $color='red'; }
else ($color='white';}

then in your table set bgcolor=$color

i think i would just set the first row in the table to red instead of the entire table. it might be hard to read!
Quote Reply
Re: [knue] Display and image if there is a record in the DB. In reply to
You could trim that function down a fair bit:

Code:
sub db_record_count {
# --------------------------------------------------------
# Displays the number of records.
my $count = 0;
open (DB, "<$db_file_toby") or &cgierr("error. unable to open database:$db_file_toby.\nReason: $!");
while (<DB>) { $count++; }
close(DB);
return $count;
}

Or better yet:

Code:
sub db_record_count {
# --------------------------------------------------------
# Displays the number of records.
use File::Slurp;
my @tmp = read_file($db_file_toby);
return int(@tmp);
}

Cool

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!