Is it possible to write a Zend View Helper that could present the resultset of any fetchAll() operation as a generic table?.
My model code looks like this
class Model_DbTable_XWZ extends Zend_Db_Table_Abstract
{
protected $_name = 'xwz';
protected $_primary = 'id';
public function getA()
{
$sql = $this->select()
....
return $this->fetchAll($sql);
}
public function getB()
{
$sql = $this->select()
......
return $this->fetchAll($sql);
but rather usin开发者_C百科g Zend_Debug::Dump() to view the results, it would be handy to use a generic view helper.
class Zend_View_Helper_DisplayGenericTableHelper extends Zend_View_Helper_Abstract {
public $view;
public function setView(Zend_View_Interface $view)
{
$this->view = $view;
}
public function displayGenericTableHelper($result)
{
....??
}
}
Something like but i'm not sure how to determine the column names from the $result object.
You could use the function Zend_Db_Table_Row_Abstract::toArray()
, like this:
// in view helper
public function displayGenericTableHelper(Zend_Db_Table_Rowset_Abstract $rowset) {
$table = '<table><tr>';
foreach(array_keys($rowset->current()->toArray()) as $column) {
$table .= '<th>'.$column.'</th>';
}
foreach($rowset as $row) {
$table .= '</tr><tr>';
foreach($row->toArray() as $content) {
$table .= '<td>'.$content.'</td>';
}
}
return $table.'</tr></table>';
}
Use case:
// in a view
<?= $this->displayGenericTableHelper($this->model_data) ?>
alternative call:
// in controller
$model_data = $your_model->getA();
$view->your_table = $view->displayGenericTableHelper($model_data);
// in view:
<?= $this->your_table ?>
Point of improvement: use a partial to keep HTML out of your helper.
See this Zend table html helper, or this http://www.zfsnippets.com/snippets/view/id/17
精彩评论