Best way to show data in html tables in zend

I am using zend. Sometimes I have to display data from a database on a web page in HTML tables. Now I am doing something like this:

in IndexController 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 a good way to do this in Zend. I saw somewhere: where a PHP class is created for each data grid, and where we need it, then we create an instance of this class in Action and render this object in phtml to display data in the format of an html table like this:

 $this->data->render();

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

+3
source share
4 answers

, partialLoop . , myTableRow.phtml APPLICATION_PATH/views/scripts/_partials/ :

<!--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>

index.phtml :

<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>

, , , .

$this->data->render();, IMHO, . , . , ZF, , , MVC. MVC , . $this->data->render(), .

+7

<?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;
    }
}
?>

,

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

Zend_Db_Table_Rowset, Zend_Db_Table, render() , , .

, :

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

rowset:

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

, :

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

and in the script view:

<?php print $this->dataset->render(); ?>
+1
source

Source: https://habr.com/ru/post/1789323/


All Articles