Zend Framework Database Step By Step Tutorial - Part 5: Now, we create table of data that we have inputed. For this job, we create a methode named "listAction" within controller. Then, create view for that list.
Open "UserController.php" within application/controllers. Add following method:
public function listAction()
{
$params = array('host' =>'localhost',
'username' =>'root',
'password' =>'admin',
'dbname' =>'zend'
);
$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
$DB->setFetchMode(Zend_Db::FETCH_OBJ);
$sql = "SELECT * FROM `user` ORDER BY user_name ASC";
$result = $DB->fetchAssoc($sql);
$this->view->assign('title','Member List');
$this->view->assign('description','Below, our members:');
$this->view->assign('datas',$result);
}
Then, create named "list.phtml" within application/views/scripts/user. Enter following code:
<? include "header.phtml"; ?>
<h1><?=$this->escape($this->title);?></h1>
<h2><?=$this->escape($this->description);?></h2>
<a href="register">Register</a>
<table border="1">
<tr>
<th>ID</th>
<th>User Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Action</th>
</tr>
<? $datas = $this->datas;
for($i = 1; $i<= count($datas);$i++){ ?>
<tr>
<td><?=$datas[$i]['id']?></td>
<td><?=$datas[$i]['user_name']?></td>
<td><?=$datas[$i]['first_name']?></td>
<td><?=$datas[$i]['last_name']?></td>
<td>
<a href="edit/id/<?=$datas[$i]['id']?>">Edit</a>
|
<a href="del/id/<?=$datas[$i]['id']?>">Delete</a>
</td>
</tr>
<? } ?>
</table>
<? include "footer.phtml"; ?>
Point your browser to http://localhost/test/zend/helloworld/ web_root/user/list
