Gossamer Forum
Home : General : Perl Programming :

writing to a file or template

Quote Reply
writing to a file or template
I have played with perl before but I have aways coded dirty projects that just get the job done. So forgive me if I am asking some silly questions.
My script that I am trying to write will connect to a database and pull out a list of films and then write these out to a file.
At the moment I have it just dumping to a txt file but I wanted to write out to a nicer formated html file
I want to out put to a html file but I am unsure how to do this
I am not sure if I should write the whole file in the script or to open a template and add the output to the file ?
I have to write the list of the moves to the html file and it will aways stay on line 222

I need to out put in this format
da[0]=new dataArray('10 things','movie (dvd)');
da[1]=new dataArray('2 fast 2 furious','movie (VHS)');
da[2]=new dataArray('25th hour','movie (LAZERDISK)');
Da is a of cuase the number (Sequential) 1,2,3,4 then it will need to list the name of the move and the finaly the movie type eg dvd vhs or lazerdisk.

[PHP]
#!/usr/bin/perl
use DBI;
$db_server = "localhost";
$db_user = "DBUSER";
$db_password = "PASS";
$db_database = "VIDDB";
$dbh = DBI->connect("dbi:mysql:$db_database:$db_server",$db_user,$db_password) || die("Can't connect");

$SELECT = "SELECT title, Mediatype
FROM videodata
WHERE Mediatype <> '50'
ORDER BY title";

$result = $dbh->selectall_arrayref($SELECT);
[/PHP]

Here is the code I used to do the connecting now I just need to code to output the file so if any one can give me a hand or some ideas that would be great :-)
Quote Reply
Re: [SimonTOZ] writing to a file or template In reply to
Code:
use strict;
use
DBI
;
use
DBI
;
use
CGI
;
use
CGI::Carp qw(fatalsToBrowser
);
use
HTML::Template
;

my $query = CGI->new
();


my $db_server = "localhost"
;
my $db_user = "videodb"
;
my $db_password = "inmos32"
;
my $db_database = "VideoDB"
;
my $dbh = DBI->connect("dbi:mysql:$db_database:$db_server",$db_user,$db_password) || die("Can't connect"
);



## Define your query and return the data in a format that
## you can easily stick into a template
my $SELECT =
"SELECT title, Mediatype
FROM videodata
WHERE Mediatype <> '50'
ORDER BY title"
;

my $sth = $dbh->prepare($SELECT
);

$sth->execute
();

my ($title,$MediaType
);
$sth->bind_columns(($title,$MediaType
));

my @rows
;
while (
$sth->fetch
) {
push @rows, { title => $title
,
MediaType => $MediaType
,
};
}


##Define Your Template
my $tmpl = HTML::Template->new( filename => "movies.tmpl"
);

## Feed the information you got from the query (@rows) into your template
$tmpl->param(rows => @rows
);


## Output the html content header
print $query->header( -type => 'text/html'
);


## Print the template
print $tmpl_hdr->output
;



This is where I am now but give the error

[Fri Mar 5 21:52:47 2004] logic.pl: Global symbol "$tmpl_hdr" requires explicit package name at logic.pl line 55.

Here is the line 54/55

## Print the template
print $tmpl_hdr->output;
Quote Reply
Re: [SimonTOZ] writing to a file or template In reply to
You probably want something like:

print $tmpl->output;

The error you are getting is just telling you that you are using a variable '$temp_hdr' which you have not defined beforehand.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] writing to a file or template In reply to
looks like you where right

Now I am getting some where it is out putting it but each film is out putted as

</tr>
<tr>
<td>
Al's Lads
</td>
<td>
9
</td>
</tr>

but I need it to be written like this

da[0]=new dataArray('10 things','movie (dvd)');
da[1]=new dataArray('2 fast 2 furious','movie (VHS)');
da[2]=new dataArray('25th hour','movie (LAZERDISK)');

very close :-) looking for the next step
Quote Reply
Re: [SimonTOZ] writing to a file or template In reply to
well the code moves on and here is one of the bugs that I need help on

I would need help on is the SQL cleanup at the moment the html that is out putted for a film that has a ' is as follows
example for al's lads.

Code:
tableData[8]=new dataArray('<img src="http://www.indiekino.com/indiekinos...lads_poster.jpg" height="140" width="95" alt="image" align="absmiddle">','http://www.imdb.com/title/0268130/[Al's Lads]','9');


as you can see these leads to problems my html javascript code thinks that the ' is a parameter end so I need to find out if I have a ' in the film title
that I escape it with a / example Al\'s lads can a clean up of the film title be done before it is entered in to the template ?

as you can see these leads to problems my html javascript code thinks that the ' is a parameter end so I need to find out if I have a ' in the film title
that I escape it with a / example Al\'s lads can a clean up of the film title be done before it is entered in to the template ?



Code:

use strict; use DBI; use CGI; use CGI::Carp qw(fatalsToBrowser); use HTML::Template; my $query = CGI->new(); my $db_server = "localhost"; my $db_user = "video"; my $db_password = "pass"; my $db_database = "Video"; my $dbh = DBI->connect("dbi:mysql:$db_database:$db_server",$db_user,$db_password) || die("Can't connect"); ## Define your query and return the data in a format that ## you can easily stick into a template my $SELECT = "SELECT imgurl, imdbID, title, Mediatype FROM videodata WHERE Mediatype <> '50' ORDER BY title"; my $sth = $dbh->prepare($SELECT); $sth->execute(); my ($imgurl,$imdbID,$title,$MediaType); $sth->bind_columns(\($imgurl,$imdbID,$title,$MediaType)); my @rows; my $count; while ($sth->fetch) { push @rows, { count => $count, imgurl => $imgurl, imdbID => $imdbID, title => $title, MediaType => $MediaType, }; $count++; } ##Define Your Template my $tmpl = HTML::Template->new( filename => "dummy2.tmpl"); ## Feed the information you got from the query (@rows) into your template $tmpl->param(rows => \@rows); ## Add the record count to the template ##$tmpl->param(count => $count); ## Output the html content header print $query->header( -type => 'text/html'); ## Print the template print $tmpl->output;