[eluser]ravengerpl[/eluser]
Hello!
I have a question. I have controller "movie" with function "index()".
Index() is loading model and view.
Code:
public function index()
{
$this->load->model('moviemodel');
$data['q'] = $this->moviemodel->get_all();
$this->load->view('index', $data);
}
I also have function "sort($sort_key, $sort_type)" in my "movie" controller (it also loading model and view).
Code:
public function sort($sort_key, $sort_type)
{
$this->load->model('moviemodel');
$data['q'] = $this->moviemodel->sort_and_get($sort_key, $sort_type);
$this->load->view('index', $data);
}
getall() and sort_and_get($sort_key, $sort_type) are getting all rows from one table (but sort_and_get also sort by key (column name) and type (asc, desc).
Is it possible to merge them and set default values for variables sort_type and sort_key?
I would like to have something like this:
- when I'm typing example.com/movie/index.html I would like to load page with full list of movies from database (sorted by title, asc - this would be default)
- when I'm typing example.com/movie/index/$sort_key/$sort_type I would like to load page with full list of movies from database sorted by key and type from URI
I was trying something like this but that didn't work.
Code:
public function index($sort_key, $sort_type)
{
if ( empty($sort_type) || empty($sort_key)
{ $sort_type = 'title';
$sort_key = 'asc';
}
$this->load->model('moviemodel');
$data['q'] = $this->moviemodel->sort_and_get($sort_key, $sort_type);
$this->load->view('index', $data);
}
Or should I use routing and redirect from example.com/movie/sort to index?