Thanks.  I'll work on it.

Wiggins d'Anconia <[EMAIL PROTECTED]> wrote:A L wrote:
> Hello [EMAIL PROTECTED],
> This continuation post is from another post at [EMAIL PROTECTED]; it's posted here 
> because this is more relevant to CGI. Previously, I had 2 questions, one being 
> pagination (the other question is yet addressed until pagination issue is 
> understood). Now, after following great advices from [EMAIL PROTECTED], I've looked 
> up SQL's Limit and Offset concept, which I was able to understand-demonstrate by 
> only getting first 20 rows of the selections from mysql database. However, when I 
> tried to implement the rest of the rows, I got stuck as to how to approach what I 
> wanted to do, which is to make a result that is similar to google search shown on 
> the web. Then, I searched more on the web about pagination without restricting my 
> search to beginner and perl. Luckily, I found a lot more information and tried to 
> use some of the concepts which were given. Previously, Jeff 'japhy' Pinyan had give 
> me some script (thanks, Jeff) that he used to do the similar thing that I'm trying t
o do, but
> I couldn't get all of his codes (ones near the end of his script). Therefore, I had 
> yet ventured the web again to better understand what I'm trying to do. Yet, I have 
> no luck in solving my issue with pagination. In the end, I even used Jeff's code, 
> only changing what is necessary (I think), to do see if it would work, and it 
> didn't. I'm beginning to feel that I'm missing some fundamental concept of cgi, but 
> I don't know what. Can any one of you tell me what that may be by looking at my 
> script? Also, if possible, what is wrong with my code to get it to work? Or, better 
> yet, if it's really bad, could you show me how you would do it, as Jeff did, to see 
> if I can get better sense of what I'm trying to do? Or, if I'm being really dumb 
> that I can't get this? Currently, I'm going around a circle by testing my code each 
> time I change to the script to see what it would do when it doesn't seem to change 
> anything. Thanks for your help or any input that may help me get the id
ea of what
> I can do to get the pagination. Following is three of my numerous scripts that had 
> been tested.
> Angela 



Ok, let's step back from the problem for a bit and look at what needs to 
actually happen. I only caught the tail end of the previous discussion 
so don't know what all has been stated.

First of all you should be using 'use strict' at the top of all of your 
code, this should be the first step of fixing any of your problems, when 
you have mastered that then the help will be more forth coming. 
Secondly, Perl ignores whitespace and text formatting for a reason, a 
*very* good one at that, so use it judiciously. I didn't even attempt to 
read your code, not because it was long, or incorrect, etc. I didn't 
read it because it was crammed together without any discernible breaks 
in the code, comments, or indenting. Finally half of the code you gave 
us was related to table formatting, etc. which while it may be important 
to you and your users only serves to clutter the code you have posted 
and hide the problems you are having. Now having said that...

Generally when I want to do pagination of a result set I need to know 3 
things:

1) the total # of records possible,
2) my current position (0 if not provided),
3) the number to display (some default if not provided)

>From these I can easily determine the next, previous, and number of 
possible pages of results. Now, the current position and the number to 
display I generally pass from my form or link to the script creating the 
result set, so they are hard set. To find the total number I build a 
where clause independent of any particular statement, that where clause 
can then be used in two statements, the first which selects just a 
"COUNT(*) AS total" from the table using the where clause to give me the 
total rows in my query. Then (assuming there are results) I issue a 
second select that retrieves my actual display data using the 
offset/limit that is built from the current position and the number to 
display. To display my result I step through all of the results from 
the second select. From the total, position, and display # I can 
calculate my 'next/previous' positions. Position twiddling is really all 
you need to do, then I take the current URL and "edit" it swapping out 
the current position with my next or previous position to create links 
to those destinations. Because I know all of my goodies are passed in 
the url still, the script can be generic.

So the next steps are getting your script so that it can get the 3 
parameters above, using those three parameters to generate the positions 
you want, using those positions to generate links that will call the 
script in the same manner with only changing the starting position.

To help with that I am going to post a very old subroutine that I used 
in the past to do this sort of thing, it takes 4 arguments, a bool of 
whether or not to display "clear" links when a link is not needed (set 
to true until you understand what I mean), the total rows, the number to 
display, and the current position. It returns a next, previous, first, 
and last link, followed by the display starting number and the display 
ending number. There is one subroutine missing, 'EditQueryString' you 
will have to write that yourself, it merely takes the current URL, one 
parameter and its value and changes the url to either include that 
parameter or update the parameter's value and then returns the new URL. 
Since it is no longer in production I make no guarantees, but it 
*should* work....

Try to incorporate this into what you have then come back with more 
specific questions. Clean up your code, develop a formatting style and 
use it when posting, it will make figuring out the troubles *much* easier.

http://danconia.org

-- UNTESTED --
sub BuildNextPrevLinks {
my ($blankon,$total,$numdisplay,$position) = @_;
my ($startno,$endno,$prevstring,$nextstring,$firststring,$laststring);


return unless ($total || $blankon);
$numdisplay=15 unless $numdisplay;


# calculations need to be based on total found not our display total
if ($total > $numdisplay) {
$startno = $position+1;
$endno = $startno + ($numdisplay - 1);
$endno = $total if ($endno > $total);
}
else {
# when we have less than the number of rows passed in
$startno = 1;
$endno = $total;
}


if ($position>=$numdisplay) {
my $newquerystring;
my $prevstart = $position - $numdisplay;


my $prevlink = &EditQueryString('position',$prevstart);
$prevstring="Previous $numdisplay";


my $firstlink = &EditQueryString('position',0);
$firststring="First $numdisplay";
}
else {
$prevstring="Previous $numdisplay" if ($blankon);
$firststring="First $numdisplay" if ($blankon);
}



if ($endno<$total){
my $nextstart = $position + $numdisplay;
my $how_many_left = $total - $nextstart;


if ($how_many_left > $numdisplay) {
$how_many_left = $numdisplay;
}
my $nextlink = &EditQueryString('position',$nextstart);
$nextstring="Next $how_many_left";


$lastpagenum = ($total % $numdisplay);
$laststart = $total - $lastpagenum;


my $lastlink = &EditQueryString('position',$laststart);
$laststring="Last $lastpagenum";
}
else {
$nextstring="Next $numdisplay" if ($blankon);
$laststring="Last $numdisplay" if ($blankon);
}


return 
($nextstring,$prevstring,$firststring,$laststring,$startno,$endno);
}





---------------------------------
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard

Reply via email to