in reply to Re: using template on same page
in thread using template on same page

tho i get this error when i try to get array from db. but the code works ok just only not getting array's

#MY SCRIPT use strict; use warnings; use Template; my $template = Template->new; if($info) { my $select = $DBH->prepare("SELECT FOO, BAR, MOO FROM tble WHERE CONCA +T(FOO, ', ', BAR, ', ', MOO) LIKE ?"); $select->execute('%'.$info.'%'); $names = $select->fetchall_arrayref(); foreach $names ( @$names) { ($variable1, $variable2, $variable3) = @$names; } } my $templ = <<START_HTML; <!DOCTYPE html"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> <title>Untitled Document</title> </head> <body> [% FOREACH name IN list %] <li> <div class='inforno'> <img src='inforno'> </div> <div class='inforno'> <a href='#' class='inforno'>[% name %]</a> </div> <span class='inforno'> <a href='#' class='inforno'>Edit user</a> </span> </li> [% END %] </body> </html> START_HTML $template->process (\$templ, { list => \@names }) or die $template->error;

MY OUTPUT

ARRAY(0x2030674) ARRAY(0x2030634) ARRAY(0x2030618)

Replies are listed 'Best First'.
Re^3: using template on same page
by davido (Cardinal) on Sep 15, 2018 at 17:45 UTC

    You may not have noticed this yet but the template you posted generates <li>...</li> pairs, but never creates an encompassing <ul>...</ul> nor <ol>...</ol> construct to contain the list items.


    Dave

Re^3: using template on same page
by Corion (Patriarch) on Sep 15, 2018 at 17:14 UTC

    Your script as posted doesn't compile for me. I get the following errors when running it:

    Global symbol "$info" requires explicit package name (did you forget t +o declare "my $info"?) at tmp.pl line 9. Global symbol "$DBH" requires explicit package name (did you forget to + declare "my $DBH"?) at tmp.pl line 11. Global symbol "$info" requires explicit package name (did you forget t +o declare "my $info"?) at tmp.pl line 12. Global symbol "$names" requires explicit package name (did you forget +to declare "my $names"?) at tmp.pl line 13. Global symbol "$names" requires explicit package name (did you forget +to declare "my $names"?) at tmp.pl line 14. Global symbol "$names" requires explicit package name (did you forget +to declare "my $names"?) at tmp.pl line 14. Global symbol "$variable1" requires explicit package name (did you for +get to declare "my $variable1"?) at tmp.pl line 15. Global symbol "$variable2" requires explicit package name (did you for +get to declare "my $variable2"?) at tmp.pl line 15. Global symbol "$variable3" requires explicit package name (did you for +get to declare "my $variable3"?) at tmp.pl line 15. Global symbol "$names" requires explicit package name (did you forget +to declare "my $names"?) at tmp.pl line 15. Global symbol "@names" requires explicit package name (did you forget +to declare "my @names"?) at tmp.pl line 48. tmp.pl had compilation errors.

    Can you please post the code you're actually running?

    Also, if you're using strict.pm, it allows Perl to tell you when you forget to declare a variable. Going through your code and thinking about where you first use a variable will make you find typos in your script where you mistype a variable name, or mistype a variable type.

Re^3: using template on same page
by poj (Abbot) on Sep 15, 2018 at 17:27 UTC

    Which of the fields FOO, BAR, MOO contains the names ?

    Also, <!DOCTYPE html"> should not have the double quote in it.

    poj

      i dont get u well. but am trying to get poll foo,bar,moo from db

        Ok, what is the fieldname in table tble that the values 'foo', 'bar' and 'moo' are stored ?

        poj
Re^3: using template on same page
by bliako (Abbot) on Sep 16, 2018 at 10:11 UTC
    $names = $select->fetchall_arrayref(); foreach $names ( @$names) { ($variable1, $variable2, $variable3) = @$names; }

    and then

    $template->process (\$templ, { list => \@names }) or die $template->error;

    I bet this last names you wanted it to be the other names and not the names perl has in mind.

    names, names, all these names!!! fix these (and what others told you) and your program will be happier: re the joy of anonymity...

    bw, a horse with no name

      sorry it was wrong writing but its. i mistake @names with @$names. but thats the code. i dont know failed to pull from db just outputing

      ARRAY(0x2190674) ARRAY(0x2190674) ARRAY(0x2190684)
      $template->process (\$templ, { list => \@$names }) or die $template->error;
      </code>

        { list => \@$names } is the same as { list => $names }

        I'm guessing you just need to change your template to

        <a href='#' class='inforno'>[% name.0 %] [% name.1 %] [% name.2 %]</a>

        poj