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: Intermediate:
Perl Guestbook Page Navigation

 



cass84
Novice

Oct 31, 2005, 9:42 PM

Post #1 of 13 (1680 views)
Perl Guestbook Page Navigation Can't Post

I've created a perl guestbook script. I am attempting to customize it to where only 10 entries show per page and the rest of the entries are shown through page navigation. Like with the Next/Previous links showing 10 entries per page. I can't figure it out tho. Here is my code


Code
#!/usr/bin/perl -w  

$data_file = '/cgi-bin/data/guestbook.txt';

$max_entries = 10; # how many guestbook entries to save?

# set to '0' (zero) for infinite entries...

use CGI;

use Fcntl;

$query = new CGI;

$date_command = '/bin/date';

$date = `$date_command +"%A, %B %e, %Y at %I:%M %p"`; chop($date);

$smilies_url =

"http://smilies";

unless ($action = $query->param('action')) {

$action = 'none';

}

print <<"EndOfText";

Content-type: text/html

<html><head><title>page</title></head><body>

<p><font size="3">To Post A Comment Or Offer Your Support Click <A HREF="#form"><i><b>HERE</b></i></a> to fill out the form!</font></b>

<br><font size="2">HTML is ENABLED!</font></center>

<HR>

<!-- END HTML CODE BEFORE FORM -->

EndOfText

if ($action eq 'Be Heard') {

# process the form submission and assemble the guestbook entry

$name = $query->param('name');

$email = $query->param('email');

$url = $query->param('url');

$city = $query->param('city');

$state = $query->param('state');

$country = $query->param('country');

$comment = $query->param('comment');

# clean up $name

if (length($name) > 50) {

$name = 'A Really Long Name . . . ';

}

# clean up and adjust $from_where

$from_where = "$city, $state, $country";

$from_where =~ s/, , /, /; # remove duplicate ', '

$from_where =~ s/^, //; # remove initial ', '

$from_where =~ s/, $//; # remove final ', '

if ($from_where =~ /^[,\s]+$/) {

# commas and whitespace

$from_where = 'parts unknown';

}



if (length($from_where) > 75) {

$from_where = 'A Place With A Really Long Name . . .';

}

# clean up and adjust $comment

if (length($comment) > 32768) {

$comment = 'The Comment That Was Posted Was Too Long.';

}

# fix line-endings

$comment =~ s/\r\n?/\n/g;

#clean up $URL

unless ($url) {

$url = 'No Website';

}

# Smilies

$comment =~ s/\/<img src="$smilies_url\/angel.gif">/sg;

$comment =~ s/\:bow:/<img src="$smilies_url\/bow.gif">/sg;

$comment =~ s/\:clap:/<img src="$smilies_url\/clap.gif">/sg;

$comment =~ s/\/<img src="$smilies_url\/cool.gif">/sg;

$comment =~ s/\:crazy:/<img src="$smilies_url\/crazy.gif">/sg;

$comment =~ s/\/<img src="$smilies_url\/cry.gif">/sg;

$comment =~ s/\:cry2:/<img src="$smilies_url\/cry2.gif">/sg;

$comment =~ s/\:dork:/<img src="$smilies_url\/dork.gif">/sg;

$comment =~ s/\/<img src="$smilies_url\/evil.gif">/sg;

$comment =~ s/\:flag:/<img src="$smilies_url\/flag.gif">/sg;

$comment =~ s/\:giggle:/<img src="$smilies_url\/giggle.gif">/sg;

$comment =~ s/\:grin:/<img src="$smilies_url\/grin.gif">/sg;

$comment =~ s/\:hbreak:/<img src="$smilies_url\/hbreak.gif">/sg;

$comment =~ s/\:hug:/<img src="$smilies_url\/hug.gif">/sg;

$comment =~ s/\:idea:/<img src="$smilies_url\/idea.gif">/sg;

$comment =~ s/\/<img src="$smilies_url\/laugh.gif">/sg;

$comment =~ s/\:lost:/<img src="$smilies_url\/lost.gif">/sg;

$comment =~ s/\:love:/<img src="$smilies_url\/love.gif">/sg;

$comment =~ s/\/<img src="$smilies_url\/mad.gif">/sg;

$comment =~ s/\:nervous:/<img src="$smilies_url\/nervous.gif">/sg;

$comment =~ s/\:no:/<img src="$smilies_url\/no.gif">/sg;

$comment =~ s/\:nono:/<img src="$smilies_url\/nono.gif">/sg;

$comment =~ s/\hno:/<img src="$smilies_url\/ohno.gif">/sg;

$comment =~ s/\mg:/<img src="$smilies_url\/omg.gif">/sg;

$comment =~ s/\:party:/<img src="$smilies_url\/party.gif">/sg;

$comment =~ s/\:peace:/<img src="$smilies_url\/peace.gif">/sg;

$comment =~ s/\:please:/<img src="$smilies_url\/please.gif">/sg;

$comment =~ s/\:quiet:/<img src="$smilies_url\/quiet.gif">/sg;

$comment =~ s/\/<img src="$smilies_url\/rofl.gif">/sg;

$comment =~ s/\:rose:/<img src="$smilies_url\/rose.gif">/sg;

$comment =~ s/\:sad:/<img src="$smilies_url\/sad.gif">/sg;

$comment =~ s/\:sigh:/<img src="$smilies_url\/sigh.gif">/sg;

$comment =~ s/\:smile:/<img src="$smilies_url\/smile.gif">/sg;

$comment =~ s/\:stop:/<img src="$smilies_url\/stop.gif">/sg;

$comment =~ s/\:thanku:/<img src="$smilies_url\/thanku.gif">/sg;

$comment =~ s/\:think:/<img src="$smilies_url\/think.gif">/sg;

$comment =~ s/\:whatever:/<img src="$smilies_url\/whatever.gif">/sg;

$comment =~ s/\:wink:/<img src="$smilies_url\/wink.gif">/sg;

$comment =~ s/\:yap:/<img src="$smilies_url\/yap.gif">/sg;

$comment =~ s/\:yay:/<img src="$smilies_url\/yay.gif">/sg;

# assemble finished guestbook entry

$entry = <<"EndOfText";

<P><strong><emp>Posted on $date</emp></strong>

<P><strong>Name:</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $name

<br> <strong>Email:</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A HREF=\"<A href="mailto:$email\">$email</A">mailto:$email\">$email</A>

<br><strong>My URL:</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<A target="_blank" HREF=http://$url>$url</a>

<br><strong>Location:</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $from_where

<br><strong>Comments:</strong>

<BR><center><div style="width:500px">$comment</div></center></P>

<HR>

EndOfText

# Read old entries, write out new

$all_entries = $entry;

sysopen(ENTRIES, "$data_file", O_RDWR)

or die "can't open $data_file: $!";

flock(ENTRIES, 2) or die "can't LOCK_EX $data_file: $!";

while(<ENTRIES>) {

$all_entries .= $_;

}

if ($max_entries) {

# lop the tail off the guestbook, if necessary

@all_entries = split(/<HR>/i, $all_entries);

$entry_count = @all_entries - 1;

while ($entry_count > $max_entries) {

pop @all_entries;

$entry_count = @all_entries - 1;

}

$all_entries = join('<HR>', @all_entries);

}



# now write out to $data_file

seek(ENTRIES, 0, 0) or die "can't rewind $data_file: $!";

truncate(ENTRIES, 0) or die "can't truncate $data_file: $!";

print ENTRIES $all_entries or die "can't print to $data_file: $!";

close(ENTRIES) or die "can't close $data_file: $!";

}

