Welcome Guest, Not a member yet? Register   Sign In
A little help understanding the controller
#4

[eluser]xwero[/eluser]
TheFuzzy0ne there is no need to create a show method, just put the code in the index method.

bargainph is right you just have to route the url to go to the index function even with the id parameter.
Code:
$route['(blog/)(\d+)'] = '$1$2'; // i catch the controller because i don't want to repeat myself
And your controller looks like
Code:
class Blog extends Controller
{
   function index($id = '')
   {
       $this->load->model('blog_model','',true); // add 3th param if you don't autoload the database library
       if($id == '' OR ! is_numeric($id))
       {
           $data['posts'] = $this->blog_model->posts();
           $this->load->view('blog_view', $data);
       }
       else
       {
           $data['post'] = $this->blog_model->post($id); // returns row
          
           if( ! isset($data['post']->title)) // no title id must not exist
           {
               redirect('blog'); // url helper function
           }

           $data['comments'] = $this->blog_model->comments($id);

           $this->load->view('blog_id_view', $data);
       }
   }
}


Messages In This Thread
A little help understanding the controller - by El Forum - 04-27-2009, 11:32 PM
A little help understanding the controller - by El Forum - 04-28-2009, 03:33 AM
A little help understanding the controller - by El Forum - 04-28-2009, 06:31 AM
A little help understanding the controller - by El Forum - 04-28-2009, 06:53 AM
A little help understanding the controller - by El Forum - 04-28-2009, 07:05 AM
A little help understanding the controller - by El Forum - 04-28-2009, 07:21 AM
A little help understanding the controller - by El Forum - 04-28-2009, 07:25 AM
A little help understanding the controller - by El Forum - 04-28-2009, 07:43 AM
A little help understanding the controller - by El Forum - 04-28-2009, 07:46 AM
A little help understanding the controller - by El Forum - 04-28-2009, 07:49 AM
A little help understanding the controller - by El Forum - 04-28-2009, 07:56 AM
A little help understanding the controller - by El Forum - 04-28-2009, 07:59 AM
A little help understanding the controller - by El Forum - 04-28-2009, 08:14 AM
A little help understanding the controller - by El Forum - 04-28-2009, 08:57 AM
A little help understanding the controller - by El Forum - 04-28-2009, 09:07 AM
A little help understanding the controller - by El Forum - 04-28-2009, 09:35 AM
A little help understanding the controller - by El Forum - 04-28-2009, 09:50 AM
A little help understanding the controller - by El Forum - 04-28-2009, 09:56 AM



Theme © iAndrew 2016 - Forum software by © MyBB