• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] URI Segments and default value for function

#1
[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?

#2
[eluser]ravengerpl[/eluser]
OK, solved, found my answer here http://ellislab.com/forums/viewthread/132040/

My index() now looks like this:

Code:
public function index($sort_key = '', $sort_type = '')
    {    
        if($sort_key === '' || $sort_type === '')
        {
        $sort_key = 'title';
        $sort_type = 'asc';
        }
    $this->load->model('moviemodel');
    $data['q'] = $this->moviemodel->sort($sort_key, $sort_type);
    $this->load->view('index', $data);
    }


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.