Hi all, I have a script that someone else wrote that I am trying to do some small mods to. The only thing I have left to do is have the output in a 2 column table instead of a 1 column.
Any suggestions?
Thanks for reading!
Jim
Any suggestions?
Thanks for reading!
Jim
Code:
my $dbh = DBI->connect('DBI:mysql:farthing_valleyweb','farthing_farthin','ginajim')
or die "Couldn't connect to database: " . DBI->errstr;
my $sth = $dbh->prepare('SELECT * FROM valley WHERE category = ?')
or die "Couldn't prepare statement: " . $dbh->errstr;
# Collect parameters that determine where we are in the display.
# Default to beginning of result set, 10 records/page if parameters
# are missing/malformed.
my $cat1 = param("search1");
my $cat2 = param("search2");
$search = param("search");
if ($cat1) { $search = $cat1 };
if ($cat2) { $search = $cat2 };
my $start = param ("start");
$start = 1
if !defined ($start) || $start !~ /^\d+$/ || $start < 1;
my $per_page = param ("per_page");
$per_page = 10
if !defined ($per_page) || $per_page !~ /^\d+$/ || $per_page < 1;;
# If start > 1, then we'll need a live "previous page" link.
# To determine whether or not there is a next page, try to select one
# more record than we need. If we get that many, display only the first
# $per_page records, but add a live "next page" link.
# Select the records in the current page of the result set, and
# attempt to get an extra record. (If we get the extra one, we
# won't display it, but its presence tells us there is a next
# page.)
if ($type eq 'alpha') {
$query = sprintf (
"SELECT name, address, city, phone
FROM valley
where name like '$search%'
ORDER BY name LIMIT %d,%d",
$start - 1, # number of records to skip
$per_page + 1); # number of records to select
} else {
$query = sprintf (
"SELECT name, address, city, phone
FROM valley
where keywords like '%$search%'
ORDER BY name LIMIT %d,%d",
$start - 1, # number of records to skip
$per_page + 1); # number of records to select
}
my $tbl_ref = $dbh->selectall_arrayref ($query);
$dbh->disconnect ( );
# Display results as HTML table
my @rows;
for (my $i = 0; $i < $per_page && $i < @{$tbl_ref}; $i++)
{
# get data values in row $i
my @cells = @{$tbl_ref->[$i]}; # get data values in row $i
# map values to HTML-encoded values, or to if null/empty
@cells = map {
defined ($_) && $_ ne "" ? escapeHTML ($_) : " "
} @cells;
# add cells to table
push (@rows, Tr (td (\@cells)));
}
$page .= table ({-border => 0}, @rows) . br ( );
# If we're not at the beginning of the query result, present a live
# link to the previous page. Otherwise present static text.
if ($start > 1) # live link
{
my $url = sprintf ("%s?start=%d;per_page=%d;search=$search",
url ( ),
$start - $per_page,
$per_page);
$page .= "[" . a ({-href => $url}, "previous page") . "] ";
}
else # static text
{
$page .= "[previous page]";
}
# If we got the extra record, present a live link to the next page.
# Otherwise present static text.
if (@{$tbl_ref} > $per_page) # live link
{
my $url = sprintf ("%s?start=%d;per_page=%d;search=$search;type=$type",
url ( ),
$start + $per_page,
$per_page);
$page .= "[" . a ({-href => $url}, "next page") . "]";
}
else # static text
{
$page .= "[next page]";
}
$page .= end_html ( );
print $page;