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

Thanks to all my brothers /sisters who has assisted me so far.

My script is so far
package TWiki::Plugins::FogbugzTablePlugin; use strict; use DBI; use vars qw( $connection ); # cached connection # DBI specification of the connection to the database my $DB_PATH = "DBI:Sybase:server=192.168.70.56"; # DB user my $DB_USER = 'twiki'; # DB user's password my $DB_PASS = 'twiki'; # TWiki ihook sub initPlugin { return 1; } # TWiki hook; this is run on the text being translated to HTML, and is # your opportunity to expand any tags in that text. sub commonTagsHandler { my($text, $topic, $web ) = @_; #$_[0] refers to the first parameter to this function $_[0] =~ s/\bFog(\d+)/_link_to_bug($1)/ge; $_[0] =~ s/%FOG{(.*?)}%/_show_bugs($1)/ge; } # Connect to the DB sub _connect { my $this = shift; unless( $connection ) { $connection = DBI->connect( $DB_PATH, $DB_USER, $DB_PASS, { PrintError => 0, RaiseError => 1, }); } return $connection; } sub _show_bugs { my $args = shift; my @headers = map { "*$_*" } qw/ ID Title Priority Name fix_for Sta +te /; my $fmt = "| %s | %s | %s | %s | %s | %s |\n"; _connect(); my $sth =$connection->prepare(my $sql = "SELECT Bug.ixBug, Bug.sTitle, + Priority.sPriority, Person.sFullName, FixFor.sFixFor, Status.sStatus + FROM Bug, Priority, Person, FixFor, Status WHERE (Bug.ixPriority=Pri +ority.ixPriority) AND (Bug.ixPersonAssignedTo=Person.ixPerson) AND (B +ug.ixStatus = Status.ixStatus) AND (Bug.ixFixFor = FixFor.ixFixFor) A +ND (Bug.ixBug = $args)"); $sth->execute(); while (my @rows = $sth->fetchrow_array() ) { return join '', map {sprintf $fmt, @$_ } \@headers, \@rows; } $sth->finish; } sub _link_to_bug { my $bugid = shift; return '[[http://apwadev01/fogbugz/default.asp?'.$bugid.'][Fog'.$b +ugid.']]'; } 1;
Now the script bring back everything from one record (which is perfect) but the script is meant to take in a list of id numbers i.e 23,45,3564 and populate a whole table. I am starting to read about fetchrow from the dbi but it seems that will return more than one record that fit a criteria of a single search (am i right?) What i need is that the syntax %FOG{num1,num2,num3}% populates one table with three records the reading in off the numbers is correct but the script above fails becuase it can only do %FOG(num1,num2,num}% .

What should commands should i read that may be helpful apart from fetchall ? Plus where can i find an detailed explanation of map? I want to append the string 'FOG' to the begining of the first field of my result not to Headers or other subsequent fields in the table?

Thanks again

Replies are listed 'Best First'.
Re: What do i need to read to ..
by jdporter (Paladin) on Aug 23, 2006 at 15:23 UTC

    What bothers me a little is this:

    while (my @rows = $sth->fetchrow_array() ) { return join '', map {sprintf $fmt, @$_ } \@headers, \@rows; } $sth->finish;

    That's returning after fetching only a single row, and it also never calls the finish (unless, of course, no rows are returned).

    Maybe this would be better?

    my $result = sprintf $fmt, @headers; while ( my @rows = $sth->fetchrow_array() ) { $result .= sprintf $fmt, @rows; } $sth->finish; return $result;
    We're building the house of the future together.
Re: What do i need to read to ..
by holli (Abbot) on Aug 23, 2006 at 15:26 UTC
    I assume you're basically talking about this part:
    ... AND (Bug.ixBug = $args)");
    You need to make the query fetching multiple id's, right? In this case, use the IN syntax of SQL, like so:
    ... AND (Bug.ixBug IN (23, 45, 3564)");
    Note that this is completely unrelated to using fetchrow_* or fetchall_*. I'm not sure, what your problem with them (the functions) is. fetchrow fetches one row, so you need to loop over the table. fetchall fetches all rows in one go.


    holli, /regexed monk/
Re: What do i need to read to ..
by imp (Priest) on Aug 23, 2006 at 15:18 UTC
Re: What do i need to read to ..
by yoyomonkey (Initiate) on Aug 23, 2006 at 15:30 UTC

    Got the appending working I didn't think to keep it simple ..noob error (which is what i am :) )

    $rows[0]="FOG$rows[0]";

    then the return statement