Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess an id of a user in a table from a controller
    primarykey
    data
    text
    <p>PHP, Zend Framework, Apache, MySql.</p> <p>I want to edit a user in a list by clicking its corresponding edit button. when i click the edit button, the corresponding users id should be send to the controller from where it should be accessed. But I cant seem to get the user id in the controller. </p> <p>After getting the id , i want to populate the fields in edit.phtml with the data retrieved view model.</p> <p>Since i cant access the id, i cant populate the fields.</p> <p>The url is like /Sample/user/edit/2 where 2 is id of a user.</p> <p>UserController.php</p> <pre><code>&lt;?php class UserController extends Zend_Controller_Action { protected $_user; public function init() { /* Initialize action controller here */ $this-&gt;_user = new Application_Model_User(); } public function indexAction() { // action body } public function listAllAction() { // action body $this-&gt;view-&gt;users = $this-&gt;_user-&gt;listUsers(); } public function registerAction() { // action body if($this-&gt;getRequest()-&gt;isPost()) { $data = array( 'user_uname' =&gt; $this-&gt;_request-&gt;getParam('uname'), 'user_pwd' =&gt; $this-&gt;_request-&gt;getParam('paswd'), 'user_address' =&gt; $this-&gt;_request-&gt;getParam('address') ); $this-&gt;_user-&gt;insert($data); } } public function editAction() { // action body **$u_id = $this-&gt;_request-&gt;getParam('user_id');** // print_R("Hi ".$u_id); // exit; if($this-&gt;_request-&gt;isPost()) { $u_id = $this-&gt;_request-&gt;getPost('user_id'); //print_R("Hi ".$u_id); //exit; } else { **$this-&gt;view-&gt;user = $this-&gt;_user-&gt;getUser($u_id);** } } } </code></pre> <p>Model class</p> <pre><code>&lt;?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-&gt;_db-&gt;query($sql); return $result-&gt;fetchAll(); } public function getUser($id) { $query = "select * from tbl_user where user_id = ?"; return $this-&gt;_db-&gt;fetchRow($query,array($id)); } } </code></pre> <p>ListUser.phtml</p> <pre><code>&lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;b&gt;&lt;center&gt;List of Users&lt;/center&gt;&lt;/b&gt; &lt;form name="list_users" method="post" action=""&gt; &lt;table&gt; &lt;tr&gt;&lt;th&gt;ID&lt;/th&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Password&lt;/th&gt; &lt;th&gt;Address&lt;/th&gt; &lt;th&gt;Action&lt;/th&gt; &lt;/tr&gt; &lt;?php foreach ($this-&gt;users as $usr): ?&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo $usr['user_id'] ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $usr['user_uname'] ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $usr['user_pwd'] ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $usr['user_address'] ?&gt;&lt;/td&gt; &lt;td&gt;&lt;a href="&lt;?php print $this-&gt;baseUrl() ?&gt;/user/edit/&lt;?php print $usr['user_id'] ?&gt;"&gt;Edit&lt;/a&gt;&lt;/td&gt; &lt;td&gt;&lt;a href="&lt;?php print $this-&gt;baseUrl();?&gt;/user/delete/&lt;?php print $usr['user_id']; ?&gt;"&gt;Delete&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php endforeach; ?&gt; &lt;tr&gt; &lt;td colspan=2&gt;&lt;a href="/Sample/user/register"&gt;Add More Users&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>edit.phtml</p> <pre><code>&lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;form name="user_edit" method="post" action="&lt;?php print $this-&gt;baseUrl(); ?&gt;/user/edit"&gt; &lt;b&gt;&lt;center&gt;Edit Profile&lt;/center&gt;&lt;/b&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;Username&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="uname" id="uname1" value="&lt;?php print $this-&gt;user['user_uname'] ?&gt;"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Password&lt;/td&gt; &lt;td&gt;&lt;input type="password" name="paswd" id="paswd1"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Address&lt;/td&gt; &lt;td&gt;&lt;textarea type="text" name="address" id="address1"&gt;&lt;/textarea&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='submit' name='edit_user' value='Update User'/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan=2&gt;&lt;a href="/Sample/user/list-all"&gt;See All Users&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Thank you in advance..</p> <hr> <p>I got the answer.</p> <p>In usercontroller's editaction change the code $u_id = $this->_request->getParam('user_id'); to $u_id = $this->getRequest()->getParam('id');</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload