CGI questions are often better addressed to [EMAIL PROTECTED]

> 
> Hi everybody,
> 
> I'm stuck trying to output multiple search results.
> 
> I wrote this code but, and it outputs correctly the number of pages,
but it
> shows only the results for the first page no mater what link do I click.
> 
> You can find the script in "action" at
> www.wiq.ro/cgi-bin/rointera/results.cgi 
>
> Please look at the code and tell me where I'm wrong.
> 
>  
> 
> _______________________________________________
> 
> #!/usr/bin/perl
>

First place you are wrong is right here, you need,

use strict;
use warnings;
 
>  
> 
> use CGI qw( :standard );
> 
> use CGI::Carp qw( fatalsToBrowser );
> 
> my $q = new CGI;
> 
> print $q->header;
> 
> use DBI;
> 
> my $user = "root";
> 
> my $pass = "";
> 
> my $source = "DBI:mysql:chestie";
> 
> my $dbh=DBI->connect($source, $user, $pass, {RaiseError => 1});
> 
>  
> 
> my $sql = qq{SELECT parola FROM parole};
> 

This can be improved using a LIMIT clause so that you only select the
rows needed, this will improve performance on large result sets. This
early in the script you only need a total count of the rows possible,
use a COUNT(*) to select that specifically, it will be much more
efficient than getting all of the rows from the DB.

> my $sth = $dbh->prepare($sql);
> 
> $sth->execute;
> 
>  
> 
> while (my @row = $sth->fetchrow_array()){
> 

I suspect (hope) you are going to do more with the result set than just
push to another array, if that is the case then switch to a
fetchrow_hashref so you can access into the fields by name, otherwise
you might want to use one of the 'selectall_' methods.

>       push (@results, $row[0]);
> 
> }
> 
>  
> 
> $sth->finish; 
> 
> $result_count = @results;
> 
> $pagesize = 5;
> 
> $reqpage = 1;

I suspect this is your error, $reqpage is *always* 1, which means....

> 
>  
> 
> if ($result_count != 0) {
> 
> $pagecount = int($result_count / $pagesize);
> 
> if (($pagecount * $pagesize) != $result_count) {
> 
> $pagecount++;
> 
> }
> 
> }
> 
>  
> 
>  
> 
> $firstresult = (($reqpage - 1) * $pagesize) + 1;


$firstresult will *always* be 1, because $reqpage (which is 1) -1 is
always 0, times *anything* is 0, +1 is always 1.

> 
> $lastresult = $firstresult + $pagesize - 1;
> 

Since $firstresult is 1, the above construct is negative, which seems a
little odd...

> if ($lastresult > $result_count) {
> 

If lastresult is negative, it will never be larger than result count.

> $lastresult = $result_count;
> 
> }
> 

<snip rest of code>

Fix the above issues and see if that will get you going. This is one of
the more difficult things to do in CGI it seems.  I also posted a rather
lengthy explanation of how to do this back in November that you can find
here:

http://groups.google.com/groups?q=result+pages+group:perl.beginners.cgi&hl=en&lr=&ie=UTF-8&group=perl.beginners.cgi&selm=3FC7B85D.6050508%40danconia.org&rnum=3

HTH,

http://danconia.org


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to