SpiritFly
SpiritFly

Reputation: 105

How to access $_GET parameters in CodeIgniter?

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

Model articles:

<?php
class Articles_model extends CI_Model {

 public function __construct()
     {
  $this->load->database();
 }

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

  return $query->result_array();

}
} 

Controller:

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

class Json extends CI_Controller {


 public function __construct()
 {
  parent::__construct();
  $this->load->model('articles_model');
 }

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

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

}
}

And my view:

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

<?php
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:

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

//GET page/count
$count=10;$offset=0;
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.

Upvotes: 1

Views: 5278

Answers (2)

Arun
Arun

Reputation: 685

Otherwise you can use



    // if your url come like controller/someview/1/2
    $this->uri->segment(3) // will get 1
    $this->uri->segment(4) // will get 2


Upvotes: 0

Venkata Krishna
Venkata Krishna

Reputation: 4305

You just need to pass parameters like this..........

public function controller_name($param1, $param2) {

      $result = $this->model->get_result($param1, $param2);

      $data = array();
      $data['result'] = $result;
      $this->load->view('view_file_name', $data);
}

Upvotes: 3

Related Questions