[ic] Column-names part II

Mike Heins interchange-users@icdevgroup.org
Fri Aug 16 10:56:00 2002


Quoting Rene Hertell (interchange@hertell.com):
> Hi guys,
> 
> I asked about a month ago on how to get the fieldnames from a query
> (http://icdevgroup.org/pipermail/interchange-users/2002-July/022988.html),
> and I got a solution from Mike. After struggling with it now for some time I
> can't still figure out in how to get the column-names out...
> 
> I have a query like this (according to the icfaq, this is the fastest way in
> handling a query, but wouldn't it be better to place the query inside the
> [perl] tags?):
> 
> 
> [query arrayref=my_sel_code sql="select * from address where ((rrka='1') or
> (rral='1')) order by name"][/query]
> 
> [perl]
> my $ary = $Tmp->{my_sel_code};
> foreach $line (@$ary)
>  {
>   my ($code,$name,$title....  +20 fields more...)= @$line;
>     if($name){$out .="$name<br>\n"};
>     if($title){$out .="$title<br>\n"};
> etc.
> 
>     return $out;
> [/perl]
> 
> Could someone be so kind and help me out in how to do the foreach-part so
> that I wouldn't have to specify out all columns separately. The reason is
> that I have this code on 10 different pages, and it is quite a pain to go
> trough each page when I have to add a few columns..

This is basic programming -- you always use arrays. We could build our
own array with pointers, but you can even get help from DBI and IC with
a hash query type...

[perl]
	my $db = $Db{query};
	my $q = 'select * from address';
	my $ary = $db->query( { sql => $q, hashref => 1 });
	defined $ary
		or die errmsg("query error for %s!", $q);

	my @out;
	foreach my $row (@$ary) {
	    push @out, "Address for $row->{code} is $row->{address}";
	}
	return join "<br>", @out;
[/perl]
	
-- 
Mike Heins
Perusion -- Expert Interchange Consulting    http://www.perusion.com/
phone +1.513.523.7621      <mike@perusion.com>

Fast, reliable, cheap.  Pick two and we'll talk.  -- unknown