开发者

Best way to show data in html tables in zend

开发者 https://www.devze.com 2023-02-08 11:53 出处:网络
I am using zend. Sometimes I have to show data from database on a webpage in HTML tables. For now I am doing something like this:

I am using zend. Sometimes I have to show data from database on a webpage in HTML tables. For now I am doing something like this:

in IndexController's IndexAction:

$myModel = new Model_MyTable_Object();
$data = $myModel->getAllRecords();
$this->view->show = $data->toArray(); 

And in index.phtml

<table>
 <tr>
  <th>id</th>
  <th>FirstName</th>
  <th>LastName</th>
  <th>Locaion</th>
 </tr>

 <?php

 foreach( $this->show as $data ) {

  echo "<tr>
          <td>" . $data['id'] . "</td>
          <td>" . $data['firstname'] . "</td>
          <td>" . $data['lastname'] . "</td>
          <td>" . $data['location'] . "</td>
        </tr>";
 }
 ?>

</table>

Is there any good way to do th开发者_StackOverflow社区is in Zend. I have seen somewhere, Where a PHP class is created for each data grid and where ever we need this then we create instance of that class in Action and render that object in phtml to show data in html table format something like this:

 $this->data->render();

How can we do this ? Any good example, tutorial or link.


I think that a good way would be to use partialLoop view helper. For example, you could create partial view file called myTableRow.phtml in APPLICATION_PATH/views/scripts/_partials/ as follows:

<!--APPLICATION_PATH . '/views/scripts/_partials/myTableRow.phtml -->
<tr> 
    <td> <?php echo $this->id;                       ?> </td>
    <td> <?php echo $this->escape($this->firstname); ?> </td>
    <td> <?php echo $this->escape($this->lastname);  ?> </td>
    <td> <?php echo $this->escape($this->location);  ?> </td>
</tr>

Then your index.phtml would be as follows:

<table>
    <tr>
        <th>id        </th>
        <th>FirstName </th>
        <th>LastName  </th>
        <th>Locaion   </th>
    </tr>
    <!-- I assume that $myModel->getAllRecords();  returns an instance of Zend_Db_Table_Rowset_Abstract --> 
    <?php echo $this->partialLoop('_partials/myTableRow.phtml', $this->show); ?>

</table>

If you need to do it in many views than you could create your own view helper that would take your data, construct a table and return it as string to the view.

As far as doing $this->data->render(); goes, IMHO, it is not a very good way. The reason is that you would need to embed presentation of data into your models. However, by using ZF you most likely want to use its MVC system. The principal of MVC is to separate models, controllers and views. By doing $this->data->render() you would be actually mix models with views.


Create a View Helper

<?php
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(Zend_Db_Table_Rowset_Abstract $rowset,$border=0) {
        $table = "";
        if(count($rowset)>0) {
            $table .= '<table border="'.$border.'"><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>';
                }
            }
            $table .='</tr></table>';
           }
           return $table;
    }
}
?>

In your view then

<?php
    echo $this->displayGenericTableHelper($this->points,0);
?>


You could create your own view helper to do that. http://framework.zend.com/manual/en/zend.view.helpers.html


If you created a descendant class of Zend_Db_Table_Rowset that was connected to your Zend_Db_Table class, you could then implement a render() function on that rowset class that would do what you're talking about.

For example, here's the hypothetical table class, with the rowset attribute defined:

class MyTable extends Zend_Db_Table_Abstract {
    $_name = 'my_table';
    $_rowsetClass = 'MyTableRowset';
}

and here's the rowset class:

class MyTableRowset extends Zend_Db_Table_Rowset_Abstract {
    public function render() {
        // HTML table rendering happens here.
        return $table;
    }
}

and then you would call it in your controller as you indicated above:

$model = new MyTable();
$dataset = $model->getAllRecords(); // Returns an instance of MyTableRowset
$this->view->data = $dataset;

and in the view script:

<?php print $this->dataset->render(); ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号