CodeIgniter Forums
Sorting not working for dynamic values. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Sorting not working for dynamic values. (/showthread.php?tid=63302)



Sorting not working for dynamic values. - prasad - 10-16-2015

Hello everybody,

I'll get right to it.

I am trying to a sort user list by either desc or asc.

My controller looks like this -
PHP Code:
public function ByUser($sort_by 'issue_date'$sort_order 'desc')
    {
        
/**
         * Store _POST value from dropdown in variable - $user_type.
         * Pass that value to model to get issues only "by that user"
         */
        
$user_type $this->input->post('issues-by-user');
        
        
$data['records'] =  $this->report_model->issues_by_user($sort_by$sort_order$user_type);
        
        
$data['main_content'] = 'report';
        
$data['title'] = 'MarksMate Report';
        
$this->load->view('includes/template'$data);
    } 

In my model, i have an inner join that joins two tables and shows issues by specific user.

My model method looks like this -
PHP Code:
public function issues_by_user($sort_by$sort_order$user_type)
    {
        
$sort_order = ($sort_order == 'asc') ? 'asc' 'desc';
        
$sort_columns = array('id''issue_date');
        
$sort_by = (in_array($sort_by$sort_columns)) ? $sort_by 'issue_date';

        
$query $this->db->select('tblissue.id, student_id, issue_date, issue_title, issue_description, issue_screenshot, first_name, last_name, email_id, role')
                        ->
from('tblissue')
                        ->
join('tblstudentprofile''tblissue.student_id=tblstudentprofile.id')
                        ->
where('role'$user_type)
                        ->
order_by($sort_by$sort_order);

        
$records $query->get()->result_array();
        
        return 
$records;
    } 

Query works fine. (Checked it using print_r)

But when i change the url to - "http://localhost/anothertrack/report/ByUser/id/asc", all the records that were fetched go away. If i print_r, all i see is an empty array.

However, if i remove the variable
PHP Code:
$user_type 
from
PHP Code:
->where('role'$user_type
and instead put a constant value like 0 or 1 or 2 (0,1,2,3 are my enum values set in database) then it works.

Where am i going wrong here?


RE: Sorting not working for dynamic values. - ignitedcms - 10-17-2015

This suggests your post for usertype is not being fetched correctly.

Can you show you html for the drop down?


RE: Sorting not working for dynamic values. - lazyfox - 10-18-2015

Try to figure it out by displaying the $user_type's value before query. if it is null value then something wrong there. That's why you get an error in your code/query. I suggest you to initialize your variable $user_type to 1 or any default value from your database ($user_type = 1).

Hope it will help Smile

God bless and more power!


RE: Sorting not working for dynamic values. - Avenirer - 10-19-2015

If you change the url then you have no more POST data... only GET data.