473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Creating dynamic names for variables

Kelicula
176 Expert 100+
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
4 1622
numberwhun
3,509 Expert Mod 2GB
Is $len getting set correctly? When I try the for loop you are using, it works fine.

Regards,

Jeff
Jan 29 '08 #2
KevinADC
4,059 Expert 2GB
when you want dynamic variable names in perl you use a hash.
Jan 29 '08 #3
eWish
971 Expert 512MB
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
176 Expert 100+
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

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

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
5
by: Billy Cormic | last post by:
Hello, I am interested in dynamically creating temp tables using a variable in MS SQL Server 2000. For example: DECLARE @l_personsUID int select @l_personsUID = 9842
5
by: C White | last post by:
I'm trying to write an asp script that will create a series of drop lists based on a table like: canada ontario toronto street name link canada ontario toronto road...
7
by: Mark | last post by:
I want to create textboxes dynamically but with dynamic names also. i am retrieveing a load of values from a table in SQL into a DataReader. Then i want to create textboxes from those variables. ...
4
by: Tim.D | last post by:
People, I've ventured into the wonderful world of Stored Procedures. My first experience has been relatively successful however I am stuck on using host variables to specifiy actualy table or...
4
by: Livin | last post by:
I need to dynamically create dictionary names using strings input at the time of creation. These will then be placed into a "Parent" dictionary. I'm new to python, and programming, so please bear...
3
by: Mark S. | last post by:
As I understand it, C# doesn't offer dynamic variable names. Below is my attempted workaround. Is what I'm doing possible? FYI, I already read all the "why in the world do you need dynamic...
6
by: tjboudreaux | last post by:
Hi, This might be a crazy question, but does anyone know how to add variables dynamically to a class. Here's what I want to do. I have a class Model, which I inherit for interfacing with...
26
by: Aaron \Castironpi\ Brady | last post by:
Hello all, To me, this is a somewhat unintuitive behavior. I want to discuss the parts of it I don't understand. .... f= lambda: n .... 9 9
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.