CodeIgniter Forums
Need a quick fix to sort - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Need a quick fix to sort (/showthread.php?tid=40611)



Need a quick fix to sort - El Forum - 04-13-2011

[eluser]dinorubin[/eluser]
Here is my code -- I just want to be able to display the results by lastname on this page. Thanks!
[code]<?php
class Members extends Controller {

function Members()
{session_start();
parent::Controller();
if($this->session->userdata('userid')<1)
{
redirect(ADMIN_PATH.'welcome/verify','refresh');
exit;
}
$this->load->model(ADMIN_PATH.'MSite_report');
$this->load->model(ADMIN_PATH.'member/Member_model');
$this->load->model(ADMIN_PATH.'MPersonal_document');
$this->load->library('pagination');
$this->load->library('form_validation');
$this->load->library('mylibrary');
$this->load->library('general_function');

}

function index($activated='all',$duplicateid='')
{

$data['title']="Members Management";
//$data['main']='admin_view/member/members';
$data['nav']="admin_view/admin_nav";

$data['status']=$activated;


//set pagination configuration
$config['base_url'] = ADMIN_PATH.'members/index/'.$activated;
$config['total_rows']=$this->Member_model->count_members($activated);
$config['num_links'] = 10;
$config['prev_link'] = '<<';
$config['next_link'] = '>>';
$config['per_page'] = '525';
$config['uri_segment'] = '5';
$offset=$this->uri->segment(5,0);


$this->pagination->initialize($config);



$data['member_list']=$this->Member_model->get_all_members($activated,$config['per_page'],$offset);
$data['export_data']=$this->Member_model->get_for_export($activated);
$data['main']='admin_view/member/members';

$this->load->view('admin_view/dashboard', $data);


}

function type($status='all',$duplicateid='')
{

$data['title']="Members Management";
//$data['main']='admin_view/member/members';
$data['nav']="admin_view/admin_nav";
$data['status']=$status;



//set pagination configuration
$config['base_url'] = ADMIN_PATH.'members/type/'.$status;
$config['total_rows']=$this->Member_model->count_members_type($status);
$config['num_links'] = 10;
$config['prev_link'] = '<<';
$config['next_link'] = '>>';
$config['per_page'] = '25';
$config['uri_segment'] = '5';
$offset=$this->uri->segment(5,0);
$this->pagination->initialize($config);


$data['member_list']=$this->Member_model->get_members_type($status,$config['per_page'],$offset);
$data['main']='admin_view/member/members';

$this->load->view('admin_view/dashboard', $data);

}

function search_member($status='')
{
$data['title']="Members Management";
$data['main']='admin_view/member/members';
$data['nav']="admin_view/admin_nav";
$data['status']=$status;

$s_member =$this->input->post('search_member');
$data['member_list']=$this->Member_model->get_search_results($s_member);
$this->load->view('admin_view/dashboard', $data);
}

function export()
{
$data['title']="Members Management";
$data['main']='admin_view/member/members';
$data['nav']="admin_view/admin_nav";

redirect('export.php?fn=member_list');
}


Need a quick fix to sort - El Forum - 04-13-2011

[eluser]adamfairholm[/eluser]
Is there a certain thing going wrong? You probably need to edit get_all_members() in the member model.


Need a quick fix to sort - El Forum - 04-13-2011

[eluser]dinorubin[/eluser]
Nothing is wrong - it just sorts by ID instead of by lastname which I want it to. Any suggestions/modifications?


Need a quick fix to sort - El Forum - 04-13-2011

[eluser]adamfairholm[/eluser]
The Member model is getting the member list - we'd need to see the code for get_all_members().


Need a quick fix to sort - El Forum - 04-13-2011

[eluser]dinorubin[/eluser]
Sorry - bit of a newbie so hope you can help me. You want the code for the actual page that the table is displaying? It's in the admin section.


Need a quick fix to sort - El Forum - 04-13-2011

[eluser]wh1tel1te[/eluser]
We need to know what this line of code is doing:
Code:
$data[‘member_list’]=$this->Member_model->get_all_members($activated,$config[‘per_page’],$offset);
Show the code for this get_all_members() function.


Need a quick fix to sort - El Forum - 04-14-2011

[eluser]adamfairholm[/eluser]
No problem, dinorubin - like wh1tel1te said, we just need to see the get_all_members function code.

In CodeIgniter's MVC structure, most database calls are made using models, outside of the controllers. Models are used primarily for grabbing data from the database, and that's where you'll find the code that dictates how to order things.


Need a quick fix to sort - El Forum - 04-14-2011

[eluser]dinorubin[/eluser]
&lt;?php
class Member_model extends Model {

function Member_model()
{
parent::Model();
$this->load->library('general_function');
}

function count_members($status)
{
if($status == "yes" || $status == "no"){$this->db->where('activated', $status);}
else
{
if($status != "all")
$this->db->where('activated', $status);
}

return $this->db->count_all_results('member');
}

function count_members_type($status)
{
if($status == "admin" || $status == "user"){$this->db->where('status', $status);}
else
{
if($status != "all")
$this->db->where('status', $status);
}

return $this->db->count_all_results('member');
}

function chk_password($password)
{
$this->db->where('password', $password);
return $this->db->count_all_results('member');
}

function chk_username($username)
{
$this->db->where('user_name', $username);
return $this->db->count_all_results('member');
}


//GET RECORDS OF ALL MEMBERS
function get_all_members($activated,$perpage, $offset)
{
$curent_date=$this->mylibrary->get_local_time("time");

$data=array();
$this->db->from('member');

if($activated == "yes" || $activated == "no"){
$this->db->where('activated', $activated);
$this->db->where('status', 'user');
}
else
{
if($activated != "all")
$this->db->where('activated', $activated);
}
$this->db->order_by("id", "DESC");

$this->db->limit($perpage, $offset);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$data=$query->result_array();
$query->free_result();
}

return $data;
}
//GET RECORDS OF ALL MEMBERS

function get_for_export($activated)
{
$curent_date=$this->mylibrary->get_local_time("time");

$data=array();
$this->db->from('member');

if($activated == "yes" || $activated == "no"){
$this->db->where('activated', $activated);
$this->db->where('status', 'user');
}
else
{
if($activated != "all")
$this->db->where('activated', $activated);
}
$this->db->order_by("id", "DESC");

$query = $this->db->get();
if($query->num_rows() > 0)
{
$data=$query->result_array();
$query->free_result();
}

return $data;
}



function get_members_type($status,$perpage, $offset)
{
$curent_date=$this->mylibrary->get_local_time("time");

$data=array();
$this->db->from('member');

if($status == "admin" || $status == "user"){$this->db->where('status', $status);}
else
{
if($status != "all")
$this->db->where('status', $activated);
}
$this->db->order_by("id", "DESC");

$this->db->limit($perpage, $offset);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$data=$query->result_array();
$query->free_result();
}

return $data;
}


function get_search_results($s_member)
{
$data=array();
$this->db->like('id',$s_member,'after');
$this->db->or_like('fName',$s_member,'after');
$this->db->or_like('lName',$s_member,'after');
$this->db->or_like('email',$s_member,'after');
$this->db->or_like('user_name',$s_member,'after');
$this->db->orderby('fName','DESC');
$query = $this->db->get('member');

if($query->num_rows() > 0)
{
$data=$query->result_array();
$query->free_result();
}

return $data;
}

//FIND RECORDS OF MEMBER BY USERID
function get_member($id)
{
$data=array();
$options=array('id'=>$id);
$query = $this->db->getwhere('member',$options,1);
return $query->row();
}

function get_member_name($id)
{
$data=array();
$this->db->select("fName,lName,user_name");
$options=array('id'=>$id);
$query = $this->db->getwhere('member',$options,1);
return $query->row();
}


Need a quick fix to sort - El Forum - 04-14-2011

[eluser]adamfairholm[/eluser]
On this line:

Code:
$this->db->order_by(“id”, “DESC”);

You need to change "ID" to whatever the last name column is and that should do it.


Need a quick fix to sort - El Forum - 04-14-2011

[eluser]dinorubin[/eluser]
Brilliant! Worked like a charm. Thanks so much!