SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    codeigniter pagination

    I recently started to use code igniter and was happy it had pagnitation support. However, I need to page a resultset only, not all records.
    Below is some code:

    First page for search:
    Code:
    <html>
    
    <head>
    <meta http-equiv="Content-Language" content="en-us">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Search string</title>
    </head>
    
    <body>
    
    <form method="POST" action="http://localhost/codeigniter/index.php/blog/index">
      <blockquote>
        <blockquote>
          <p>Search string: <input type="text" name="t1" size="20">&nbsp;&nbsp;&nbsp;
          <input type="submit" value="Submit" name="B1">&nbsp;&nbsp;
          &nbsp;&nbsp;&nbsp;&nbsp; <input type="reset" value="Reset" name="B2"></p>
        </blockquote>
      </blockquote>
    </form>
    
    </body>
    
    </html>
    Controller:
    Code:
    <?php
    class Blog extends Controller {
    
    	function Blog()
    	{
    	parent::Controller();
    	$this->load->helper('url');
    	$this->load->helper('html');
    	}
    
    	function index()
    	{
    		$this->load->library('pagination');
    		$t1 = $this->input->post('t1');
    		$qusql = "SELECT petid, petname, dogpic";
    		$qusql = $qusql . " FROM pets";
    		$qusql = $qusql . " WHERE petname Like '".addslashes("$t1&#37;")."'";		   
    		$qusql = $qusql . " ORDER BY petname asc";
    		
    		$query = $this->db->query($qusql);
    		$data['query'] = $this->db->query($qusql);
    		
    		$this->load->view('blog_view', $data);
    		
    		$tra = $query->num_rows(); 
    		echo $tra;
    
    		$config['base_url'] = 'http://localhost/codeigniter/index.php/blog/index';
    		$config['total_rows'] = $tra;
    		$config['per_page'] = '5'; 
    		$this->pagination->initialize($config); 
    		echo $this->pagination->create_links();
    
    	}
    }
    ?>
    View:
    Code:
    <html>
    <body>
    <table border=1>
    <tr>
    <th>pet id</th>
    <th>pet name</th>
    <th>dog pic</th>
    
    </tr>
    <?php foreach($query->result() as $row): ?>
    <tr>
    <td><?php echo $row->petid ?></td>
    <td><?php echo $row->petname ?></td>
    <?php
    
    if (empty($row->dogpic)) {
    	echo "<td>&nbsp;</td>";
    	}
    else {
    	$tpic = $row->dogpic;
    	// echo "<td><a href=\"http://localhost/codeigniter/upload/$tpic\" target=\"_blank\"><img width=\"80\" border=\"0\" src = \"http://localhost/codeigniter/upload/$tpic\">$tpic</a></td>";
    	echo "<td><a href=\"http://localhost/codeigniter/upload/$tpic\" target=\"_blank\"><img width=\"80\" border=\"0\" src = \"http://localhost/codeigniter/upload/$tpic\"></a></td>";
    
    	}
    ?>
    </tr>
    <?php endforeach; ?>
    </table>
    </body>
    </html>
    The line:
    Code:
    echo $this->pagination->create_links();
    I tryed in controller and view, all I get is all records displayed. Does anyone know how to page a limited resultset like this. By the way, This code works but no paging.
    Last edited by jim9; Mar 9, 2008 at 15:21. Reason: spell

  2. #2
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    pagination

    jim9 here is your answer:
    Throw $t1 at end of base_url and keep track of uri with sessions.
    Make $config['uri_segment'] = '4'; not 3.
    Funny when it is a hard question no one answers. Funny how you can answer a mvc question that others cannot. Well I have a clue!

  3. #3
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or it might just be that your belligerent and rude posting history is why people no longer bother.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •