RE: [htmltmpl] Adding to a row in a loop
Brought to you by:
samtregar
From: Kenny S. <ke...@jo...> - 2002-08-06 15:34:01
|
> This means that you should copy the result from > fetchrow_hashref to your array, for example: > > while (my $data = $sth->fetchrow_hashref ('NAME_lc')) { > push (@$rows, { $data }); > } Doh, you're right, someone on this list told me that once before, but I haven't done much since then, so I forgot about it. > Better still, take the effort out of your code and let > the (new) function fetchall_arrayref do it all for you: > > my $rows = $sth->fetchall_arrayref ({}); Yeah, I use this one myself usually, but I thought the above code was easier to understand as an example. Todd, Ok, now that I understand your question (and was reminded of the above)... Does 'uname' have anything to do with the database data? If so, and it's not complicated, I usually just add an item into the select statement... I use the following code to select employees out of the database and set one of them to "selected" so I can populate a select box with the data (the code is stripped down to provide an easier to read example): ----------------- snip ---------------------------- $sql = "SELECT id,name,IF(id=?,1,0) as is_selected ". "FROM employee WHERE status='active" ; $sth = $dbh->prepare( $sql ) ; $sth->execute( $selected_employee_id ) ; $employee_list = $sth->fetchall_arrayref({}); ----------------- end ------------------------------ This way, you don't need to go back and add that one value in. If 'uname' is not a value you can really add to your select statment, then just do this: ----------------- snip ---------------------------- $sth = $dbh->prepare( $sql ) ; $sth->execute( @your_bind_vars ) ; $list = $sth->fetchall_arrayref({}) ; if ( scalar( @{$list} ) > 0 ) { foreach $item ( @{$list} ) { $item->{'uname'} = set_uname() ; } } ----------------- end ------------------------------ Kenny Smith |