【问题标题】:Pagination on Pyrocms pagesPyrocms 页面上的分页
【发布时间】:2012-08-24 08:31:05
【问题描述】:

我目前正在 pyrocms 页面上显示产品列表。使用插件我从数据库表中获取产品并将它们显示为我想使用分页但不知道如何使用它以及从哪里开始的列表。有谁知道任何教程或有一些建议?

【问题讨论】:

  • 为什么需要在插件中编写代码...??
  • 只是CodeIgniter,做你喜欢的吧!按照 CodeIgniter 文档的说明,从 URL 中获取信息并将其输入分页库。

标签: codeigniter pagination pyrocms


【解决方案1】:

您不需要加载分页库或对其进行初始化。这和你在常规的 codeigniter 中做的有点不同,你也可以使用你在codeigniter pagination class 中做的方式

我通常这样做是为了分页。

在你的控制器中使用这个

    ....
    // Create pagination links
    $total_rows = $this->products_m->count_all_products();
    $pagination = create_pagination('products/list', $total_rows);
    //notice that product/list is your controller/method you want to see pagination there        

    // Using this data, get the relevant results
    $params = array(
        'limit' => $pagination['limit']
    );
    $this_page_products = $this->product_m->get_all_products($params);
    ....
    //you got to pass $pagination to the view
    $this->template
            ->set('pagination', $pagination)
            ->set('products', $this_page_products)
            ->build('products/view');

显然,您将拥有一个名为 products_m 的模型 在您的模型中,这将是您的 get_all_products 函数,我相信您也可以构建 count_all_products() 函数

private function get_all_products($params){
 //your query to get products here
 // use $this->db->limit();

 // Limit the results based on 1 number or 2 (2nd is offset)
        if (isset($params['limit']) && is_array($params['limit']))
                $this->db->limit($params['limit'][0], $params['limit'][1]);
        elseif (isset($params['limit']))
                $this->db->limit($params['limit']);
 //rest of your query and return
}

现在在您的视图中,您必须在传递的 $products 变量中使用 foreach 并在您的视图中使用此行来显示分页链接

<?php echo $pagination['links'];?>

我在我的作品中使用它,希望它有所帮助。

【讨论】:

    【解决方案2】:

    Pyrocms 使用了 codeigniter 框架,这段代码在模块的情况下可以完美运行,但我从未在插件上尝试过这个,但你仍然可以尝试这个。

    在Controller中加载分页库

    $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();
    

    【讨论】:

    • 感谢您的回复,但控制器不涉及简单的页面和插件是我所要做的一切
    • 好的,然后把这段代码放在 plugin.php 中并使用它,你能分享你的 url 以便我更好地理解
    • 谢谢。我找到了一种方法来做到这一点。创建了一个插件和两个用于获取数据和创建链接的插件方法,然后在页面中调用它。
    猜你喜欢
    • 1970-01-01
    • 2012-02-20
    • 2012-10-08
    • 2014-08-21
    • 1970-01-01
    • 2012-03-14
    • 2015-12-15
    • 2013-02-13
    • 1970-01-01
    相关资源
    最近更新 更多