Graeme McLaren wrote:
> Hi Rob,
>  
> Basically I want the data displayed in a table, for example (missed out
> Euros):
>  
> <table>    
> 
>   <tr>
>     <td>Sterling</td>
>     <td>Dollars</td>
>     <td>Euros</td>
>   </tr>    
> 
>   <tr>      
>     <td>Sterling to Euros</td>
>     <td>1.5</td>
>     <td>Dollars to Sterling</td>
>     <td>1.98</td>
>     <td>Euros to Sterling</td>
>     <td>0.72</td>    
>   </tr>    
> 
>   <tr>     
>     <td>Sterling to Dollars</td>
>     <td>2.05</td>
>     <td>Dollars to Euros</td>
>     <td>0.75</td>
>     <td>Euros to Dollars</td>
>     <td>1.52</td>    
>   </tr>  
> 
> </table>  
>  
> Which produces:
> 
> <table>
> 
>   <tr>
>     <td>Sterling</td>
>     <td></td>
>     <td>Dollars</td>
>     <td></td>
>     <td>Euros</td>
>     <td></td>
>   </tr>                             
> 
>   <tr>                               
>     <input type="hidden" name="id" value="2">                  
>     <td width="50%" height="22">
>       <p><b>Sterling -> Euros</b></p>
>     </td>                  
>     <td width="50%" height="22">
>       <input type="text" name="rate" size="5" value="1.5">
>     </td>
> 
>   <tr>                               
>     <input type="hidden" name="id" value="4">                  
>     <td width="50%" height="22">
>       <p><b>Sterling -> Dollars</b></p>
>     </td>                  
>     <td width="50%" height="22">
>       <input type="text" name="rate" size="5" value="2.05">
>     </td>
> 
>   <tr>                               
>     <input type="hidden" name="id" value="14">                  
>     <td width="50%" height="22">
>       <p><b>Sterling -> Malaysian Ringgit</b></p>
>     </td>                  
>     <td width="50%" height="22">
>       <input type="text" name="rate" size="5" value="6.47">
>     </td>
> 
>     <input type="hidden" name="id" value="1">                  
>     <td width="50%" height="22">
>       <p><b>Dollars -> Sterling</b></p>
>     </td>                  
>     <td width="50%" height="22">
>       <input type="text" name="rate" size="5" value="1.98">
>     </td>                                         
> 
>     <input type="hidden" name="id" value="3">                  
>     <td width="50%" height="22">
>       <p><b>Dollars -> Euros</b></p>
>     </td>                  
>     <td width="50%" height="22">
>     <input type="text" name="rate" size="5" value="0.75">
>     </td>                                         
> 
>     <input type="hidden" name="id" value="16">                  
>     <td width="50%" height="22">
>       <p><b>Dollars -> Malaysian Ringgit</b></p>
>     </td>                  
>     <td width="50%" height="22">
>       <input type="text" name="rate" size="5" value="3.22">
>     </td>                                            
> 
>     <input type="hidden" name="id" value="5">                  
>     <td width="50%" height="22">
>       <p><b>Euro -> Sterling</b></p>
>     </td>                  
>     <td width="50%" height="22">
>       <input type="text" name="rate" size="5" value="0.72">
>     </td>                                          
> 
>     <input type="hidden" name="id" value="6">                  
>     <td width="50%" height="22">
>       <p><b>Euro -> Dollars</b></p>
>     </td>                  
>     <td width="50%" height="22">
>       <input type="text" name="rate" size="5" value="1.52">
>     </td>                                          
> 
>     <input type="hidden" name="id" value="15">                  
>     <td width="50%" height="22">
>       <p><b>Euro -> Malaysian Ringgit</b></p>
>     </td>                  
>     <td width="50%" height="22">
>       <input type="text" name="rate" size="5" value="5.15">
>     </td>
> 
> </table>
>  
> The first (Sterling) column is correct, I can't just add new <tr> tags as it
> would just create a list instead of tablulate the data. I actually have three
> array refs of data as I have three db queries, each organising the data by
> currency type:
>  
>   $html->param(
>     LOOP => [
>       {
>         sterling =>  $sterlingaref,
>         dollars =>  $dollarsaref,
>         euros => $euroaref
>       },
>     ],
>   );
>  
> I don't think it's being displayed correctly because the loops maybe should 
> be nested, I'm not sure on that and not sure how to do it exactly. I've RTFM
> but no luck.
>  
> Any help you can give me on this would be greatly appreciated.

Hi Graeme.

(Please bottom-post your responses to this list, so that lengthy threads can
remain comprehensible. Thanks.)

I see what you're doing now. I didn't understand you were using HTML::Template.
The problem is that your data is in column-order in the structure you're feeding
to $html->param, whereas it needs to be in row order so you need to change the
structure. It's the same as transposing a matrix if you know any maths, and is
discussed in the documentation for HTML::Template starting where it says, "Often
you'll want to generate a <TMPL_LOOP>'s contents programmatically".

I've written a fragment of Perl that does the job. It relies on the number of
elements in the three exchange rate arrays being the same, but I assume that's 
ok.

Hope it helps.

Rob


use strict;
use warnings;

use CGI qw/:standard/;
use HTML::Template;

my $html = HTML::Template->new(filehandle => *DATA);

my $sterlingaref = [
  { id => 2, name => 'Sterling -> Euros', rate => 1.5, },
  { id => 4, name => 'Sterling -> Dollars', rate => 2.05, },
  { id => 14, name => 'Sterling -> Malaysian Ringgit', rate => 6.47, },
];

my $dollarsaref = [
  { id => 1, name => 'Dollars -> Sterling', rate => 1.98, },
  { id => 3, name => 'Dollars -> Euros', rate => 0.75, },
  { id => 16, name => 'Dollars -> Malaysian Ringgit', rate => 3.22, },
];

my $euroaref = [
  { id => 5, name => 'Euro -> Sterling', rate => 0.72, },
  { id => 6, name => 'Euro -> Dollars', rate => 1.52, },
  { id => 15, name => 'Euro -> Malaysian Ringgit', rate => 5.15, },
];

my @rows;
foreach my $i (0 .. $#{$sterlingaref}) {
  push @rows, {
    CURRENCY => [
      $sterlingaref->[$i],
      $dollarsaref->[$i],
      $euroaref->[$i],
    ],
  }
}

$html->param(ROWS => [EMAIL PROTECTED]);

print
  header,
  start_html('Template Question'),
  $html->output,
  end_html;

__END__
<table>

  <tmpl_loop name=ROWS>

    <tr>

      <tmpl_loop name=CURRENCY>
        <input type="hidden" name="id" value="<tmpl_var name=id>">
        <td width="50%" height="22">
          <p><b><tmpl_var name=name></b></p>
        </td>
        <td width="50%" height="22">
          <input type="text" name="rate" size="5" value="<tmpl_var name=rate>">
        </td>
      </tmpl_loop>

    </tr>

  </tmpl_loop>

</table>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to