Patrick C
Patrick C

Reputation: 789

Conditional URI segment error - Codeigniter

I have sort of a weird problem, so bear with me. I'm using the _remap function to implement an example.com/user/username protocol in my URI and I am using the following code:

function _remap()
    {       
                //URI Segment I am retrieving (this part works for me)
        $profile_name = $this->uri->segment(2,0);
                // Query the DB where user_name (column in DB) == $profile_name
        $query = $this->db->get_where('users', array('user_name' => $profile_name));
                    // Load user data when URI segment is retrieved, load page
            foreach($query->result() as $row){
                $this->load->view('user_view', $data);          
            }       

}

So my problem is, whenever I type in an INVALID URI segment, i.e. it isn't found in the database, it just returns a blank page. I've tried a bunch of conditional statements, but basically I want this algorithm:

if $profile_name = FOUND (in DB)
display page
else 
redirect to error page

Like I said, I am able to get it to accept valid DB user_name, but with an invalid one it'll just display a blank page. I figured it was because I included the 0 argument in segment(2,0) function. Let me know what you think... Thanks so much all!

P.S. Just in case you are wondering why I am not using routing features, I wasn't sure if I could do all this with routing (checking it against the DB anyway).

Upvotes: 2

Views: 1731

Answers (2)

Mark Eirich
Mark Eirich

Reputation: 10114

Just before your foreach, insert this:

if (!$query->num_rows()) {
    $this->load->helper('url');
    redirect('error_page_uri_here');
}

Upvotes: 3

Shivaas
Shivaas

Reputation: 694

You do not need to return 0, as the URI class will return FALSE if no segment is found at that position (which is as good as returning 0)

 function _remap()
    {       
                //URI Segment I am retrieving (this part works for me)
        $profile_name = $this->uri->segment(2);

        if(!$profile_name){
            redirect('error_page');
        }
        // Query the DB where user_name (column in DB) == $profile_name
        $query = $this->db->get_where('users', array('user_name' => $profile_name));
        // Load user data when URI segment is retrieved, load page

       /* 
        *  Assuming $query returns false if no records are found.  
        *  Or else substitute with another condition
        */

        if($query){ 
            foreach($query->result() as $row){
                $this->load->view('user_view', $data);          
            }
        }else
             show_error('msg goes here', 404);       

}

Now to your other question, you can easily do this by setting custom routing rules, and doing the user DB check in the method that you route to (so you would rename _remap to an actual method, lets call it *fetch_user($username)* for discussion sake)

In your routes.php, add this at the end:

$route['user/(:any)'] = "user/fetch_user";

URI Routing Reference

Your new fetch_users function:

function fetch_user($username)
 { 
     // first check if $username has a value or not. We don't want to run a query if this is null.      
    if(!$username)
        redirect('to_some_page')

    $query = $this->db->get_where('users', array('user_name' => $username));

    /* 
    *  Assuming $query returns false if no records are found.  
    *  Or else substitute with another condition
    */

    if($query){ 
       foreach($query->result() as $row){
             $this->load->view('user_view', $data);          
       }
    }else
      show_error('msg goes here', 404);       

}

Upvotes: 2

Related Questions