Welcome Guest, Not a member yet? Register   Sign In
Get Offset Pagination In Generated Json?
#1

[eluser]Unknown[/eluser]
This is a sample of the json format I generate through CI:

Code:
{"bannersleft":[{"Id":"24","HdCreateDate":"2012-11-15 14:17:42","HdOrder":"21","Title":"IMG Title","URL":"http:\/\/www.imgsite.com\/","Image":"http:\/\/www.imgsite.com\/img.gif"}],
"news":[{"Id":"81","HdCreateDate":"2012-11-15 22:17:30","HdOrder":"61","Title":"Some Title","URL":"http:\/\/www.somesite.com\/","Image":"http:\/\/www.imgsite.com\/img.jpg"}],
"articles":[{"Id":"81","HdCreateDate":"2012-11-15 22:17:30","HdOrder":"61","Title":"Some Title","URL":"http:\/\/www.somesite.com\/","Image":"http:\/\/www.imgsite.com\/img.jpg"}],
"bannersright":[{"Id":"24","HdCreateDate":"2012-11-15 14:17:42","HdOrder":"21","Title":"IMG Title","URL":"http:\/\/www.imgsite.com\/","Image":"http:\/\/www.imgsite.com\/img.gif"}]}

I generate it like this:

4 models of bannersleft, news, articles, bannersright. All four have pretty much the same structure. Sample for articles:

Code:
<?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();

}
}

My controller combining the 4 models:

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

class Json extends CI_Controller {


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

public function index()
{
$data['news'] = $this->news_model->get_news();
$data['articles'] = $this->articles_model->get_articles();
$data['bannersleft'] = $this->bannersleft_model->get_bannersleft();
$data['bannersright'] = $this->bannersright_model->get_bannersright();

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

}

public function page($pageNum){
  echo "OK";
}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

And my json.php view that displays the JSON results:

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

<?php

echo '{"bannersleft":'.json_encode($bannersleft).',';
echo '"news":'.json_encode($news).',';
echo '"articles":'.json_encode($articles).',';
echo '"bannersright":'.json_encode($bannersright).'}';

?>


I suppose it's pretty simple what I do, and everything seems to work well. The JSON is generated without problems.

However a need to paginate that json came up. My script sends the offset and count number by the get method. So I have tried to do it outside CI and wrote it in simple php file and it looks very simple:

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

$count=10;$offset=0;
if (isset($_GET['count'])) $count=$_GET['count']*1;
if (isset($_GET['page']))  $offset=$_GET['page']*$count*1; // Added to get page and count

$arr = array();
$arr2 = array();
$arr3 = array();
$arr4 = array();

$rs = mysql_query("SELECT * FROM bannersleft ORDER BY HdOrder LIMIT $offset,$count");
$rs2 = mysql_query("SELECT * FROM news ORDER BY HdOrder LIMIT $offset,$count");
$rs3 = mysql_query("SELECT * FROM articles ORDER BY HdOrder LIMIT $offset,$count");
$rs4 = mysql_query("SELECT * FROM bannersright ORDER BY HdOrder LIMIT $offset,$count"); // Added LIMIT with $offset and $count to all four queries

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

while($obj = mysql_fetch_object($rs2)) {
$arr2[] = $obj;
}

while($obj = mysql_fetch_object($rs3)) {
$arr3[] = $obj;
}

while($obj = mysql_fetch_object($rs4)) {
$arr4[] = $obj;
}

echo '{"bannersleft":'.json_encode($arr).',';
echo '"news":'.json_encode($arr2).',';
echo '"articles":'.json_encode($arr3).',';
echo '"bannersright":'.json_encode($arr4).'}';

?>

Now the question is how do I translate the last piece of code above into my CI? I already have my methods, controllers and views setup as you can see. But how do I add the offset and count to it? I'm a little new to CI, already like it, but this is still mystery to me. How do I pass GET variables from the view to the method? Or maybe some other solution is preferred? What would be the easiest most simple way to add the offset and count to my current CI structure?




Theme © iAndrew 2016 - Forum software by © MyBB