Welcome Guest, Not a member yet? Register   Sign In
Returning number of rows
#2

[eluser]skunkbad[/eluser]
Maybe try something like this (untested):

Code:
// MODEL ------------------------------------------
private $query_params = array();

public function localidades_data( $params = FALSE, $count = FALSE )
{
// Set params if this is the first call
if( $params !== FALSE )
{
  $this->query_params = $params;

  // Compute / set the offset for the query
  $this->query_params['offset'] = ( $params['page'] * $params['limit'] ) - $params['limit'];
}

// If this is the actual data query, we want to add a SELECT to our query
if( ! $count )
{
  $this->db->select('x.*, y.*');
}

$this->db->from('localidades x');
$this->db->join('regiones y', 'y.id_region = x.id_region');

// If this is the row count
if( $count )
{
  return $this->db->count_all_results();
}

// If this is the data query
else
{
  // Set the limit / offset
  $this->db->limit( $this->query_params['limit'], $this->query_params['offset'] );

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

  // Return data if there is any
  if( $query->num_rows() > 0 )
  {
   return $query->result();
  }
}

return FALSE;
}

// CONTROLLER -------------------------------------
// The pagination class doesn't allow for multiple config files, so we load configuration the old fashion way
$this->config->load( 'pagination/localidades' );
$pagination_config = config_item('localidades_pagination_settings');

// Set the query params for both count and data queries
$query_params = array(
'limit'      => $pagination_config['per_page'],
'page'       => (int) $page
);

// Get the total rows that match the requested set of localidades
$pagination_config['total_rows'] = $this->localidades_data_model->localidades_data( $query_params, TRUE );

// Initialize pagination and create links
$this->pagination->initialize( $pagination_config );
$view_data['pagination_links'] = $this->pagination->create_links();

// Get the actual localidades data that matches the requested set of localidades
$view_data['localidades_data'] = $this->localidades_data_model->localidades_data();

This is the way I do it in Community Auth


Messages In This Thread
Returning number of rows - by El Forum - 10-14-2012, 07:00 AM
Returning number of rows - by El Forum - 10-14-2012, 08:30 AM
Returning number of rows - by El Forum - 10-14-2012, 08:37 AM
Returning number of rows - by El Forum - 10-14-2012, 08:50 AM



Theme © iAndrew 2016 - Forum software by © MyBB