title: Paginating query results with Codeigniter
body: <p>I'm new to Codeigniter and PHP in general, so bear with me.</p>
  
  <p>I'm attempting to use the codeigniter pagination class to paginate the results returned from a query.  The code below works fine with static queries that don't require any parameters passing. However, it seems to break down when attempting to pass a variable as a parameter in the URL e.g. </p>
  
  <pre><code>localhost/index.php/termsbyletter/index/a  
  </code></pre>
  
  <p>where 'a' is the $letter variable passed to the controller/model.</p>
  
  <p>PHP isn't outputting any errors and the query performs as expected, as does the record_count function within the model.  The result is that all of the query results are displayed, but all on the same page, which stays the same when the pagination links are clicked.</p>
  
  <p>Also, in the model, is there a more efficient way of returning the row count than running the query twice?  I've read on here that this is necessary, and I haven't had any success trying to pass this value any other way.</p>
  
  <p>Here is my controller:</p>
  
  <pre><code> &lt;?php
  
  class Termsbyletter extends CI_Controller
  {
      public function __construct() {
          parent:: __construct();
          $this-&gt;load-&gt;helper("url");
          $this-&gt;load-&gt;model("terms_by_letter");
          $this-&gt;load-&gt;library("pagination");
      }
  
      public function index($letter) {
  
          $config = array();
          $config["base_url"] = base_url() . 'index.php/termsbyletter/index/' . $letter;
          $config["total_rows"] = $this-&gt;terms_by_letter-&gt;record_count($letter);
          $config["per_page"] = 3;
          $config["uri_segment"] = 5;
  
          $this-&gt;pagination-&gt;initialize($config);
  
          $page = ($this-&gt;uri-&gt;segment(5)) ? $this-&gt;uri-&gt;segment(5) : 0;
          $data["results"] = $this-&gt;terms_by_letter-&gt;term($config["per_page"], $page, $letter);
          echo $this-&gt;pagination-&gt;create_links();
  
          $this-&gt;load-&gt;view("/templates/header"); 
          $this-&gt;load-&gt;view("/terms/index", $data);  
          $this-&gt;load-&gt;view("/templates/footer"); 
         }
  }
  </code></pre>
  
  <p>and the model:</p>
  
  <p>
  
  <pre><code>class Terms_by_letter extends CI_Model
   {
      public function __construct() {
          parent::__construct();
              $this-&gt;load-&gt;database();
            }
  
      public function record_count($letter) {
  
            $query = $this-&gt;db-&gt;query("SELECT * FROM news WHERE LEFT(slug, 1) = '$letter'");
               return $query-&gt;num_rows();      
            }
  
      public function term($limit, $start, $letter) {
  
            $this-&gt;db-&gt;limit($limit, $start);
            $query = $this-&gt;db-&gt;query("SELECT * FROM news WHERE LEFT(slug, 1) = '$letter'");
  
  
                if ($query-&gt;num_rows() &gt; 0) {
                foreach ($query-&gt;result() as $row) {
                   $data[] = $row;
  
              }
              return $data;
          }
          return false;
  
  
      }
   }
  </code></pre>
  
  <p>and I'm using something like this to output the results in the view:</p>
  
  <pre><code> &lt;?php
  foreach($results as $data) {
      echo $data-&gt;slug "&lt;br&gt;";
  }
  ?&gt;
     &lt;p&gt;&lt;?php echo $links; ?&gt;&lt;/p&gt;
  </code></pre>