Codeigniter Rest API paginate json array output
<p>How can I paginate a json array response from codeigniter rest api controller? Can I make the controller get function to read two variables (one for controller function and one for page number for pagination function on CI) like this?</p>

<pre><code>public function index_get($kost_name, $page_num){
        $real_name = urldecode($kost_name);
        $kost_list = $this-&gt;model-&gt;get_kost_name($real_name,$page_num);
        //var_dump($kost_detail);
        // echo "before ". (microtime(true) - $start);

        // Check if the requested kost was found
        if(!$kost_list){
            $response_data["code"] = API_V1_NO_DATA_STATUS_CODE;
            $response_data["message"] = API_V1_NO_DATA_STATUS_MESSAGE;
            $response_data["info"] = "Your requested kost was not found in our system.";
            $this-&gt;response($response_data, 404);
            return 0; // not really needed, but why not?
        }else{
            $response_data["code"] = API_V1_SUCCESS_STATUS_CODE;
            $response_data["message"] = API_V1_SUCCESS_STATUS_MESSAGE;
            $response_data["info"] = "Requested kost was found in our system.";
            foreach ($kost_list as $key =&gt; $kost) {
                $response_data['kost_list'][$key]['kost_id'] = $kost['kost_id'];
                $response_data['kost_list'][$key]['kost_name'] = $kost['kost_name'];
                $response_data['kost_list'][$key]['area_name'] = $kost['area_name'];
                $response_data['kost_list'][$key]['kost_address'] = $kost['kost_address'];
                $response_data['kost_list'][$key]['kost_lat'] = $kost['kost_lat'];
                $response_data['kost_list'][$key]['kost_long'] = $kost['kost_long']; 
                $response_data['kost_list'][$key]['kost_thumbnail'] = 
                    KOST_IMAGE_DIR . $kost['kost_id'] . "/" . $kost['kost_thumbnail'];
                $response_data['kost_list'][$key]['periods']
                    ['daily'] = $kost['daily'] ? true:false ;
                $response_data['kost_list'][$key]['periods']
                    ['monthly'] = $kost['monthly'] ? true:false ;
                $response_data['kost_list'][$key]['periods']
                    ['trimonthly'] = $kost['trimonthly'] ? true:false ;
                $response_data['kost_list'][$key]['periods']
                    ['semester'] = $kost['semester'] ? true:false ;
                $response_data['kost_list'][$key]['periods']
                    ['annually'] = $kost['annually'] ? true:false ;
            }
        $this-&gt;response($response_data);
        var_dump($response_data);
    }
</code></pre>

<p>and the model like this?</p>

<pre><code>function get_kost_name($kost_name, $page_num){
    $page_num2 = 1+(($page_num-1)*10);
    $sql = "SELECT
                    k.KOST_ID kost_id,
                    k.KOST_NAME kost_name,
                    ar.AREA_NAME area_name,
                    k.KOST_ADDRESS kost_address,
                    k.LATITUDE kost_lat,
                    k.LONGITUDE kost_long,              
                    (SELECT ki.IMAGE_URL
                        FROM
                            tb_r_kost_image ki
                        WHERE
                            ki.KOST_ID = k.KOST_ID 
                        LIMIT 1 
                    ) kost_thumbnail,
                    kr.HAS_DAILY daily,
                    kr.HAS_MONTHLY monthly,
                    kr.HAS_TRIMONTHLY trimonthly,
                    kr.HAS_SEMESTER semester,
                    kr.HAS_ANNUALLY annually
            FROM
                    tb_r_kost k,
                    tb_m_area ar,
                    tb_r_kost_rates kr
            WHERE
                    k.KOST_ID = kr.KOST_ID AND
                    k.AREA_ID   = ar.AREA_ID AND
                    k.AVAILABLE = 1 AND
                    k.DELETE_FLAG = 0 AND
                    k.KOST_NAME LIKE '%$kost_name%'
            LIMIT
                    $page_num2, 10";
    return fetchArray($sql, 'all');
}
</code></pre>