Welcome Guest, Not a member yet? Register   Sign In
Sorting not working for dynamic values.
#1

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?
Reply
#2

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

Can you show you html for the drop down?
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#3

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!
Reply
#4

(This post was last modified: 10-19-2015, 03:26 AM by Avenirer.)

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




Theme © iAndrew 2016 - Forum software by © MyBB