Gossamer Forum
Home : Products : DBMan : Customization :

Extracting new lines...

Quote Reply
Extracting new lines...
I am doing a site for a news station. Suppose I have multiple db's. One for sports, one for world news, one for local news, etc. Is there a subroutine I could write to access the newest (by dbkey) record from each db and place it on one page?

---Scratch that...
Just one simple subroutine for each database. Then I could use SSI to run each program and get the data for the main page. I need a new mini program because you can't pass name=value sets through SSI. How would I grab the newest line from a DB without using the DB man program? I would use DBMan for searching and adding records.

Thanks

[This message has been edited by ChrisE (edited July 06, 1999).]
Quote Reply
Re: Extracting new lines... In reply to
Do all the databases have the same structure?



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





Quote Reply
Re: Extracting new lines... In reply to
Yes, they do have the same structure.

Wow, that was FAST!

------------------
webmaster@racedaze.com
Quote Reply
Re: Extracting new lines... In reply to
I know. I happened to be on the forum at the same time as you were. I answered your question before you had edited it. Smile

You can do this within DBMan. It really wouldn't be too hard. How many of the latest stories do you want for each of the databases?


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





Quote Reply
Re: Extracting new lines... In reply to
There will only be one story on the front page from each db. There will only be 7 stories at a time in the db as one story is added per day and archived for seven. After that, it is deleted.

------------------
webmaster@racedaze.com
Quote Reply
Re: Extracting new lines... In reply to
So this will be in html_home, right?

I'm assuming that your databases are all in the same directory -- and in the same directory as DBMan. If not, this will take a little more work.

Also, I'm assuming that your databases are named
world.db
local.db
sports.db

You can probably see where to make changes if you have it set up differently.

Code:
@databases = ('world','local','sports');
foreach $database (@databases) {
open (DB, "<$db_script_path/$database.db") or &cgierr("error in reading file.
unable to open db file: $database.db.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;
$line= chomp($lines[$#lines]);
@rec_array = split /$db_delim/, $line;
%rec = &array_to_hash(0,@rec_array);
&html_record(%rec);
}

I think that should do it. You might need to do some stuff with formatting, to get it to look the way you want it to, but this should get the last record from each of your three databases and print them out in the format you have defined in html_record.


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





Quote Reply
Re: Extracting new lines... In reply to
Where would I insert this code?

How would I delete the oldest record (by dbkey) when an eighth is added?

Thanks for all the help JP! You are the greatest.
Quote Reply
Re: Extracting new lines... In reply to
I'd put the code in html_home, if you want users to see the stories as soon as they enter the database.

Deleting the earliest record when a new one is added is a completely separate issue.

Hmmmmm.

In db.cgi, sub add_record, change

Code:
open (DB, ">>$db_file_name") or &cgierr("error in add_record. unable to open database: $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 &join_encode(%in);
close DB; # automatically removes file lock

to

Code:
open (DB, "<$db_file_name") or &cgierr("error in add_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>; # Slurp the database into @lines..
close DB;
$i = 1;
while ($i<6) {
$output .= $lines[$i];
++$i;
}
$output .= &join_encode(%in);
open (DB, ">$db_file_name") or &cgierr("error in add_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; # automatically removes file lock

Quote:
Thanks for all the help JP! You are the greatest.

You're welcome. Smile That's what my hubby keeps telling me! Wink

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