Welcome Guest, Not a member yet? Register   Sign In
Formatting database results
#8

[eluser]brianw1975[/eluser]
What I did when I ended up in this same situation with having to format db results yet not wanting to clutter up my views or controller -- I made a "Transformer" library with various functions to "transform" the data and return it and then send it to the view.

Yes, you still have the "clutter", but at least its all centralized and reusable.

In code you provided above, I would have a function like so:

Code:
class Transformer{

function Transformer(){
//enter log entry for library/helper being loaded -- forget the function args
}
/**
*
* called via Transformer::articleComments($result);
*
*/
function articleComments($data){
foreach($data as $row)
{
    $row['name']       = uc_words($row['name']);//example
    $row['comment']    = htmlentities($row['comment']);//example
    $return['my_data'][] = $row;
}

return $return;
}

or you could leave your foreach in place and simply modify the code to pass a single result to be transformed

Code:
foreach($result as $row)
{
    $data['my_data'][] = Transformer::articleComments($row);
}

but mostly these suggestions are just "smarter" refactoring of your original code...


Messages In This Thread
Formatting database results - by El Forum - 09-17-2009, 11:32 PM
Formatting database results - by El Forum - 09-18-2009, 01:52 AM
Formatting database results - by El Forum - 09-18-2009, 10:28 AM
Formatting database results - by El Forum - 09-18-2009, 07:43 PM
Formatting database results - by El Forum - 09-19-2009, 12:12 AM
Formatting database results - by El Forum - 09-19-2009, 04:54 AM
Formatting database results - by El Forum - 09-19-2009, 12:30 PM
Formatting database results - by El Forum - 09-19-2009, 02:02 PM
Formatting database results - by El Forum - 09-19-2009, 02:14 PM
Formatting database results - by El Forum - 09-19-2009, 10:16 PM
Formatting database results - by El Forum - 09-29-2009, 02:53 PM



Theme © iAndrew 2016 - Forum software by © MyBB