# display the guestbook

open (IN, "$data_file") or die "Can't open $data_file for reading: $!";

flock(IN, 1) or die "Can't get LOCK_SH on $data_file: $!";

while (<IN>) {

print;

}

close IN or die "Can't close $data_file: $!";

# display the form

print <<"EndOfText";

<!-- BEGIN HTML CODE FOR FORM AND AFTER -->

<A NAME="form"><H2>Submit Your Voice!</H2></A>

<FORM NAME="gbook" METHOD="POST" ACTION="/cgi-bin/guestbook.cgi">

<TABLE>

<TR>

<TD ALIGN="right"><STRONG>Name:</STRONG></TD>

<TD><INPUT NAME="name" SIZE=30></TD>

</TR>

<TR>

<TD ALIGN="right"><STRONG>Email:</STRONG></TD>

<TD><INPUT NAME="email" SIZE=30></TD>

</TR>

<TR>

<TD ALIGN="right"><STRONG>My URL:</STRONG><br>(www.domain.com)</TD>

<TD><INPUT NAME="url" SIZE=30></TD>

</TR>

<TR>

<TD ALIGN="right"><STRONG>City:</STRONG></TD>

<TD><INPUT NAME="city" SIZE=30></TD>

</TR>

<TR>

<TD ALIGN="right"><STRONG>State:</STRONG></TD>

<TD><INPUT NAME="state" SIZE=30></TD>

</TR>

<TR>

<TD ALIGN="right"><STRONG>Country:</STRONG></TD>

<TD><INPUT NAME="country" SIZE=30 value="USA"></TD>

</TR>

<TR>

<TD ALIGN="right"><STRONG>Comment:</STRONG></TD>

<TD>

<TEXTAREA NAME="comment" ROWS=5 COLS=55 WRAP="virtual"></TEXTAREA>

</TD>

</TR>

<TR><TD COLSPAN=2> </TD></TR>

<TR>

<TD> </TD>

