简体   繁体   English

URI 段在 Codeigniter 中丢失

[英]URI segment lost in Codeigniter

On my codeigniter application, I have a list of users in which an admin can change the role of a user and update the user information.在我的 codeigniter 应用程序中,我有一个用户列表,管理员可以在其中更改用户的角色并更新用户信息。 When editing a user the url is user/edit_user/1002 and the edit_user view is loaded.编辑用户时,url 为 user/edit_user/1002 并加载 edit_user 视图。 1002 is the userid of the user. 1002 是用户的用户 ID。

When I submit the form and form_validation fails I reload the view but the problem is the last segment on the url with the userid is lost.当我提交表单并且 form_validation 失败时,我重新加载视图,但问题是 url 上带有 userid 的最后一段丢失了。 So the sql queries used in the form fails.所以表单中使用的sql查询失败。 Is there a way I can reload the view with the errors and also keep the userid in the uri segment ?有没有办法可以重新加载包含错误的视图并将用户 ID 保留在 uri 段中? Given below is the code:下面给出的是代码:

if ($this->form_validation->run() == FALSE) {

        $userid = $this->uri->segment(3);   
        $this->db->where('userid', $userid);
        $query = $this->db->get('user');
        $row1 = $query->row();      
        $data = (array)$row1;                                                   

        $sql = "SELECT userid, role_id FROM user_role WHERE userid = $userid ";// This sql fails
        $ro = $this->db->query($sql);

        $data['role_check'] = $ro->result(); 
        $this->load->view('edit_user',$data);   
}   

Thanks in advance.提前致谢。

In view_user.php,在 view_user.php 中,

    <tr>
        <td><?php echo $loop->userid;?></td>
        <td><?php echo $loop->name;?></td>                      
        <td><a class="btn-small btn-info" href="<?php echo site_url(); ?>/user/edit_user/<?php echo $loop->userid;?>">Edit</td>

    </tr>

The in controller user, I have used the code I posted first.在控制器用户中,我使用了我首先发布的代码。

Add the user id in the form's action.在表单的操作中添加用户 ID。

Something like this:像这样的东西:

action="user/edit_user/<?php echo $this->uri->segment(3); ?>"

I have solved the problem myself and sharing it in case someone else faces this problem.我自己解决了这个问题并分享它以防其他人遇到这个问题。 Going through the CI docs, I found that the function returns FALSE if the segment does not exist.通过 CI 文档,我发现如果该段不存在,该函数将返回 FALSE。 I have used it to cover all the conditions that an update may face.我已经用它来涵盖更新可能面临的所有条件。 The code is given below:代码如下:

  if($this->uri->segment(3))
        {   $userid = $this->uri->segment(3);   
            $this->session->set_userdata('update_user_id',$userid);
        }               
        else            
            $userid= $this->session->userdata('update_user_id');    

In case of revalidation of the form, $this->uri->segment(3) will return FALSE and the else condition will pick the userid from session which was set when the form was first populated by clicking EDIT link.在重新验证表单的情况下,$this->uri->segment(3) 将返回 FALSE,else 条件将从会话中选择用户 ID,该用户 ID 是在通过单击 EDIT 链接首次填充表单时设置的。

Thank you all.谢谢你们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM