<?php
Class Search_Model extends CI_Model
{
    
    //array with categories to be selected
    function category_options()
    {
        $category_options = array("First Name", "Last Name", "Company", "Position", "City", "State", "Country");
        return $category_options;
    }
    
    //get all user id's
    function list_all_users()
    {
    $query = $this->db->get('users');
    $array = array();

    foreach($query->result() as $row)
    {
        $array[] = $row->id; // add each user id to the array
    }

    return $array;
    }
    
    //get all user fields
    function list_all_fields()
    {
        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
	$sort_columns = array('id', 'first_name', 'last_name', 'company', 'position', 'email', 'phone', 'city', 'state', 'country', 'activated', 'listing');
	$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by 
                        : 'first_name';
		
      $q = $this->db->select('id, first_name, last_name, company, position, email, phone, city, state, country, activated, listing')
		    ->from('users')
		    ->order_by($sort_by, $sort_order);
      
		$ret['rows'] = $q->get()->result();
		return $ret;
    }
    
    
    //search for members
    //$category will be the category item selected
    //$search_for will be whats in the input box
    function member_search($category, $a, $limit, $offset, $sort_by, $sort_order)
    {
        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
	$sort_columns = array('id', 'first_name', 'last_name', 'company', 'position', 'email', 'phone', 'city', 'state', 'country', 'activated', 'listing');
	$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by 
                        : 'first_name';
		
      //Change Below
      $q = $this->db->select('id, first_name, last_name, company, position, email, phone, city, state, country, activated, listing')
		    ->from('users')
	            ->limit($limit, $offset)
		    ->order_by($sort_by, $sort_order);
        
      
      //if there is a length in the first_name 
        
    //  if(strlen($query_array['first_name']))
      //{
      $q->where($category, $a);//$query_array['searchinfo']);
      //}
      
      //save result for all items
      $ret['rows'] = $q->get()->result();
                
                
                // count query
		$q = $this->db->select('COUNT(*) as count', FALSE)
			->from('users');
		
                //get result
		$tmp = $q->get()->result();
		
                //set result of count
		$ret['num_rows'] = $tmp[0]->count;
                 
		
		return $ret;
    }
    
    //search for members
    //$category will be the category item selected
    //$search_for will be whats in the input box
    function member_search_no_where_clause($category, $limit, $offset, $sort_by, $sort_order)
    {
        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
	$sort_columns = array('id', 'first_name', 'last_name', 'company', 'position', 'email', 'phone', 'city', 'state', 'country', 'activated', 'listing');
	$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by 
                        : 'first_name';
		
      //Change Below
      $q = $this->db->select('id, first_name, last_name, company, position, email, phone, city, state, country, activated, listing')
		    ->from('users')
	            ->limit($limit, $offset)
		    ->order_by($sort_by, $sort_order);
        
      
      //if there is a length in the first_name 
        
    //  if(strlen($query_array['first_name']))
      //{
      //$q->where($category, $a);//$query_array['searchinfo']);
      //}
      
      //save result for all items
      $ret['rows'] = $q->get()->result();
                
                
                // count query
		$q = $this->db->select('COUNT(*) as count', FALSE)
			->from('users');
		
                //get result
		$tmp = $q->get()->result();
		
                //set result of count
		$ret['num_rows'] = $tmp[0]->count;
                 
		
		return $ret;
    }
    
    public function changetype($id, $type)
    {
        
        $data = array(
            'listing' => $type
        );
        
        $this->db->where('id', $id);
        $this->db->update('users', $data); 
    }
    
 
    
}
?>