<TD><INPUT TYPE="submit" NAME="action" VALUE="Be Heard" onsubmit="return validate();"></TD>

</TR>

</TABLE>

</FORM>



<!-- BEGIN JAVASCRIPT VALIDATION -->

<script language="JavaScript">

<!--

document.gbook.onsubmit=validate;

function validate() {



var name=document.gbook.name.value;

if( (name.length==0) ) {



alert("Please Enter A Name.");

document.gbook.name.focus();

document.gbook.name.select();

return false;

}



var city=document.gbook.city.value;

if( (city.length==0) ) {



alert("Please Enter A City.");

document.gbook.city.focus();

document.gbook.city.select();

return false;

}

var state=document.gbook.state.value;

if( (state.length==0) ) {



alert("Please Enter A State.");

document.gbook.state.focus();

document.gbook.state.select();

return false;

}

var comment=document.gbook.comment.value;

if( (comment.length==0) ) {



alert("Please Enter A Comment.");

document.gbook.comment.focus();

document.gbook.comment.select();

return false;

}

var email=document.gbook.email.value;

if( (email.length<5) ||

(countchar(email," ")>0) ||

(countchar(email,"@")!=1) ||

(countchar(email,".")==0) ) {



alert("Invalid email format.");

document.gbook.email.focus();

document.gbook.email.select();

return false;

}



return true;

}



//-->

</script>

<!-- END JAVASCRIPT VALIDATION -->

</body>

</html>



EndOfText



davorg
Thaumaturge / Moderator

Nov 1, 2005, 5:26 AM

Post #2 of 13 (1676 views)
Re: [cass84] Perl Guestbook Page Navigation [In reply to] Can't Post

You should take a look at Data::Page.

Also, please don't use the "date" command to get the date. Every time you use an external command you make your program slower and less portable. The 'strftime' function from the POSIX module is just as simple to use and keeps everything internal to your Perl program.


Code
use POSIX 'strftime'; 

# then later
$date = strftime '%A, %B %e, %Y at %I:%M %P', localtime;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


cass84
Novice

Nov 1, 2005, 7:00 AM

Post #3 of 13 (1674 views)
Re: [davorg] Perl Guestbook Page Navigation [In reply to] Can't Post

thanks im going to take a look at this and try to figure it out


cass84
Novice

Nov 1, 2005, 1:46 PM

Post #4 of 13 (1669 views)
Re: [davorg] Perl Guestbook Page Navigation [In reply to] Can't Post

I tried to figure out this module thing but I just dont get it. can anyone offer anymore help? DO i just copy and paste the synopsis into the code? I don't understand . . .


rork
User

Nov 2, 2005, 6:55 AM

Post #5 of 13 (1664 views)
Re: [cass84] Perl Guestbook Page Navigation [In reply to] Can't Post

You should learn how to work around with perldoc, the examples usually are a good help but you can use a module better if you understand what the methods do. Read it carefully, everything you need to know is in it.

Edit: I made it a little more educational.
--
Don't reinvent the wheel, use it, abuse it or hack it.

(This post was edited by rork on Nov 2, 2005, 9:14 AM)


cass84
Novice

Nov 2, 2005, 9:20 AM

Post #6 of 13 (1655 views)
Re: [rork] Perl Guestbook Page Navigation [In reply to] Can't Post

dang i refresh my page and the help is gone . . . so no one is going to help me? . . . so assuming i was just given a page with a module i guess i just have to copy all the codes on that page into my code . . .


(This post was edited by cass84 on Nov 2, 2005, 9:23 AM)


rork
User

Nov 2, 2005, 9:38 AM

Post #7 of 13 (1651 views)
Re: [cass84] Perl Guestbook Page Navigation [In reply to] Can't Post

It's not that I don't want to help you, but when it's for school I won't give you any code.

That you were given a page with a module means that another programmer did some work for you, it will save you some time and could save you a mistake in your code. You should take a look at it and see how you can implement it into your code. I've seen worse documentation then this one, or documentation I don't understand for I know nothing about the things the module is working with (like Mime::).
--
Don't reinvent the wheel, use it, abuse it or hack it.


cass84
Novice

Nov 2, 2005, 10:47 AM

Post #8 of 13 (1648 views)
Re: [rork] Perl Guestbook Page Navigation [In reply to] Can't Post

yea i see what you're saying its just hard cuz i'm a beginner and i am trying to learn. so finding this module in the first place would have been difficult for me and when i got it i didn't understand it anyway thats why i asked about the synopsis on that page cuz i didnt get it so i asked for help.


rork
User

Nov 2, 2005, 11:11 AM

Post #9 of 13 (1646 views)
Re: [cass84] Perl Guestbook Page Navigation [In reply to] Can't Post

So you don't know the basics of using module's. I'll try to explain.

