Welcome Guest, Not a member yet? Register   Sign In
filter data by URI (newbie question)
#1

[eluser]Rick Blackdog[/eluser]
Hi, just getting to grips with CI, love it so far.

I've been able to do CRUD stuff with my data but something I haven't been able to do is displaying the data on a page from a specific record as per the URI. What's the best way of doing this?

Thanks for the help

Rick
#2

[eluser]cahva[/eluser]
The easiest way is to use the records id. For example if you have a blog and want to show blog post depending on the id, you would have link something like this:
http://yourblog.com/blog/view_post/2

That means that you have a controller called a blog with method "view_post". 2 would be the parameter to view_post. The controller would be something like this:
Code:
class Blog extends Controller {

    function __construct()
    {
        parent::__construct();
        
        // Youll probably use blog_model from every method from this controller so lets load it in contructor
        $this->load->model('blog_model');
    }
    
    function view_post($id = FALSE)
    {
        // If no id has not been passed via url, just return false(stop execution of this method here)
        if (!$id)
        {
            return FALSE;
        }
        
        $data['post'] $this->blog_model->get_post($id);
        
        $this->load->view('blog/view_post',$data);
    }

Model blog_model would have that get_post($id) method where you grab the post depending on the $id and pass finally pass it to the view with $data.




Theme © iAndrew 2016 - Forum software by © MyBB