从控制器访问表中用户的id

时间:2013-08-28 10:11:50

标签: zend-framework

PHP,Zend Framework,Apache,MySql。

我想通过单击相应的编辑按钮来编辑列表中的用户。 当我单击编辑按钮时,相应的用户ID应该从控制器的位置发送到控制器。 但我似乎无法获得控制器中的用户ID。

获取id之后,我想用edit检索的视图模型填充edit.phtml中的字段。

由于我无法访问id,我无法填充字段。

网址类似于/ Sample / user / edit / 2,其中2是用户的ID。

UserController.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));
    }
}

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

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

提前谢谢..


我得到了答案。

在usercontroller的editaction中更改代码$ u_id = $ this-&gt; _request-&gt; getParam('user_id'); to $ u_id = $ this-&gt; getRequest() - &gt; getParam('id');

1 个答案:

答案 0 :(得分:0)

需要进行小的更改:更改以下行:

<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/edit?user_id=<?php print $usr['user_id'] ?>">Edit</a></td>