PHP、Zend 框架、Apache、MySql。
我想通过单击相应的编辑按钮来编辑列表中的用户。当我单击编辑按钮时,应将相应的用户 ID 发送到应从其访问的控制器。但我似乎无法在控制器中获取用户 ID。
获得 id 后,我想用检索到的数据视图模型填充 edit.phtml 中的字段。
由于我无法访问 ID,因此无法填充字段。
url 类似于 /Sample/user/edit/2 ,其中 2 是用户的 ID。
用户控制器.php
<?php
class UserController extends Zend_Controller_Action
{
protected $_user;
public function init()
{
/* Initialize action controller here */
$this->_user = new Application_Model_User();
}
public function indexAction()
{
// action body
}
public function listAllAction()
{
// action body
$this->view->users = $this->_user->listUsers();
}
public function registerAction()
{
// action body
if($this->getRequest()->isPost())
{
$data = array(
'user_uname' => $this->_request->getParam('uname'),
'user_pwd' => $this->_request->getParam('paswd'),
'user_address' => $this->_request->getParam('address')
);
$this->_user->insert($data);
}
}
public function editAction()
{
// action body
**$u_id = $this->_request->getParam('user_id');**
// print_R("Hi ".$u_id);
// exit;
if($this->_request->isPost())
{
$u_id = $this->_request->getPost('user_id');
//print_R("Hi ".$u_id);
//exit;
}
else
{
**$this->view->user = $this->_user->getUser($u_id);**
}
}
}
模型类
<?php
class Application_Model_User extends Zend_Db_Table_Abstract
{
protected $_name="tbl_user";
public function listUsers()
{
// action body
$sql = "select * from tbl_user";
$result = $this->_db->query($sql);
return $result->fetchAll();
}
public function getUser($id)
{
$query = "select * from tbl_user where user_id = ?";
return $this->_db->fetchRow($query,array($id));
}
}
列表用户.phtml
<html>
<head></head>
<body>
<b><center>List of Users</center></b>
<form name="list_users" method="post" action="">
<table>
<tr><th>ID</th>
<th>Name</th>
<th>Password</th>
<th>Address</th>
<th>Action</th>
</tr>
<?php foreach ($this->users as $usr): ?>
<tr>
<td><?php echo $usr['user_id'] ?></td>
<td><?php echo $usr['user_uname'] ?></td>
<td><?php echo $usr['user_pwd'] ?></td>
<td><?php echo $usr['user_address'] ?></td>
<td><a href="<?php print $this->baseUrl() ?>/user/edit/<?php print $usr['user_id'] ?>">Edit</a></td>
<td><a href="<?php print $this->baseUrl();?>/user/delete/<?php print $usr['user_id']; ?>">Delete</a></td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan=2><a href="/Sample/user/register">Add More Users</a></td>
</tr>
</table>
</form>
</body>
</html>
编辑.phtml
<html>
<head></head>
<body>
<form name="user_edit" method="post" action="<?php print $this->baseUrl(); ?>/user/edit">
<b><center>Edit Profile</center></b>
<table>
<tr>
<td>Username</td>
<td><input type="text" name="uname" id="uname1" value="<?php print $this->user['user_uname'] ?>"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="paswd" id="paswd1"/></td>
</tr>
<tr>
<td>Address</td>
<td><textarea type="text" name="address" id="address1"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='edit_user' value='Update User'/></td>
</tr>
<tr>
<td colspan=2><a href="/Sample/user/list-all">See All Users</a></td>
</tr>
</table>
</body>
</html>
先感谢您..
我得到了答案。
在用户控制器的编辑操作中更改代码 $u_id = $this->_request->getParam('user_id'); 到 $u_id = $this->getRequest()->getParam('id');