Welcome Guest, Not a member yet? Register   Sign In
URI segments with index() of a controller?
#1

[eluser]inktri[/eluser]
I've got a User controller. I want users to be able to view their profiles with:
http://localhost/user/<username>

So I added this function into the User controller:
Code:
function index()
    {
        $username = $this->uri->segment(1);
        $users = $this->db->query('select * from users where username = "' . $username . '" limit 1');
        
        if ($users->num_rows > 0) {
            $user = $users->result();
            $data['user_info'] = $user[0];
            $this->load->view('user_view', $data);
        } else {
            $this->load->view('invalid_user_view');
        }
    }

however going to url http://localhost/user/<username> produces a 404 error...
if I rename the function above to view() and use segment(3), and direct my browser to http://localhost/user/view/<username> everything works fine. Anybody know what I'm doing wrong with the index() case?

Thanks
#2

[eluser]wiredesignz[/eluser]
Yep. CI will try to locate the username as a method of the controller. You may like to consider _remap() instead. Or use routes. Good Luck and Welcome to the forums.
#3

[eluser]inktri[/eluser]
thanks _remap() worked




Theme © iAndrew 2016 - Forum software by © MyBB