php - Zend Framework Generate HTML in Loop -
i new zend framework , advice on how best loop through database data generate list of links. understanding model should contain of application logic controllers , views being light possible.
i querying db set of records , want loop through them , generate html links. psuedocode below.
controller:
$this->view->mylist = model->generatehtml(); model:
function generatehtml() { query db loop through record set build string of html within loop including links return string controller } view:
echo $this->mylist; this seems put logic in model , leave controller light , view rendering.
one problem have want use $this->view->url generate routing links in html output cannot in model. reading have done online suggests should not building html in model. can generate array of required data in model , return , loop through in either controller or view generate html unsure correct approach , appreciate advice.
thanks given.
new problem - updated code:
hi again....have tired suggestion below have different issue now.
my code now:
model:
not used test. return array similar array created in controller.
controller:
$astorylist = array( array( 'headline' => 'headline 1', 'story' => 'story 1' ), array( 'headline' => 'headline 2', 'story' => 'story 2' ) ); $this->view->astorylist = $astorylist; view:
echo $this->partialloop('partials/storylist.phtml', $this->astorylist); storylist.phtml:
echo "<br />" . $this->headline . $this->story; i have placed partial thus....
views/partials/storylist.phtml
this placement , path used in view derived answer stackoverflow question - where save partial (views) in zend framework, accessible views in app?
when run following error
message: script 'partials/storylist.phtml' not found in path (/home/sites/xxxxx.com/public_html/xxxxxxx/application/views/scripts/)
pulling hair out now!
the model should used pull data data source, should not generating html markup. save html generation view. controller glue between model , view; controller work fetch data , hand off view output generated.
in particular case, partialloop view helper should useful creating markup in loop.
i'd propose following pseudocode instead of had posted above:
controller:
$this->view->mylist = model->getlistofitems(); // return array of data model:
function getlistofitems() { $results = array(); // array of data return // query db // loop on result set foreach($result $row) { $results[] = $row; } return $results; } view:
<?php echo $this->partialloop('mylist.phtml', $this->mylist); mylist.phtml view partial:
<tr> <td><a href="<?php echo $this->url(array('id' => $this->id))"><?php echo $this->username ?></a></td> <td><?php echo $this->firstname ?> <?php echo $this->lastname ?></td> <td><?php echo $this->email ?></td> </tr> to summarize:
- controller queries model data
- model returns array of results
- controller passes array directly view
- view calls
partialloophelper , passes array model partialloophelper iterates on of results, passing them 1 @ timemylist.phtml(note how variable scope becomes local view partial).
my example assumes array returned model contains keys id, username, firstname, lastname.
hope helps, feel free comment if have questions.
Comments
Post a Comment