First you need a variable to refer to the module in this case it's $page, for the CGI module it's $query. You 'connect' it to the module with new:

Code
my $page = Data::Page->new('some arguments'); 
#or
my $page = new Data::Page;

In the last way you have to set the arguments later.

Then you can use the methods that are given in the documentation like you do with param at the CGI module

Code
   $page->method(argument);

you can look in the documentation what a method will return and which arguments it takes. Also the name of most methods will tell you what it does.

It's not just copy paste, it's more like looking up what methods you need, what arguments they require and what they return.
--
Don't reinvent the wheel, use it, abuse it or hack it.


davorg
Thaumaturge / Moderator

Nov 3, 2005, 5:04 AM

Post #10 of 13 (1642 views)
Re: [rork] Perl Guestbook Page Navigation [In reply to] Can't Post


In Reply To

Code
my $page = Data::Page->new('some arguments'); 
#or
my $page = new Data::Page;

In the last way you have to set the arguments later.


Actually, that's not necessarily true as you can use


Code
my $page = new Data::Page('some arguments');


But please don't recommend that people use the "new Foo" syntax as it can lead to really hard to find bugs. The "Foo->new" doesn't have any of these problems and should usually be preferred.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


davorg
Thaumaturge / Moderator

Nov 3, 2005, 5:21 AM

Post #11 of 13 (1641 views)
Re: [cass84] Perl Guestbook Page Navigation [In reply to] Can't Post

The Synopsis gives examples of how you might use the code. You need to adapt that code to your specific needs. Reading the rest of the documentation will help you do that.

Data::Page is based round the idea that you have a number of items in an array and that you only want to display a certain number of items from that array on each page. You'll probably also want to have links to the previous and next pages (and perhaps the first and last pages).

So let's assume that you have a list of entries in @entries. You'll need to populate that array yourself. Perhaps it comes from a database. In your case, I think it comes from a text file.

The next thing you'll need to know is which page you want displayed. So let's add a "page" parameter to your CGI program. If no page is given then we'll default to the first page (page 1).


Code
# this assumes we're using "use CGI ':standard';" 
my $curr_page = param('page') || 1;


Now we have all the information we need to create our paging object:


Code
use Data::Page; 

my $page = Data::Page->new;
# tell it the total number of entries we have
$page->total_entries(scalar @entries);
# tell it how many items we want on a page
$page->entries_per_page(10);
# tell it the current page number
$page->current_page($curr_page);


Now the page object can tell us interesting things about the items that should be on the current page. The most interesting will be the indexes into our @entries array of the first and last items on the page. We can use this to display all of the items on this page.


Code
foreach my $item ($page->first .. $page->last) { 
my $entry = $entries[$item];
# do whatever you need to do in order to display $entry
}


The other thing you can do it to get the numbers of the previous and next pages. In this example, we can use them to build links to those pages.


Code
my $prev = $page->previous_page; 
my $next = $page->next_page;

print qq(<a href="this_script.pl?page=$prev">prev</a>);
print qq(<a href="this_script.pl?page=$next">next</a>);


You can use similar code to creat links to the first and last pages.

I think you'll find that the page object will return 0 (or maybe "undef") for pages that don't exist. For example, if you're on the first page then there isn't a previous page to go to. So you can use that information to only print those links when they are needed.


Code
if ($prev) { 
print qq(<a href="this_script.pl?page=$prev">prev</a>);
}


All of this is described in the documentation. It's just a case of getting into the right mindset to read those docs.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


cass84
Novice

Nov 3, 2005, 6:02 PM

Post #12 of 13 (1632 views)
Re: [davorg] Perl Guestbook Page Navigation [In reply to] Can't Post

well i attempted to use this script but i got this error when trying to compile



Can't locate Data/Page.pm in @INC (@INC contains: /usr/lib/perl5/5.8.5/i386-linux-thread-multi /usr/lib/perl5/5.8.5 /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.4/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.3/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl/5.8.4 /usr/lib/perl5/site_perl/5.8.3 /usr/lib/perl5/site_perl/5.8.2 /usr/lib/perl5/site_perl/5.8.1 /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.4/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.3/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl/5.8.4 /usr/lib/perl5/vendor_perl/5.8.3 /usr/lib/perl5/vendor_perl/5.8.2 /usr/lib/perl5/vendor_perl/5.8.1 /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl .) at guestbook.cgi line 20.
BEGIN failed--compilation aborted at guestbook.cgi line 20.


davorg
Thaumaturge / Moderator

Nov 4, 2005, 12:33 AM

Post #13 of 13 (1630 views)
Re: [cass84] Perl Guestbook Page Navigation [In reply to] Can't Post

Looks to me like Data::Page either isn't installed or is installed in the wrong place. It's not a standard part of Perl, you need to install it.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


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

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