Separation question?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
asgerhallas
Forum Commoner
Posts: 80
Joined: Tue Mar 14, 2006 11:11 am
Location: Århus, Denmark

Separation question?

Post by asgerhallas »

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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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

Post by asgerhallas »

Thanks a lot! That makes sense...

Just one thing: Would I put such a flattening-method in my view-, controller- or model-class?

/Asger
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If it's just flattening the array, I think model, but personally, I don't pay attention to OOP patterns I use, I just build what makes sense. :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

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

Post by asgerhallas »

This was exactly what I was looking for - really hard to figure out how to do such a simple thing correct, I think.

Thank you very very much!
Post Reply