sort with limit in codeigniter
<p>I created a table which fetches and displays the records from the database with given Limit and Offset. Now i am trying to apply the sorting (desc/asc) in the table when clicked on the heading of any field of table. My purpose is to sort only the results i get with limit and offset but instead it is sorting the entire table and replacing the previous results with the new.</p>

<h2>Controller</h2>

<pre><code>public function dummy(){
    $field = "";
    $sort = "";
    $limit= 2;
    $id= 5;
    $field = $this-&gt;uri-&gt;segment(3);
    $sort = $this-&gt;uri-&gt;segment(4);
    $data['users'] = $this-&gt;admin_model-&gt;sortData($field,$sort,$limit,$id);
    $this-&gt;load-&gt;view('admin/dummy',$data);
}
</code></pre>

<h2>Model</h2>

<pre><code>public function sortData($field,$sort,$limit,$id){
        $this-&gt;db-&gt;limit($limit,$id);
        $this-&gt;db-&gt;order_by($field,$sort);
        $query = $this-&gt;db-&gt;get('employee');
        return $query-&gt;result();
        }
</code></pre>

<p>Here is the example of records which i get before sorting</p>

<pre><code>Firstname|Lastname|BirthDay |Email     |Mobile
Wendy    |moore   |28/7/1990|abc@a.com |xxx
Brad     |hoff    |14/3/1985|xyz@.com  |zzz
</code></pre>

<p>The result i want after sorting based on firstname is</p>

<pre><code>Firstname|Lastname|BirthDay |Email     |Mobile
Brad     |hoff    |14/3/1985|xyz@.com  |zzz
Wendy    |moore   |28/7/1990|abc@a.com |xxx
</code></pre>

<p>The result i am getting now is</p>

<pre><code>Firstname|Lastname|BirthDay |Email     |Mobile
Anna     |Fox     |1/8/1995 |xyz@.com  |zzz
Berry    |Alan    |9/10/1992|abc@a.com |xxx
</code></pre>

<p>See it is sorting the entire table and replacing the results based on previous offset.</p>

<h2>My URL for sorting</h2>

<pre><code>            &lt;?php if($this-&gt;uri-&gt;segment(5)!= 'desc'){ ?&gt;
     &lt;a href="&lt;?php echo site_url('admin/dashboard/'.$geturl.'/firstName/desc');?&gt;"&gt;
&lt;?php } else
        { ?&gt;
    &lt;a href="&lt;?php echo site_url('admin/dashboard/'.$geturl.'/firstName/asc');?&gt;"&gt;
    &lt;?php } ?&gt; FirstName&lt;/a&gt;
</code></pre>