How to access $_GET parameters in CodeIgniter?

I use codeigniter to generate JSON. This is my current structure:

Model articles:

class Articles_model extends CI_Model {

 public function __construct()

 public function get_articles()
  $this->db->order_by("HdOrder", "asc");
  $query = $this->db->get('articles');

  return $query->result_array();



<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Json extends CI_Controller {

 public function __construct()

 public function index()
 $data['articles'] = $this->articles_model->get_articles();

 $this->load->view('json', $data);


And my view:

<?php header('Access-Control-Allow-Origin: *'); ?>

echo '{"articles":'.json_encode($articles).'}';

Now I need to pass two get parameters to this json so I can return it in pages. I have done it in php and it works. Below is the php code without CI:

$link = mysql_pconnect("localhost", "user", "pass") or die("Could not connect");
mysql_select_db("dbase") or die("Could not select database");

//GET page/count
if (isset($_GET['count'])) $count=$_GET['count']*1;
if (isset($_GET['page']))  $offset=$_GET['page']*$count*1;

$arr = array();

$rs = mysql_query("SELECT * FROM bannersright 
                   ORDER BY HdOrder 
                   LIMIT $offset,$count"); #<-HERE

while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;

echo '{"bannersright":'.json_encode($arr).'}';


Now how do I do this in my CodeIgniter structure? How should I pass my $offset and $count variables from my view-controller-model to set them as limit values for the mysql query? Actually I have found a tutorial on how to pass a varuable to the controller But I’m still not sure how to pass the same values to the model and then return the query to the same controller. And I’m not pretty sure if that is the correct approach to the problem.

I would like to solve it as simple as possible though.

Leave a Reply


Hire Me
Follow Me!
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.