Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^11: Need help with pagination

by marto (Cardinal)
on Jan 01, 2023 at 16:35 UTC ( #11149264=note: print w/replies, xml ) Need Help??


in reply to Re^10: Need help with pagination
in thread Need help with pagination

Use three argument open, http://modernperlbooks.com/mt/2010/04/three-arg-open-migrating-to-modern-perl.html, use $! when calling die to tell you why the open failed.

Replies are listed 'Best First'.
Re^12: Need help with pagination
by *alexandre* (Beadle) on Jan 01, 2023 at 20:06 UTC
    Doesn't work by the way when I run the following code
    #!c:/xampp/perl/bin/perl.exe use warnings; use CGI; use Time::HiRes qw(gettimeofday); my $query = CGI->new; my $dir = "C:/Users/41786/OneDrive/Documents/recordz1/"; print $query->header; test(); sub test { my $counter = 10000; #Should be ge from db; my $string = ""; open (FILE, "<$dir/test2.html") or die "cannot open file $dir/test +2.html"; my $content = ""; my $nb_page = round ($counter / 40, 1);#Number of objects display +ed per page. for (my $i = '0'; $i < $nb_page+ 1;$i++) { my $n2 = $i %= 10; $min_index += 40; $string .= "<a href=\"/cgi-bin/recordz.cgi?lang=FR&amp;session +=1&page=historique&amp;article=1amp;min_index=$min_index&amp;max_inde +x=40\" ><-$n2-></a>&#160;&nbsp;"; $nb_page--; } while (<FILE>) { s/\$ARTICLE{'index'}/$string/g; $content .= $_; } print $content; close (FILE); } sub round { my $n = shift || ''; my $r = sprintf("%.0f", $n); return $r; }
    The result I'm getting in my web page is the following : <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-0-> <-1-> <-2-> <-3-> <-4-> <-5->

      How does that not work?

      We don't know what you expect and what you think is wrong with the output.

      I assume that you pasted the rendered HTML output, but you may want to reduce your program to the bare minimum. Is the CGI part necessary for testing your program?

      Update: Maybe you want to go line by line over this loop and explain what you are doing there and why:

      for (my $i = '0'; $i < $nb_page+ 1;$i++) { my $n2 = $i %= 10; $min_index += 40; $string .= "<a href=\"/cgi-bin/recordz.cgi?lang=FR&amp;session +=1&page=historique&amp;article=1amp;min_index=$min_index&amp;max_inde +x=40\" ><-$n2-></a>&#160;&nbsp;"; $nb_page--; }

      I suggest you use a Perl-style loop instead of a C-style loop and use

      for my $i (0..$nb_page) { ... }

      Also, why do you use my $i = '0'; instead of my $i = 0;?

      Why do you decrement $nb_page at the end of the loop?

      What is the $min_index variable about?

      What is the $i variable supposed to hold and why do you keep it below 10?

        HI thanks for your time : I made some change to make to code more readable :
        #!c:/xampp/perl/bin/perl.exe use warnings; use CGI; use Time::HiRes qw(gettimeofday); my $query = CGI->new; my $dir = "C:/Users/41786/OneDrive/Documents/recordz1/"; print $query->header; test(); sub test { my $counter = 1000; #Should be get from db; my $string = ""; my $first_page = 0; my $min_index = $query->param("min_index"); if ($min_index = '') { $min_index = 0; } open (FILE, "<$dir/test2.html") or die "cannot open file $dir/test +2.html"; my $content = ""; my $max_index = $query->param("max_index"); if ($max_index = '') { $max_index = 40; }else { $max_index = round ($counter / 40, 1);#Number of objects displ +ayed per page. } my $last_page = $nb_page - 1; my $n2 = 0; my $count_per_page = 10; my $index_page = 0; for my $index (0..$max_index) { $next_page = $min_index + 40; if ($index < $count_per_page) { if (($index % $count_per_page) > 0) { $string .= "<a href=\"/cgi-bin/pagination.pl?lang=FR&a +mp;session=1;min_index=$min_index&amp;max_index=$max_index\" ><-$inde +x_page-></a>&#160;&nbsp;"; } } $index_page++; $min_index += 40;; } $string .= "<a href=\"/cgi-bin/pagination.pl?lang=FR&amp;session=1 +;min_index=$min_index&amp;max_index=$max_index\" ><-\"Next\"-></a>&#1 +60;&nbsp;"; while (<FILE>) { s/\$ARTICLE{'index'}/$string/g; $content .= $_; } print $content; close (FILE); } sub round { my $n = shift || ''; my $r = sprintf("%.0f", $n); return $r; }
        Now I have the following output : <-1-> <-2-> <-3-> <-4-> <-5-> <-6-> <-7-> <-8-> <-9-> <-"Next"-> Thx for kind help.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11149264]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2023-10-01 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?