zend framework - Access an id of a user in a table from a controller -


php, zend framework, apache, mysql.

i want edit user in list clicking corresponding edit button. when click edit button, corresponding users id should send controller should accessed. cant seem user id in controller.

after getting id , want populate fields in edit.phtml data retrieved view model.

since cant access id, cant populate fields.

the url /sample/user/edit/2 2 id of user.

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);**         }     } } 

model class

<?php  class application_model_user extends zend_db_table_abstract {      protected $_name="tbl_user";      public function listusers()     {         // action body         $sql = "select * tbl_user";         $result = $this->_db->query($sql);         return $result->fetchall();     }      public function getuser($id)     {         $query = "select * tbl_user 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 $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 users</a></td>         </tr>         </table>     </body> </html> 

thank in advance..


i got answer.

in usercontroller's editaction change code $u_id = $this->_request->getparam('user_id'); $u_id = $this->getrequest()->getparam('id');

small change needed: change following line:

<td><a href="<?php print $this->baseurl() ?>/user/edit/<?php print $usr['user_id'] ?>">edit</a></td> 

to

<td><a href="<?php print $this->baseurl() ?>/user/edit?user_id=<?php print $usr['user_id'] ?>">edit</a></td> 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -