I have this simple class, that reads a hierarchical structure of elements from a database into an array and sorts it.
Now I would like to present that hierarchi in an HTML-list, but after reading a lot of posts in here, I've come to the conclusion, that it's best to separate the rendering part from the datahandling part (am I right?)...
It sounds so easy in theory, but how do I do this separation in practice?
I can't use a method in my class to render the HTML - that wouldn't be separation I think.
I can't make a simple foreach-loop in my HTML "template", cause it isn't that simple to make the list from the array (I have to use a recursive function).
My questions are:
1. Where should I put the code for rendering the list?
2. Should I make another class for that?
3. or should I just write the rendering code directly in the HTML?
4. or maybe something completely different?
Hope some of you can help me find a clean solution.
/Asger
Separation question?
Moderator: General Moderators
-
asgerhallas
- Forum Commoner
- Posts: 80
- Joined: Tue Mar 14, 2006 11:11 am
- Location: Århus, Denmark
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Since this is special data to handle, you may need to either create a flattening function to make it a single level, continuous array (no recursion needed) then have a basic array-to-HTML rendering function that fills a template entry or entries depending on how your template works and how you'd like the function to render.
-
asgerhallas
- Forum Commoner
- Posts: 80
- Joined: Tue Mar 14, 2006 11:11 am
- Location: Århus, Denmark
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
As this is PHP, it seem like the data access part could just return a multi-dimensional array that expressed the hierarchy. Then pass that to the rendering class.
Code: Select all
$db = new MySQL($dsn);
$list = new List($db);
$writer = new ListWriter($list->fetch());
$writer->useStylesheet('blue');
$output = $writer->toHtml();(#10850)
-
asgerhallas
- Forum Commoner
- Posts: 80
- Joined: Tue Mar 14, 2006 11:11 am
- Location: Århus, Denmark