Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Here's the deal, I'm just starting with perl basicly. What I'm doing now is putting together a very simple Guestbook. I have the full code writen, and it works fine! However I've ran into the problem of having to many entrys on one page. I'm using 'open(Dash)', how can set a max number per page, and then make it open a new page?

open(DASH,">>view.htm"); print "Content-Type: text/html\n\n"; if ($used eq "") { print <<EndOfCoding;

Can anyone help with this simple question?

Replies are listed 'Best First'.
Re: Max Submissions?
by Chady (Priest) on Sep 06, 2001 at 14:51 UTC

    If you cannot afford DataBasing the entries, I really think you should put the entries into a seperate flat text file, and then retrieve them upon request. This way you won't have a lot of html files to take care of, just a template file and the rest of the data databased.

    Now, using simple methods you can count your way through the text file and print the results, showing 1 - 10 - or even 100 entry per page.

    If you don't know what I'm talking about: here's some explanation:
    You have a template html file and you know where to print the results.
    You open the text file and get the entries you want and then print them out.
    now, whenever you need to change the look of the page, you have only one html file to change that does all the rest.

    I hope this helps you decide.


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/

      Building on the idea of flat files, one might be inclined to use DBD::CSV where a flat file gets the DBI treatment. I use this on hosts where I can't install a full database, but I need simple database access lest I be forced to code it all myself.

      Of course, moving from an HTML file to flat files isn't trivial if you're knee deep in data already, but the transition would easily pay for itself when the entries gets into the 1000s and you need to go to a full database solution because (suprise!) the client side is already done! Just change the dbd connect statement at the top to point to oracle, postgres, mysql, what-have-you, let out an evil cackle or two, then go to your next project =)

      -Ducky

Splitting stuff into many pages with back and next buttons - (Was: Max Submissions?)
by bjelli (Pilgrim) on Sep 06, 2001 at 16:51 UTC
    Perl won't do this for you automatically, you have to program it. I agree with chadys advice, storing the entries in a database of some sort would be better than just storing the html.

    anyways, here's what you can do with your version of the program:

    You need to keep track of when it's time to write a new file. That means you need a counter of some sort. You'll have to store the counter in a different file again, maybe counter.txt.

    open(C, "counter.txt") and $c = <C> and close C and $c++; open(C, ">counter.txt") and print C $c and close C;

    Let's say you want to start a new file every time the counter reaches a multiple of 10. You might call the files that contain the html "view0.htm" for the first 10 entries, "view1.htm" for the next 10, and so on.

    $fileno = int($c / 10); $filename = "view" . $fileno . ".htm"; open(DASH,">>$filename"); print DASH "print the entry here"; close DASH

    That's almost all. If you want to have Links from one page of the guestbook to the next, you have two options: do it in HTML, with frames (yuck, I hate frames) or write the links into the view*.htm files. Putting the "back" at the top of each page:

    if (int($c / 10) == $fileno ) { # this is the first time we write to this # file, because $c is 10 or 20 or 30 or ... # link back to the one before print DASH "<a href=view" . ($fileno-1) . ".htm>Back</a>"; }
    and the "Next" link at the bottom is probably the easiest:
    if ( $c % 10 == 9 ) { # this is the last time we write to this # file, because $c is 9 or 19 or 29 or 39 or ... # link on to the next one print DASH "<a href=view" . ($fileno+1) . ".htm>Next</a>"; }

    Assembling these code snippets in the right order is left as an exercise to the student ;-)

    --
    Brigitte    'I never met a chocolate I didnt like'    Jellinek
    http://www.horus.com/~bjelli/         http://perlwelt.horus.at
Re: Max Submissions?
by peschkaj (Pilgrim) on Sep 06, 2001 at 17:17 UTC

    I like Chady's method of counting through them: quick and easy solution. I implement all the pages on my website using a similar solution (templates + server side includes). Using an incremental counter also allows you to provide for a user variable, at a later date, that can be saved in a cookie.

    I could come up with pseudo code for this one, but hopefully someone else will have some good ideas for real code. If I don't see anything later on today, I could post it.

Re: Max Submissions?
by rapier1 (Novice) on Sep 10, 2001 at 20:06 UTC
    Okay, there are a couple ways to handle this. The database is a fine idea but probably overkill. The perl tradition is to do the minimum necessary to get the job done. So futzing with databases to handle a guestbook (unless its *insanely* huge) is antithetical to the nature of perl.

    The other way to handle it is quick and dirty but cheap and easy to do which is chady's idea of a 'flatfile' - or just a text file that contains the comments.

    Things to keep in mind. Unless you force it you cannot be assured that all of the comments are going to be on one line. It makes more sense to use a unique seperator between entries. From seperator to seperator counts as one entry. I ripped this out of another program of mine to show you how I did it.

    sub read_quotes { open (QUOTES, "< $basedir$quotefile") || die "No quotes file!" +; while (<QUOTES>) { chomp; if (!($_ eq $seperator)) { $build .= "$_\n"; } else { push(@quotes,$build); $build = "-------\n"; #seperate body from sig } } close QUOTES; }
    This builds an array of entries. Each entry will correspond to one guest book entry. Then, to print out a subset of them you use a very very simple for loop
    for ($i = $min; $i <= $max; $i++) { print $quotes[$i]; }
    $max and $min are the upper and lower bounds of the arrays section you want to print. For example, if you want to print 10 entries per pages the $min and $max for the 1st page would be 0 and 9. The 2nd page would be 10 and 19, 20 and 29 for the 3rd and so on. The max and min can be set with hidden values that are submitted when the user clicks on the 'next page' button.

    One note about the design of this... The simplest thing in the world would be to run this all from the cgi-bin directory in one script. The template Chady talks about should just be part of your perl code. Do something like this;

    script starts if no arguements print 1st page of guestbook exit else is arguement 'next page'? print page N build next page button with values for next page is argument 'previous page'? same as above but in reverse :) is argument 'add entry'? parse user entry and concatonate to end of guestbook file script ends
    Make use of the CGI modules if you can to save time especially with parsing the user data. Enjoy.