Forum Moderators: coopster & phranque

Message Too Old, No Replies

Displaying database results from template

Going from Tables to a Link...

         

victoryrun

6:24 pm on Nov 18, 2003 (gmt 0)

10+ Year Member



I finally got my database to display with the help of csvread.pl ... But what it does is pulls the form fields into a template that is HTML driven. Currently, I have it set up in tables so that each form field displays in seperate cells. What I'd like to accomplish is having the 'title' field displayed as a LINK, and then by clicking that LINK, it takes you to the table where all other forms/info is displayed. This helps for large ammounts of form submissions (since the form is huge to begin with) from displaying on one HUGE page. Does this make sense? Here is the current pl file:

#!/usr/bin/perl

##########################################################
# CSVread 17/Nov/2003
# © 1999-2003 EZscripting.co.uk
# Script by Alexandre Golovkine
##########################################################
my $CSV_file = "database.txt";
my $HTML_template = "featuredtopic_submissions.html";
my $no_matches_found = "Sorry, no results found";
##########################################################
#%FORM = parse_cgi();
print "Content-type: text/html\n\n";
(my $head, my $tmp, my $foot) = get_html($HTML_template);

$qs=$ENV{'QUERY_STRING'};
##read db
my @data = read_file($CSV_file);
chomp $data[0];
my @fields= split('\¦', shift @data);
$base_length = @data;
error("You have bad file!") if!@fields;
error("Database is clear!") if $base_length<1;

if($qs =~m/header=([^\&\Z]*)/){push @header,$1;}
@conditions=split(/&/,$qs);
my $a=0;
foreach (@conditions){
($name, $value) = split(/=/, $_);
if($name eq 'search'){
$FORM{search} = $value;
@data = search($value, $header[0]);
}
elsif($name eq 'header'){}
elsif($_=~/([^=<>!]+)!=([^=<>!]+)/){@data = search($value, $1, "!=");}
elsif($_=~/([^=<>!]+)=([^=<>!]+)/){@data = search($value, $1);}
$a++;
}
my $result;
##matched data
foreach(@data){
chomp;
@line = split('\¦', $_);
$a=0; %INSERT=();
foreach(@fields){$INSERT{$_} = $line[$a++];}
$result.=get_record($tmp)
}
%INSERT=();
$INSERT{'#_matches'} = @data;
$INSERT{'#_total'} = $base_length;

print get_record($head), $result, get_record($foot);
exit;

#########################################################
sub search{
my $word=shift;
my $field=shift;
my $action=shift;
$word=~tr/+/ /;
$word=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex($1))/eg;

my $position=-1;
my $a=0;
if($field){
$field=~tr/+/ /;
$field=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex($1))/eg;
foreach(@fields){$position=$a if $_ eq $field; $a++;}
}
my %match;
$word =~s/ +/ /g;
my @new_data=();
my @keys= split(" ", $word);
if($action eq '!='){for(0..@data-1){$match{$_} = 1;}}
foreach $key (@keys){
$a=0;
foreach $record (@data){
@line = split('\¦', $record);
if($field && $position>-1){
if($action eq '!='){
$match{$a} = 0 if $line[$position]=~m/$key/i;
}
else{$match{$a} = 1 if $line[$position]=~m/$key/i;}
}
else{
foreach(@line){if ($_=~m/$key/i){$match{$a} = 1; last;}}
}
$a++;
}
}
$a=0; my $b=0;
foreach(@data){
$new_data[$b++] = $_ if $match{$a};
$a++;
}
return @new_data;
}

sub get_record{
my $text = $_[0];
$text =~ s{<<(.*?)>>}{exists($INSERT{$1})? $INSERT{$1} : ""}gsex;
return $text;
}
sub get_html{
my @txt = read_file($_[0]);
foreach(@txt){$txt.=$_;}
$txt=~/(.*)<template>(.*)<\/template>(.*)/s;
error("Template-tag not found!") if!$1 or!$2;
return ($1,$2,$3);
}
sub read_file{
open(F, $_[0]) ¦¦ error("Can't open file $_[0]!");
my @data = <F>;
close F;
return @data;
}
sub error{
print "<html><head><title>Error</title>$style</head><body><br><br><br><font color=red><h3>$_[0]</h3></font></body></html>";
exit;

}

jatar_k

7:14 pm on Nov 18, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



so where, in that big mess, is the title?

which line or small piece of code are you trying to change. That is a lot for people to work through.

victoryrun

2:39 am on Nov 19, 2003 (gmt 0)

10+ Year Member


Well I'd post a link for you to see what I mean, but that is considered "url dropping" so I didn't know anyother way to go about it. That is the code that pulls the fields from my database into an HTML file, which is completely template driven. For instance, if I had 3 fields, <field1> <field2> and <field3>, this code would display these 3 fields in a bulleted list format, IF my HTML template looked like this:
<ul>
<li><field1>
<li><field2>
<li><field3>
</ul>

In essense, the form that the PL file pulls all these fields from has about 50 fields, and there should be about 30 instances of each field, so you can see where this may get a little cluttered. I'd like to be able to have my html template pull the first field (in this case, is called 'title') and only list the title from each form that's been submitted. Then each title will be linked to a second page that has all the fields displayed via another html template. Does this make sense? Is this possible? Do you need to see my current html template? I'm sorry for not being descriptive or posting the right thing with my first post, as I really don't know how to accomplish this and don't know what you all need to see in order to understand what I'm trying to convey, without 'dropping' my url. :D

Thanks!

jatar_k

4:35 pm on Nov 20, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



a good description

mostly with large amounts of code like that, people don't have the time to work through it all and they often go unanswered.

So since my perl isn't great, do you understand the code well enough to figure out where/ what you need to change? Just need a little help changing it.

essentially I would play with the template to see what can be changed (keep a copy of the original, obviously). You may need to seperate templates on for the linked list and one for the detail pages.

You also may be able to use a param for it. if detail then grab all else only add the link to list.