1

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.

2
  • 1
    If you read my question more closely you'll notice that I provide that code just as a reference or a primer if you wish, as to what I wish to achieve with Codeigniter. I'm rewriting some old code hence the reason I ask how to complete this in CodeIgniter. I hope the moderator automatically assigned negative rep to my question and I request that it's removed. I can edit my question to point more clearly that the code using mysql_* is used as reference only if needed. I'm very thankful for the notice though, but don't agree that my question should be negatively repped. Commented Nov 19, 2012 at 17:30
  • 1
    @tereško There's no need to be condescending. He DID state that the code containing the antiquated mysql_ functions was "without CI". I'm not sure why you assumed this was his CI code. Commented Nov 19, 2012 at 20:38

2 Answers 2

3

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly the answer I needed. I was thinking about this solution, but wasn't really sure if it's the right approach. Thanks for you answer!
0

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


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.