perl - cgi - how to displ a block of records with navigational hyperlinks

Discussion in 'Perl Misc' started by shree, Oct 11, 2003.

  1. shree

    shree Guest

    Hello,

    First off, pls forgive me if this appears repeated..I had posted a
    similar ques a while ago but could not find fix back then, but found a
    workaround for it at the time.

    Objective:
    How to get a block of records at a time from a flat file database and
    display it as an html table, with navigational hyperlinks to index
    forward to the next block of records, or index backward to a previous
    block of records.

    My workaround previously was to import flatfile into a MySQL database
    and modify SQL stmts in my perl-cgi scripts. I don't have the luxury
    of this option currently..hence, I'm stuck and would appreciate
    hearing suggestions.

    I have pasted sample data, code, output this code generates and output
    I would like to see..hence, request to help identify and fix the code.

    Data File name: TestData
    ----Sample Data------
    RecordID|Last Name|First Name|Date Entered|Details
    1|Smith|John|2003/10/01|ID1 Details
    2|Smith|Joe|2003/10/02|ID2 Details
    3|Smith|Mary|2003/10/03|ID3 Details
    4|Smith|John4|2003/10/04|ID4 Details
    5|Smith|John5|2003/10/05|ID5 Details
    6|Smith|John6|2003/10/06|ID6 Details
    7|Smith|John7|2003/10/07|ID7 Details
    8|Smith|John8|2003/10/08|ID8 Details
    9|Smith|John9|2003/10/09|ID9 Details
    10|Smith|John10|2003/10/10|ID10 Details

    Code name: Disp2Records.cgi
    ----Code------

    #!/usr/bin/perl -w

    use CGI qw:)standard);
    use CGI::Carp qw(fatalsToBrowser);
    use strict;

    my $items = 2; #Disp 2 records at a time..increase this later
    my $start_item = param('start') || 0 ;

    my $me = script_name();

    my (@data_array) = ();
    my ($next_items, $prev_items) = "";

    print header;
    print start_html('TestDatabase - Records Display');

    my $File_Data = "TestData";

    if (open(FILE, $File_Data ) or die "Cannot open $File_Data $!")
    {
    while(<FILE>)
    {
    chomp;
    my @stuff = split /\|/,$_;
    push @data_array,\@stuff;
    }
    print "<TABLE>\n";
    foreach (@data_array[$start_item ... ($start_item + $items - 1)])
    {
    print Tr(td(@{$_})),"\n";

    }
    print "</TABLE>\n";
    $prev_items = $start_item - $items;
    $prev_items = 0 if ($prev_items < 0 );
    $next_items = $start_item + $items;
    $next_items = 0 if ($next_items > $#data_array);

    print "<TABLE WIDTH=100%>\n";
    print "<TR WIDTH=100%>\n";
    print "<TD WIDTH=50% ALIGN=left>\n";
    if ( $start_item > 0 )
    {
    print "<A HREF=$me?start=$prev_items>Previous Page</A>\n";
    }
    print "</TD>\n";
    print "<TD WIDTH=50% ALIGN=right>\n";
    if ( $next_items > 0 )
    {
    print "<A HREF=$me?start=$next_items>Next Page</A>\n";
    }
    print "</TD>\n";
    print "</TR>\n</TABLE>\n";
    }
    else
    {
    print h1('Couldnt open file');
    }

    print end_html;

    ------Output the code generates when script is -------
    Please note: I have pasted content between <body> </body> tags only as
    thats the only part thats relevant for the discussion.

    <body>
    <TABLE>
    <tr><td>RecordID Last Name First Name Date Entered Details</td></tr>
    <tr><td>1 Smith John 2003/10/01 ID1 Details</td></tr>
    </TABLE>
    <TABLE WIDTH=100%>
    <TR WIDTH=100%>
    <TD WIDTH=50% ALIGN=left>
    </TD>
    <TD WIDTH=50% ALIGN=right>
    <A HREF=/cgi-bin/Disp2Records.cgi?start=2>Next Page</A>
    </TD>
    </TR>
    </TABLE>
    </body>

    Note In the 1st TABLE block, between <tr> to </tr> it stuffs entire
    line (one record) into one <td> </td> tag. Instead I would like it to
    be

    <TABLE>
    <tr><td>RecordID</td><td>Last Name</td><td>First Name</td><td>Date
    Entered Details</td></tr>
    <tr><td>1</td><td>Smith</td><td>John</td><td>2003/10/01</td><td>ID1
    Details</td></tr>
    </TABLE>

    Later..it would be nice to have hyperlinks to allow user to jump to
    end or anywhere in between..similar to google search. Suppose there
    are 100 records and we're displaying 10 records at a time. In the
    inital results, it will display records 1 - 10, and will have
    hyperlinks for 11-20, 21-30 and so on..till 91 - 100. Hence, display 9
    hyperlinks.

    I appreciate any help and guidance you can offer.

    Thank you,
    Shree
     
    shree, Oct 11, 2003
    #1
    1. Advertising

  2. In article <>,
    (shree) wrote:

    > Hello,
    >
    > First off, pls forgive me if this appears repeated..I had posted a
    > similar ques a while ago but could not find fix back then, but found a
    > workaround for it at the time.
    >
    > Objective:
    > How to get a block of records at a time from a flat file database and
    > display it as an html table, with navigational hyperlinks to index
    > forward to the next block of records, or index backward to a previous
    > block of records.
    >
    > My workaround previously was to import flatfile into a MySQL database
    > and modify SQL stmts in my perl-cgi scripts. I don't have the luxury
    > of this option currently..hence, I'm stuck and would appreciate
    > hearing suggestions.
    >
    > I have pasted sample data, code, output this code generates and output
    > I would like to see..hence, request to help identify and fix the code.
    >
    > Data File name: TestData
    > ----Sample Data------
    > RecordID|Last Name|First Name|Date Entered|Details
    > 1|Smith|John|2003/10/01|ID1 Details
    > 2|Smith|Joe|2003/10/02|ID2 Details
    > 3|Smith|Mary|2003/10/03|ID3 Details
    > 4|Smith|John4|2003/10/04|ID4 Details
    > 5|Smith|John5|2003/10/05|ID5 Details
    > 6|Smith|John6|2003/10/06|ID6 Details
    > 7|Smith|John7|2003/10/07|ID7 Details
    > 8|Smith|John8|2003/10/08|ID8 Details
    > 9|Smith|John9|2003/10/09|ID9 Details
    > 10|Smith|John10|2003/10/10|ID10 Details
    >
    > Code name: Disp2Records.cgi
    > ----Code------
    >
    > #!/usr/bin/perl -w


    good so far

    >
    > use CGI qw:)standard);
    > use CGI::Carp qw(fatalsToBrowser);
    > use strict;


    excellent

    >
    > my $items = 2; #Disp 2 records at a time..increase this later
    > my $start_item = param('start') || 0 ;
    >
    > my $me = script_name();
    >
    > my (@data_array) = ();
    > my ($next_items, $prev_items) = "";


    s/b:

    my (@data_array, $next_items, $prev_items);

    >
    > print header;
    > print start_html('TestDatabase - Records Display');
    >
    > my $File_Data = "TestData";
    >
    > if (open(FILE, $File_Data ) or die "Cannot open $File_Data $!")


    good error handling

    > {


    insert this here:

    # start the results table
    print "\n<TABLE>\n";

    # retrieve and show the column headings
    my $header = <FILE>;
    chomp $header;
    my @row = split /\|/,$header;
    print Tr(td(\@row)); # note: using reference generates separate td's

    > while(<FILE>)
    > {
    > chomp;
    > my @stuff = split /\|/,$_;
    > push @data_array,\@stuff;
    > }


    next line no longer needed:

    > print "<TABLE>\n";
    > foreach (@data_array[$start_item ... ($start_item + $items - 1)])
    > {
    > print Tr(td(@{$_})),"\n";
    > }
    > print "</TABLE>\n";


    [snip rest of code - looks ok and works fine]

    [snip output of original code]

    > Note In the 1st TABLE block, between <tr> to </tr> it stuffs entire
    > line (one record) into one <td> </td> tag. Instead I would like it to
    > be
    >
    > <TABLE>
    > <tr><td>RecordID</td><td>Last Name</td><td>First Name</td><td>Date
    > Entered Details</td></tr>
    > <tr><td>1</td><td>Smith</td><td>John</td><td>2003/10/01</td><td>ID1
    > Details</td></tr>
    > </TABLE>


    the code will do as you ask now

    >
    > Later..it would be nice to have hyperlinks to allow user to jump to
    > end or anywhere in between..similar to google search. Suppose there
    > are 100 records and we're displaying 10 records at a time. In the
    > inital results, it will display records 1 - 10, and will have
    > hyperlinks for 11-20, 21-30 and so on..till 91 - 100. Hence, display 9
    > hyperlinks.


    there are cpan modules that will assist you in this. no need to write it
    yourself. for that matter, the same modules could've helped you with the
    'previous' and 'next ' links as well...

    --
    Michael Budash
     
    Michael Budash, Oct 11, 2003
    #2
    1. Advertising

  3. shree

    Bill Guest

    (shree) wrote in message news:<>...
    >
    > Objective:
    > How to get a block of records at a time from a flat file database and
    > display it as an html table, with navigational hyperlinks to index
    > forward to the next block of records, or index backward to a previous
    > block of records.
    >
    > My workaround previously was to import flatfile into a MySQL database
    > and modify SQL stmts in my perl-cgi scripts. I don't have the luxury
    > of this option currently..hence, I'm stuck and would appreciate
    > hearing suggestions.


    Well, if you already have working MySQL database code, is that coded with DBI?

    If so, why not use DBD::CSV with DBI on a flat file and recyle your old code?
     
    Bill, Oct 11, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?ZGF2ZS5kb2xhbg==?=

    Tree View for Non-Navigational Purposes

    =?Utf-8?B?ZGF2ZS5kb2xhbg==?=, Mar 19, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    3,209
    =?Utf-8?B?ZGF2ZS5kb2xhbg==?=
    Mar 20, 2006
  2. Adrienne Boswell

    Opinions on placement of navigational items

    Adrienne Boswell, Jul 25, 2006, in forum: HTML
    Replies:
    6
    Views:
    335
    Adrienne Boswell
    Jul 26, 2006
  3. morrell
    Replies:
    1
    Views:
    962
    roy axenov
    Oct 10, 2006
  4. Ahmad
    Replies:
    0
    Views:
    119
    Ahmad
    Nov 16, 2003
  5. kath
    Replies:
    4
    Views:
    635
    J. Gleixner
    Apr 9, 2007
Loading...

Share This Page