CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
DB manipulation

 



skirrow
stranger

May 14, 2001, 12:30 PM

Post #1 of 2 (587 views)
DB manipulation Can't Post

Hi,
Ok, I have the following code in a script I'm writing.


Code
	open (DATA2, "$home/dbs/allrev.db"); 
@fields = <DATA2>;
close (DATA2);
foreach $entries2 (@fields){
($categoryid, $rreviewid, $rtitle, $rrating, $rurl, $rrelease, $rversion, $rscriptauthor, $rprice, $rreview, $rfooter, $rauthor, $rauthorname, $rdate, $rhits) = split(/\|/, $entries2);
print "<TABLE BORDER=0 WIDTH=420 CELLPADDING=1 CELLSPACING=0>";
print "<TR><TD>";
print "<font face=Arial size=2><b><a href=\"/index.cgi?category=$categoryid\&review=$rreviewid\">$rtitle</a></b> <img src=\"images/$rrating.gif\" alt=\"$rrating out of 10\"></font></td></tr><tr><td><font face=Arial size=1>Release Date: $rrelease - Version: $rversion - Price: $rprice
</font>";
print "</TD></TR>";
print "</TABLE>";
}


Right, what I want to do, is using modified versions of this code I want to list the last 10 lines of the database it's displaying. With the bottom record first, and the 10th from last record at the bottom of the list.

In a separate part of the script, I want to list all the records, sorted by the $rhits field which just contains numbers. I'd like to print out the records with the highest first. e.g.
Blah, 100
Blob, 70
Hello, 40
So it lists the records with the record with the highest $rhits value at the top.

Any help with this would be great.

Neil

P.S. The database is a flat-file text db with one record per line. Newest records are at the bottom.

--
http://www.cgi-index.com/
http://www.cgiscriptreview.com/


randor
User

May 14, 2001, 2:03 PM

Post #2 of 2 (583 views)
Re: DB manipulation [In reply to] Can't Post

ok, for the first part, the select the last 10, try this:

open (DATA2, "$home/dbs/allrev.db");
@fields = <DATA2>;
close (DATA2);
foreach $entries2 (@fields){ unshift(@fields2, $entries2);}
foreach $entries3 (@fields2){
$i = '0';
while ($i < 10) {
($categoryid, $rreviewid, $rtitle, $rrating, $rurl, $rrelease, $rversion, $rscriptauthor, $rprice, $rreview, $rfooter, $rauthor, $rauthorname, $rdate, $rhits) = split(/\|/, $entries2);
print "<TABLE BORDER=0 WIDTH=420 CELLPADDING=1 CELLSPACING=0>";
print "<TR><TD>";
print "<font face=Arial size=2><b><a href=\"/index.cgi?category=$categoryid\&review=$rreviewid\">$rtitle</a></b> <img src=\"images/$rrating.gif\" alt=\"$rrating out of 10\"></font></td></tr><tr><td><font face=Arial size=1>Release Date: $rrelease - Version: $rversion - Price: $rprice </font>";
print "</TD></TR>";
print "</TABLE>";
$i++;
}}

i havent tested it, but it should woprk, just let me know if it doesnt.

as for the sorting part try this:
open (DATA2, "$home/dbs/allrev.db");
@fields = <DATA2>;
close (DATA2);
foreach $entries2 (@fields) {
($categoryid, $rreviewid, $rtitle, $rrating, $rurl, $rrelease, $rversion, $rscriptauthor, $rprice, $rreview, $rfooter, $rauthor, $rauthorname, $rdate, $rhits) = split(/\|/, $entries2);
$list = "$rhits $categoryid $rreviewid $rtitle $rrating $rurl $rrelease $rversion $rscriptauthor $rprice $rreview $rfooter $rauthor $rauthorname $rdate";
push(@sort_em_out, $list);
}
@sort_em_out = sort bynum @sort_em_out;
foreach $entries (@sort_em_out) {
($rhits, $categoryid, $rreviewid, $rtitle, $rrating, $rurl, $rrelease, $rversion, $rscriptauthor, $rprice, $rreview, $rfooter, $rauthor, $rauthorname, $rdate) = split(/\s/, $entries);
print "<TABLE BORDER=0 WIDTH=420 CELLPADDING=1 CELLSPACING=0>";
print "<TR><TD>";
print "<font face=Arial size=2><b><a href=\"/index.cgi?category=$categoryid\&review=$rreviewid\">$rtitle</a></b> <img src=\"images/$rrating.gif\" alt=\"$rrating out of 10\"></font></td></tr><tr><td><font face=Arial size=1>Release Date: $rrelease - Version: $rversion - Price: $rprice </font>";
print "</TD></TR>";
print "</TABLE>";

}

again this is untested, but it should work, if not let me know..

Randor


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives