Gossamer Forum
Home : General : Perl Programming :

Text files, directories, back and next buttons.

(Page 1 of 2)
> >
Quote Reply
Text files, directories, back and next buttons.
Hi,

I have a directory on my server which has a couple of hundred text files in it.

Each file stores information like this field1|field2|field3>>>>>>>|field10

I've been trying to get a cgi script which print each files field1, field2, >>>>>field10 to the browser.

With there being so many files this would take a while so I'd like to use next and back buttons and only show the info from nine files at a time.

If anyone has some code that can do this I would truly appreciate it. All the code I had used on it so far either only printed the filename or the contents from the first file. As for the next and back buttons I have no idea where to start.

Hopefully someone has a solution. Thanks in advance

Appreciated.

A
<a href="http://andymoore.info/">Andy Moore</a>
<a href="http://officialmp3s.com/">Mp3 Downloads</a>
Quote Reply
Re: [andymoo] Text files, directories, back and next buttons. In reply to
Hi,

FileMan would be met your requirements

Cheers,

Cheers,

Dat

Programming and creating plugins and templates
Blog
Post deleted by andymoo In reply to
Quote Reply
Re: [andymoo] Text files, directories, back and next buttons. In reply to
Not really looking for a package that manages as much as the suggestion, more after something that does this bit and this alone for now.

Here's what I've managed to stick together and get working since I first posted.

#!/usr/bin/perl
print "Content-type: text/html\n\n";

$directory = '/pathtodata';

opendir (DIR, $directory) || die "Couldn't read $directory: $!\n";
@fileslist = grep { /.txt/ } readdir(DIR);
closedir(DIR);

foreach $file(sort sortit @fileslist) {

open(INF,"$directory/$file") or dienice("Can't open $directory/$file: $! \n");
@items = <INF>;
close(INF);

foreach $i (@items) {
chomp($i);
($title,$actors,$desc,$aprt,$price,$id,$catagory) = split(/\|/,$i);

print "Title = $title Actors = $actors Description = $desc Approx run time = $aprt Price = $price ID = $id Catagory = $catagory\n";

}

sub sortit {
@a = split(/\|/,$a);
@b = split(/\|/,$b);
$a[0] cmp $b[0];
}

print "<hr>\n";
}

Now off in search of the way to limit it to nine items a page, plus do the back and next buttons. Coffee first tho me thinks!
<a href="http://andymoore.info/">Andy Moore</a>
<a href="http://officialmp3s.com/">Mp3 Downloads</a>
Quote Reply
Re: [andymoo] Text files, directories, back and next buttons. In reply to
Attached is a something I whipped together that does the job. it could probably be made a bit shorter with a little hacking.

--Philip
Links 2.0 moderator
Quote Reply
Re: [King Junko II] Text files, directories, back and next buttons. In reply to
let me try that again :-)

--Philip
Links 2.0 moderator
Quote Reply
Re: [King Junko II] Text files, directories, back and next buttons. In reply to
Thank you very much!

That is supreme, you've just made my morning.

Works spot on!!

Smile
<a href="http://andymoore.info/">Andy Moore</a>
<a href="http://officialmp3s.com/">Mp3 Downloads</a>
Quote Reply
Re: [andymoo] Text files, directories, back and next buttons. In reply to
is the spanning working correctly? I only tried on a limit of 2 files, and the spanning didn't work right when I changed it.

--Philip
Links 2.0 moderator
Quote Reply
Re: [King Junko II] Text files, directories, back and next buttons. In reply to
Yes, the spanning is working fine. I have just tried it with 12 files and everything works.

One question though if I may.

Instead of printing all the info from each with by using:

print join "; ", map { "$_ = $rec{$_}" } keys %rec;

How do I call the elements from those fields in the following way, $title $actors and so on.

I guess I'm looking for better control of the output. More like:

print "Title = <a href=\"$title.html\">$title</a><br>Actors = $actors.........";

I appreciate your help on this.

Thanks

A
<a href="http://andymoore.info/">Andy Moore</a>
<a href="http://officialmp3s.com/">Mp3 Downloads</a>
Quote Reply
Re: [andymoo] Text files, directories, back and next buttons. In reply to
actually the spanning is buggy... I fixed it and I'll post it shortly.
Code:
print "Title = <a href=\"$rec{title}.html\">$rec{title}</a><br>Actors = $rec{actors}.........";

--Philip
Links 2.0 moderator
Quote Reply
Re: [King Junko II] Text files, directories, back and next buttons. In reply to
QUALITY, THANK YOU!

Did I shout, I meant to!

Cheers

A
<a href="http://andymoore.info/">Andy Moore</a>
<a href="http://officialmp3s.com/">Mp3 Downloads</a>
Quote Reply
Re: [andymoo] Text files, directories, back and next buttons. In reply to
here you go...

--Philip
Links 2.0 moderator
Quote Reply
Re: [King Junko II] Text files, directories, back and next buttons. In reply to
That's amazing!

How hard would it be to change it from a next and back link to a link for every page made up from nine items.

In the same kind of way as this forum does.

Thank you very big your majesty.......

Wink
<a href="http://andymoore.info/">Andy Moore</a>
<a href="http://officialmp3s.com/">Mp3 Downloads</a>
Quote Reply
Re: [andymoo] Text files, directories, back and next buttons. In reply to
do you mean this?
Code:
<< < 1 2 3 4 [5] 6 7 8 9 > >>

If so that's a real pain in the butt to make but I've already written codes for that in my search program.

--Philip
Links 2.0 moderator
Quote Reply
Re: [King Junko II] Text files, directories, back and next buttons. In reply to
Yes, that is just what I meant!!

Thanks

A
<a href="http://andymoore.info/">Andy Moore</a>
<a href="http://officialmp3s.com/">Mp3 Downloads</a>
Quote Reply
Re: [andymoo] Text files, directories, back and next buttons. In reply to
There are probably better span codes around the forum.... but here's a quick hack of what I'm using...

in the script, find the two IF statements that create the span links, and replace it with
Code:
my $span = span(scalar @files);
print $span;

Then at the bottom of the script, add:
Code:
sub span {
my $span;
my $limit = 8;
my $hits = shift;
my $max = 9;
($page =~ /\d+$/) or ($page = 1);
my $total = int(($hits/$max) + 1);
die "invalid page" if ($page > $total);

my $start = ($page > 4) ? ($page - 4) : (1);
my $finish = ($page + 4 > $total) ? ($total) : ($page + 4);
my $i = ($finish - $start);

if (($i < $limit) and ($limit < $total)) {
($finish - $page < $page - $start) ? ($start += $i - $limit) : ($finish += $limit - $i)
}

if ($page) {
$span .= qq|<a href="files.cgi?pg=1">&lt;&lt;</a> |;
$span .= " <a href='files.cgi?pg=" . ($page) . "'>&lt;</a> ";
}
else {
$span .= qq|&lt;&lt; |;
$span .= " &lt; ";
}

for ($start .. $finish) {
if ($_ == ($page + 1)){
$span .= "[$_]";
}
else {
$span .= qq| <a href="files.cgi?pg=$_">$_</a> |;
}
}

if ($finish != $total) {
$span .= " <a href='files.cgi?pg=" . ($finish + 1) . "'>&gt;</a>";
$span .= qq| <a href="files.cgi?pg=$total">&gt;&gt;</a>|;
}
else {
$span .= " &gt; ";
$span .= qq|&gt;&gt;|;
}
return $span;
}

--Philip
Links 2.0 moderator
Quote Reply
Re: [King Junko II] Text files, directories, back and next buttons. In reply to
>>($page =~ /\d+$/) or ($page = 1); <<

$page doesn't seem to be defined anywhere.



___________________

>>my $span = span(scalar @files); <<

This does the same as scalar:

my $span = span(~~ @files);
___________________

Thats just a useless bit of info :)

Last edited by:

Paul: Mar 19, 2002, 1:05 PM
Quote Reply
Re: [Paul] Text files, directories, back and next buttons. In reply to
actually it is defined in the script attached earlier... it's also redundant code...

--Philip
Links 2.0 moderator
Quote Reply
Re: [King Junko II] Text files, directories, back and next buttons. In reply to
this code "should" be slightly better....
Code:
sub span {
my $span;
my $limit = 8;
my $hits = shift;
my $max = 9;
my $curr = ($page > 1) ? ($page + 1) : 1;
my $total = int(($hits/$max) + 1);
die "invalid page" if ($curr > $total);

my $start = ($curr > 4) ? ($curr - 4) : (1);
my $finish = ($curr + 4 > $total) ? ($total) : ($curr + 4);
my $i = ($finish - $start);

if (($i < $limit) and ($limit < $total)) {
($finish - $curr < $curr - $start) ? ($start += $i - $limit) : ($finish += $limit - $i)
}

if ($page > 0) {
my $prev = ($curr - 1 > 0) ? ($curr - 1) : 1;
$span .= qq|<a href="files.cgi?pg=1">&lt;&lt;</a> |;
$span .= qq|<a href="files.cgi?pg=$prev">&lt;</a> |;
}
else {
$span .= qq|&lt;&lt; |;
$span .= " &lt; ";
}

for ($start .. $finish) {
if ($_ == $page + 1){
$span .= "[$_]";
}
else {
$span .= qq| <a href="files.cgi?pg=$_">$_</a> |;
}
}

if ($curr != $total) {
my $next = $curr + 1;
$span .= " <a href='files.cgi?pg=$next'>&gt;</a>";
$span .= qq| <a href="files.cgi?pg=$total">&gt;&gt;</a>|;
}
else {
$span .= " &gt; ";
$span .= qq|&gt;&gt;|;
}
return $span;
}

--Philip
Links 2.0 moderator
Quote Reply
Re: [King Junko II] Text files, directories, back and next buttons. In reply to
And here's a really awful one which I adapted when I was using text based files. This is part of my hillarious other code I posted in another forum not-so-long ago :-)

Code:
$number_of_sites = (@results);

$result_count = $number_of_sites -1;

