Welcome Guest, Not a member yet? Register   Sign In
how to code for sort and filter
#1

[eluser]aussie1855[/eluser]
I have a database with a field that i need to sort in alphabetically order, filter the results so that there are no duplicates names then send the results to my view.php file for display. The method should not delete the duplicates from the table.

Can anyone point me in the right direction.


Thanks


Michael
#2

[eluser]fs_xyz[/eluser]
For sorting you should use $this->db->order_by('<field name>','asc or desc'); ( ascending for small to big / a-z, descending for the other way around )

To display no duplicates you can use $this->db->group_by('<field name>').
Foe easier use, fill field name by primary key of your table.

for example :

Code:
$this->db->select('*');
$this->db->from('A');
$this->db->group_by('id_a');
$this->db->order_by('nm_a','asc');
#3

[eluser]aussie1855[/eluser]
Thanks for the suggestion.

I have used the code as shown in the attachment and i have mark all as $query so that it passes the data from query to the next.
However it gives me the error message ' Parse error: syntax error, unexpected '}' in C:\xampp\htdocs\CI\application\models\family_model.php on line 11 '

Any suggestions as to how to fix this? I have used print_r($query) and the problem seems to be the way i trying to get the data from the first query into the others as it does return a value after the first $query but not the others.


Thanks

Michael
Code:
&lt;?php

class Family_model extends Model {
    
    function getAll() {        
        
        $query = $this->db->get('users');
        $query = $this->db->select('*');
        $query = $this->db->group_by('surname');
        $query = $this->db->order_by('surname','asc');
        
                        
        if($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
        return $data;
        }
    }
    
}
?&gt;
#4

[eluser]jabga[/eluser]
Don't close php tag ?&gt;
Tongue
#5

[eluser]fs_xyz[/eluser]
Wait... that is not how to write the code when using CI's custom query.
And also, to access a table, you need to use from, not get. Get is for the result.

It should be like this :

Code:
$this->db->select('*');
$this->db->from('users'); //I assume users is the table, right ?
$this->db->group_by('surname'); //no need to use group by since the result should already different between records.
$this->db->order_by('surname','asc');

$query = $this->db->get();

//the rest is the same with your code...
#6

[eluser]aussie1855[/eluser]
Thanks fs_xyz

That worked great and yes the table was called users. By the way I did need to use the group by otherwise it should be all of the surnames rather than just 1 of each.

Michael

PS jabga thanks for reply but that did not work.




Theme © iAndrew 2016 - Forum software by © MyBB