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

S

shree

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
 
M

Michael Budash

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...
 
B

Bill

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top