Re: [htmltmpl] New to this - conditional problem i think
Brought to you by:
samtregar
From: Thomas N. <th...@te...> - 2005-01-21 21:59:54
|
Thanks for the answer..gives me something to look into. I dont think i've really got a grip on hashes..not to mention an array of hashes..but now i have a place to start. The tmpl example was perfect! Once again many thanks from a frozen north. Thomas 2005-01-21 kl. 21.50 skrev Cees Hek: > Thomas Nyman wrote: >> push @{$rows}, $_ while $_ = $sth->fetchrow_hashref(); >> i figured..hey this is a hash so i can use >> if (exists $rows{$fornamn}) etc .. but alas no. apparently push gets >> an array and not a hash if I now get it correctly. > > Actually, what you have is an array of hashes. So $rows contains a > reference to an array, and you are pushing references to hashes onto > that array. So $rows{$fornamn} doesn't make sense, since $rows is an > array reference. What you want is $rows->[0]->{$fornamn}, which will > look for the hash key $fornamn in hash that exists in the first (0th) > entry of the array. > > You can also simplify your while statement above by using some DBI > tricks. This will do the same thing as your while statement above: > > $rows = $sth->fetchall_arrayref({}); > > That will give you an array of hashrefs without needing to worry about > looping and pushing the values individually. > >> Anyway, i was thinking, maybe i can set a conditional in the template >> so that depending on the value of a row i can output different html >> code. What i need is to alert the uses that his or her query resulted >> in no hits. I tried searching in the list but i'm not quite sure what >> to search for. I would very much appreciate it if someone could help >> me in the right direction. > > You should be able to check for the existence of $rows in the template > to see if it contains any values. And you can also do conditionals on > the hash values as well: > > <TMPL_IF rows> > <TMPL_LOOP rows> > <TMPL_IF fornamn> > <TMPL_VAR fornamn><BR /> > <TMPL_ELSE> > no value > </TMPL_IF> > </TMPL_LOOP> > <TMPL_ELSE> > No rows returned > </TMPL_IF> > > Cheers, > > Cees |