By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,275 Members | 1,341 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,275 IT Pros & Developers. It's quick & easy.

Creating dynamic names for variables

Kelicula
Expert 100+
P: 176
Hello all, I need to create a certain number of variables depending on a result set from a database.

I only want to display 20 records per page, and I would like to have those page "markers" at the botton of the page for NEXT, PREVIOUS, and the links to the actual pages eg: 1, 2, 3 etc..

So far what I have is:

Expand|Select|Wrap|Line Numbers
  1. #!c:/Perl/bin/perl
  2.  
  3. use strict;
  4. use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
  5.  
  6. use DBI;
  7. use CGI qw(simple);
  8.  
  9.  
  10. #-------------------
  11. # Configure.........
  12.  
  13. $| = 1;
  14. my $q = new CGI;
  15. print $q->header;
  16. my $database = 'complete';
  17. my $handle = '';
  18. my $password = '';
  19. my %attr = ( RaiseError => 0, PrintError => 0 );
  20.  
  21.  
  22. #-------------------
  23. # Application.......
  24.  
  25. my $dbh = DBI->connect("DBI:mysql:$database:localhost:3306", $handle, $password, \%attr) || listErr( $DBI::errstr );
  26.  
  27. my $sth = $dbh->prepare(qq{ SELECT * FROM lamtest });
  28.  
  29. $sth->execute || listErr( $sth->errstr );
  30.  
  31. my $news = $sth->fetchall_arrayref({});
  32.  
  33. my $len = @{$news} / 20; # 20 records per page.
  34.  
  35. if($len =~ /\./){  # If resultset is NOT evenly divisible 
  36. $len =~ s/\d+\.*$//;  # Chop off the decimal and everything else to the right.
  37. $len += 1;          # add one to the quotient to allow for page with less then 20 rec
  38. }
  39.  
  40. print $len;   # this now equals how many slices there are.  in $news array.
  41.               # Also equals how many pages will be in the results.
  42.  
  43.               # Need to set up splices for each page...
  44. my $wts;      # this will equal the x param, and/or "where to start splicing.
  45.               # will need $lem number of these...
  46.  
  47. my $num = 0;
  48.  
  49. for(1..$len){
  50.  
  51. $wts+$_ = $num;
  52.  
  53. $num += 20;
  54. }
  55.  
  56.  
  57. my $point = $q->param('x'); 
  58. # these will be embedded in the page links..
  59. # will need more than one but just to give you the idea
  60.  
  61. my @send = splice( @{$news},$num, 20);
  62.  
  63.  
  64. #--------------------------------
  65. # Subroutines.............
  66.  
  67. sub listErr {
  68. my $e = shift;
  69. print "$e";
  70. }
  71.  
I can't seem to get the value in $_ to add to $wts..like $wts1, $wts2 etc...

I have also tried
Expand|Select|Wrap|Line Numbers
  1.  
  2. "$wts"."$_" = $num;
  3.  
and such...

Anyone know how to do this??
Or a better way to do ALL this??


Thank you!!

(please)
Jan 29 '08 #1
Share this Question
Share on Google+
4 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Is $len getting set correctly? When I try the for loop you are using, it works fine.

Regards,

Jeff
Jan 29 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
when you want dynamic variable names in perl you use a hash.
Jan 29 '08 #3

eWish
Expert 100+
P: 971
There are some modules on CPAN that will do pagination.

Some of them do SQL
Class::DBI::Factory::List
HTML::Paging::SQL


--Kevin
Jan 29 '08 #4

Kelicula
Expert 100+
P: 176
Thank you everyone!!!

I have resolved this issue.
I transfered everything to an array whose number of elements equal the number of pages, and the values within them are the splice settings...

Expand|Select|Wrap|Line Numbers
  1. #!c:/Perl/bin/perl
  2.  
  3. use strict;
  4. use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
  5.  
  6. use DBI;
  7. use CGI qw(simple);
  8.  
  9.  
  10. #-------------------
  11. # Configure.........
  12.  
  13. $| = 1;
  14. my $q = new CGI;
  15. print $q->header;
  16. my $database = 'complete';
  17. my $handle = '';
  18. my $password = '';
  19. my %attr = ( RaiseError => 0, PrintError => 0 );
  20.  
  21.  
  22. #-------------------
  23. # Application.......
  24.  
  25. my $dbh = DBI->connect("DBI:mysql:$database:localhost:3306", $handle, $password, \%attr) || die "seeDB: $DBI::errstr";
  26.  
  27. my $sth = $dbh->prepare(qq{ SELECT * FROM lamtest });
  28.  
  29. $sth->execute || listErr( $sth->errstr );
  30.  
  31. my $news = $sth->fetchall_arrayref({});
  32.  
  33.  
  34. # begin experiment.
  35.  
  36. my $len = @{$news} / 20;
  37.  
  38. if($len =~ /\./){
  39. $len =~ s/\d+\.*$//;
  40. $len += 1;
  41. }
  42.  
  43. # $len now equals how many slices there are.  in $news array.
  44. # Also equals how many pages will be in the results.
  45.  
  46. # Need to set up splices for each page...
  47. # this will equal the x param, and/or "where to start splicing".
  48. # will need $lem number of these...
  49.  
  50. my @wts;
  51. my $num = 0;
  52.  
  53. for(1..$len){
  54.  
  55. $wts[$_] = $num;
  56. $num += 20;
  57. }
  58.  
  59. # @wts is now an array that holds the number of pages=elements and each element contains the
  60. # number at which to start the splice of $news.
  61. # Now create an html block representing the "controls" for this mechanism.
  62.  
  63. my $y = $q->param('x'); 
  64. # holds the  previous position
  65. # for use later in links
  66.  
  67. my $links = "<a href='browsenews.cgi?x=".($y-20)."'>PREVIOUS</a> <=&nbsp\;&nbsp\;";
  68.  
  69.  
  70. for(1..$#wts){
  71.  
  72. $links .= "<a href='browsenews.cgi?x=$wts[$_]'>$_</a>,&nbsp\; ";
  73.  
  74. }
  75.  
  76. $links .= "=> <a href='browsenews.cgi?x=".($y+20)."'>NEXT</a>";
  77.  
  78.  
  79.  
  80. print $links;
  81.  
  82.  
  83.  
  84. sub listErr {
  85. my $e = shift;
  86. print "$e";
  87. }
  88.  
  89.  

Now I need to check for param('x') and such (if the results are less than 20) but Links prints out properly, so I think I got it.

Thanks!!
Jan 29 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.