$pgsz = 5;

if ($result_count != 0) {
$pagecount = int($result_count / $pgsz);
if (($pagecount * $pgsz) != $result_count) {
$pagecount++;
}
}

if (!$A::rqpg) {
$rqpg = 1;
}
else {
$rqpg = $A::rqpg;
}

$firstresult = (($rqpg - 1) * $pgsz) + 1;
$lastresult = $firstresult + $pgsz - 1;
if ($lastresult > $result_count) {
$lastresult = $result_count;
}

$prev_page = $rqpg - 1;
$next_page = $rqpg + 1;
if ($rqpg == 1) {
$prev_link = "";
} else {
$prev_link = " <a href=\"/cgi-bin/en-ks5-pg.cgi?ks=$A::ks&pg=$A::pg&cat=$A::cat&id=$A::id&rqpg=$prev_page&pgsz=$pgsz\">" . "Previous Page" . "</a> &nbsp; ";
}
if ($rqpg == $pagecount) {
$next_link = "";
} else {
$next_link = "&nbsp; <a href=\"/cgi-bin/en-ks5-pg.cgi?ks=$A::ks&pg=$A::pg&cat=$A::cat&id=$A::id&rqpg=$next_page&pgsz=$pgsz\">" . "Next Page" . "</a>";
}

print qq|
Displaying <B>$firstresult</B>-<B>$lastresult</B> of <B>$result_count</B> $wording in this category. <BR><BR>
|;

if ($pagecount > 1) {
$pagelinks = $prev_link;
$pageno = 0;
while ($pageno < $pagecount) {
$pageno++;
if ($pageno == $rqpg) {
$thislink = " <b>$pageno</b> &nbsp;";
} else {
$thislink = " <a href=\"/cgi-bin/en-ks5-pg.cgi?ks=$A::ks&pg=$A::pg&cat=$A::cat&id=$A::id&rqpg=$pageno&pgsz=$pgsz\">" . $pageno . "</a> &nbsp;";
}
$pagelinks = $pagelinks . $thislink;
}
$pagelinks = $pagelinks . " " . $next_link;
} else {
$pagelinks = "";
}

# show records

foreach $found (@results[$firstresult .. $lastresult]) {

# ...

}

# print links

print "$pagelinks";

- wil
Quote Reply
Re: [Wil] Text files, directories, back and next buttons. In reply to
How's this Wil?

Code:
sub span {
my ($hits, $limit, $max, $span) = (shift, 8, $per_page, "");
my $half = $limit / 2;
my $total = int(($hits/$max) + 1);
die "invalid page" if ($page > $total);
my $start = ($page > $half) ? ($page - $half) : (1);
my $finish = ($page + $half > $total) ? ($total) : ($page + $half);
my $i = ($finish - $start);
(($finish - $page < $page - $start) ? ($start += $i - $limit) : ($finish += $limit - $i)) if (($i < $limit) and ($limit < $total));
$span .= ($page != 1) ? &link(1, "&lt;&lt;") . &link(($page - 1 or 1), "&lt;") : qq|&lt;&lt; &lt; |;
($span .= ($_ == $page) ? "[$_] " : &link($_)) for ($start .. $finish);
$span .= ($page != $total) ? &link($page + 1, "&gt;", "next") . &link($total, "&gt;&gt;") : qq|&gt; &gt;&gt; |;
return $span;
}

sub link {
my ($p, $d) = @_;
$d ||= $p;
return qq|<a href="query.cgi?$string&pg=$p">$d</a> |;
}

(note that this code is from my search engine and won't work in the file browsing script posted earlier)

--Philip
Links 2.0 moderator

Last edited by:

King Junko II: Mar 20, 2002, 3:09 AM
Quote Reply
Re: [King Junko II] Text files, directories, back and next buttons. In reply to
Hi Philip

Thanks for that. I don't actually need code to be honest - that code snippet I provided was a very old one that I wrote for some text-file database I have.

I've got a much sleeker version now that works off my SQL database.

Thanks, anyway! Look good!

- wil
Quote Reply
Re: [Wil] Text files, directories, back and next buttons. In reply to
Quote:
I don't actually need code to be honest

I know. I was just showing off Tongue I'm sure someone will come along and make me look like a fool but an ego trip is nice now and again.

--Philip
Links 2.0 moderator
Quote Reply
Re: [King Junko II] Text files, directories, back and next buttons. In reply to
>>I'm sure someone will come along and make me look like a fool <<

Well I guess I could if I wanted but my code is much to good to distribute amongst you commoners but you can look if you want :)

http://213.106.15.150/cgi-bin/test.cgi?page=1

Code:
print span({ per_page => 5,
total => 100,
page => (param('page') || 1),
spread => 11,
url => 'test.cgi' });

Last edited by:

Paul: Mar 20, 2002, 3:44 AM
Quote Reply
Re: [Paul] Text files, directories, back and next buttons. In reply to
Nah. Use graphics as your links, like I do ;-) Then I'd be impressed <g>. I believe there are a few spare going on the GT forum somewhere ;-)

- wil
> >