开发者

Codeigniter: How to add href links to curated pagination results

开发者 https://www.devze.com 2023-02-21 11:17 出处:网络
I have done pagination with CodeIgniter and it is listing Results from my database as, Title, Content, Price, and postid. I was wondering how do I make the content (title, content, price) that it is d

I have done pagination with CodeIgniter and it is listing Results from my database as, Title, Content, Price, and postid. I was wondering how do I make the content (title, content, price) that it is displaying show as links(

function index() 
    {
            $this->load->library('pagination');
            $this->load->library('table');

            $this->table->set_heading('postid', 'The Title', 'The Content', 'Price');

            $config['base_url'] = 'http://localhost/ganbaru/index.php/site/index';
            $config['total_rows'] = $this->db->get('posts')->num_rows();
            $config['per_page'] = 10;
            $config['num_links'] = 20;
            $config['full_tag_open'] = '<div id="pagination">';
            $config['full_tag_close'] = '</div>';

            $this->pagination->initialize($config);

            $posts['records'] = $this->db->get('posts', $config['per_page'], $this->uri->segment(3));

            开发者_高级运维$this->load->view('site_view', $posts);
    }

Here is the view code

</head>
<body>  


<div id="container">
                    <h1>Super Pagination with CodeIgniter</h1>
                    <?php echo $this->table->generate($records); ?>
                    <?php echo $this->pagination->create_links(); ?>
            </div>
</body>
</html>


Update sorry did not quite understand

I haven't used the table library but you can create loop and just output the links by using something like:

In Controller:

$query = $this->db->get('posts', $config['per_page'], $this->uri->segment(3));
$posts["records"] = $query->result_array();

In view file:

</head>
<body>  
<div id="container">
<h1>Super Pagination with CodeIgniter</h1>
<table>
<?php foreach($records as $record) { ?>
<tr>
<td><a href="<?php echo site_url('myuri/' . $record["id"]); ?>"><?php echo $record["id"]; ?></a></td>
<td><a href="<?php echo site_url('myuri/' . $record["id"]); ?>"><?php echo $record["title"]; ?></a></td>
<td><a href="<?php echo site_url('myuri/' . $record["id"]); ?>"><?php echo $record["content"]; ?></a></td>
</tr>
<?php } ?>
</table>
<?php echo $this->pagination->create_links(); ?>
</div>
</body>
</html>

I haven't tested this, and is a bit more ugly then your original code but will give you more direct control.>

If I understand you correctly

I don't see where you either echo or store the results of $this->pagination->create_links();, this is what produces the pagination output from the library and what you will need to display to show the links. But as you pointed out you don't know how to surround the output with tags. I tried the example from the user-guide which is :

$this->load->library('pagination');

$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = '200';
$config['per_page'] = '20'; 

$this->pagination->initialize($config); 

echo $this->pagination->create_links();

and it produced :

<strong>1</strong>
<a href="http://example.com/index.php/test/page/20">2</a>
<a href="http://example.com/index.php/test/page/40">3</a>
<a href="http://example.com/index.php/test/page/20">&gt;</a>
<a href="http://example.com/index.php/test/page/180">Last &rsaquo;</a>

which does have the tags. So you should have a look at the example in the user-guide and maybe store the Pagination Output as a variable under the posts so you can display it on the page. For example $posts["pagination"] = $this->pagination->create_links(); And then display it on the page by using echo $pagination.

The Documentation is at - http://codeigniter.com/user_guide/libraries/pagination.html - so you can have a quick read for all the options.

Hope that helps...


The CI table class is quite limited in what it can do.

You may be able to achieve what you want like this:

$table_heading = array(
                       '<a href="some_link">Title</a>', 
                       '<a href="some_link/foo">Price</a>');

$this->table->set_heading($table_heading);

Which will give you direct control to whatever is in the headings row. This is untested but should work.

The other option as Johann suggested is to create the table manually, which would of course give you 100% control of the output.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号