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

Hi, How do I stop an empty table row from being printed if one or both $arr[?] is empty?
sub registerform { $sth=runSQL(qq|SELECT usr.name, sl.sdsc1, sl.surl1, sl.header, sl. +sdsc2, sl.surl2, sl.sdsc3, sl.surl3, sl.sdsc4, sl.surl4, sl.sdsc5, sl +.surl5, sl.sdsc6, sl.surl6, sl.sdsc7, sl.surl7, sl.sdsc8, sl.surl8, s +l.sdsc9, sl.surl9, sl.sdsc10, sl.surl10, sl.sdsc11, sl.surl11 FROM users usr, sitelinks sl # $db_users WHERE usr.userid='$refer' AND sl.userid='$refer' AND sl.status +=1|); @arr=$sth->fetchrow; # # $arr[1]=~ s/\<//g; $arr[1]=~ /(.{1,64}\W)/ms; $link0="<a href=http://$arr[2]>$arr[1]</a>"; # $link1="<tr><td><a href=http://$arr[5]>$arr[4]</a><br><br></tr></t +d>"; $link2="<tr><td><a href=http://$arr[7]>$arr[6]</a><br><br></tr></t +d>"; $link3="<tr><td><a href=http://$arr[9]>$arr[8]</a><br><br></tr></t +d>"; $link4="<tr><td><a href=http://$arr[11]>$arr[10]</a><br><br></tr>< +/td>"; $link5="<tr><td><a href=http://$arr[13]>$arr[12]</a><br><br></tr>< +/td>"; $link6="<tr><td><a href=http://$arr[15]>$arr[14]</a><br><br></tr>< +/td>"; $link7="<tr><td><a href=http://$arr[17]>$arr[16]</a><br><br></tr>< +/td>"; $link8="<tr><td><a href=http://$arr[19]>$arr[18]</a><br><br></tr>< +/td>"; $link9="<tr><td><a href=http://$arr[21]>$arr[20]</a><br><br></tr>< +/td>"; $link10="<tr><td><a href=http://$arr[23]>$arr[22]</a></tr></td>"; # my $templ=opentemplate('regform','normal'); substemplate($templ,'fullname',$arr[0]); substemplate($templ,'header',$arr[3]); substemplate($templ,'link0',$link0); substemplate($templ,'link1',$link1); substemplate($templ,'link2',$link2); substemplate($templ,'link3',$link3); substemplate($templ,'link4',$link4); substemplate($templ,'link5',$link5); substemplate($templ,'link6',$link6); substemplate($templ,'link7',$link7); substemplate($templ,'link8',$link8); substemplate($templ,'link9',$link9); substemplate($templ,'link10',$link10); substemplate($templ,'refer',$refer?$refer:'none'); print $templ; }
qw(I think I have painted myself into a corner.) Thank you in advance.

Replies are listed 'Best First'.
Re: Empty table row
by cleverett (Friar) on Jul 26, 2003 at 05:04 UTC
    Try something like
    @arr=$sth->fetchrow or do { print ''; return; }
    That way, if the fetch from the database returns an empty array, you do the alternative.

    But, why are you rolling your own template system? Decent template systems will will handle this type of display logic much more cleanly:

    use strict; use HTML::Template; sub registerform { my $templ = HTML::Template->new(filename => $register_template); my $sql = <<EOSQL; SELECT usr.name, sl.sdsc1, sl.surl1, sl.header, sl.sdsc2, sl.surl2, sl.sdsc3, sl.surl3, sl.sdsc4, sl.surl4, sl.sdsc5, sl.surl5, sl.sdsc6, sl.surl6, sl.sdsc7, sl.surl7, sl.sdsc8, sl.surl8, sl.sdsc9, sl.surl9, sl.sdsc10, sl.surl10, sl.sdsc11, sl.surl11 FROM users usr, sitelinks sl WHERE usr.userid='$refer' AND sl.userid='$refer' AND sl.status=1 EOSQL my $sth = $dbh->prepare($sql); $sth->execute; my $row = $sth->fetchrow_hashref; $templ->param($row); print $templ->output; }
    Decent template systems will have if-then-else constructs to implement display logic like what you're trying to implement.

    Take a look at HTML::Template or Template Toolkit.

Re: Empty table row
by simonm (Vicar) on Jul 26, 2003 at 04:59 UTC
    A bit more context would help people help you.

    For example, what does substemplate() do?

    As a guess, I'd say you should to reorganize your code into a loop, and test the strings before calling substemplate(); here's an untested example of what that might look like:

    my $templ=opentemplate('regform','normal'); substemplate($templ,'fullname',$arr[0]); substemplate($templ,'header',$arr[3]); # $link0="<a href=http://$arr[2]>$arr[1]</a>"; substemplate($templ,'link0',$link0); # foreach my $linknum ( 1 .. 10 ) { my $pos = $linknum * 2 + 2; my ( $text, $url ) = @arr[ $pos, $pos + 1 ]; my $link = ( ! $text or ! $url ) ? '' : "<tr><td><a href=http://$url>$text</a><br><br></tr></td>"; substemplate($templ,"link$linknum",$link); } substemplate($templ,'refer',$refer?$refer:'none'); print $templ;