stew has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I am trying to slap together an index of a MySQL database using DBIx::FullTextSearch with a phrase type back-end and HTML::Template to display the results

I am trying to put the search string back into the the search box ( $template->param(SEARCH => $search);), which works fine apart from when I use a string in quotes i.e. to denote a phrase it puts nothing back into the search box

Interestingly enough if I print $search to the screen the quotes are intact

Here is my code

#!/usr/bin/perl use CGI qw(param); use DBIx::FullTextSearch; use DBIx::FullTextSearch::StopList; use DBI; use HTML::Template; my $dbh = DBI->connect('dbi:mysql:xxxxx','xxxx','xxxxx'); my $search = param("search"); my $fts = DBIx::FullTextSearch->open($dbh, 'fts_cobra'); my @files = $fts->search($search); if (@files) { foreach $filename(@files){ $filename = substr($filename,0,-4); $sth = $dbh->prepare("SELECT id, title, summary FROM resource W +HERE id ='$filename'"); $sth->execute(); while ($ref = $sth->fetchrow_hashref()){ $title = $ref->{title}; $id = $ref->{id}; push @rows, { ID => $id, TITLE => $title }; } } } my $template = HTML::Template->new(filename => 'search.tmpl'); $template->param(TAB_BG_MAIN => "#0B5875"); $template->param(TAB_BG_HEAD => "#6180BA"); $template->param(SEARCH => $search); $template->param(ROWS => \@rows); print "Content-type: text/html\n\n"; print $template->output;


I bet this is a no-brainer for the average - and most of you will be wondering how I ever got this far - any comments even with 'sneers' will be greatly appreciated

Stew

p.s. anyone want to give me a clue how to take this further and add HTML::Pager functionallity will definately be considered for a Christmas Card :)

Replies are listed 'Best First'.
Re: HTML::Template question
by broquaint (Abbot) on Nov 14, 2002 at 11:34 UTC
    Placeholders my friend, placeholders
    $sth = $dbh->prepare( 'SELECT id, title, summary FROM resource WHERE id = ?' ); $sth->execute($filename);
    Oh yes, and see the docs for more info.

    Update: As LTjake has duly noted, placeholders are not the issue here (although they are a must!). You need you to flip the quoting in either the <input> tag or the <TMPL> tag e.g

    <input name="search" type="text" value="<TMPL_VAR NAME='SEARCH'>" /> ## or <input name='search' type='text' value='<TMPL_VAR NAME="SEARCH">' />

    HTH

    _________
    broquaint

Re: HTML::Template question
by LTjake (Prior) on Nov 14, 2002 at 12:20 UTC
    If by searchbox you mean:
    <input name="search" type="text" value="<TMPL_VAR NAME="SEARCH">" />
    You'll want to add ESCAPE="HTML" to the TMPL_VAR tag in order to convert quotes into it's HTML equivalent (&quot;).
    <input name="search" type="text" value="<TMPL_VAR NAME="SEARCH" ESCAPE +="HTML">" />
    HTH

    Update: This problem (with input boxes) is mentioned in the docs under TMPL_VAR

    --
    Rock is dead. Long live paper and scissors!
(jeffa) Re: HTML::Template question
by jeffa (Bishop) on Nov 14, 2002 at 17:56 UTC
    Three more pointers for you ...
    1. associate - add this option to the HTML::Template 'constructor' and point it to an instantiated CGI object:
      my $query = new CGI; my $template = HTML::Template->new( filename => 'search.tmpl', associate => $query, );
      Now there is no need to explicitly assign "search":
      $template->param(SEARCH => $search);
      You should validate $search before you pass it to $fts, however - better safe than sorry.
    2.  

    3. Use CSS instead of TAB_BG_MAIN and TAG_BG_HEAD. Design decisions are best left out your Perl script.
    4.  

    5. Post your template code along with your Perl code, at least the relevant portion(s). ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)