【问题标题】:Codeigniter: How to add href links to curated pagination resultsCodeigniter:如何将 href 链接添加到策划的分页结果
【发布时间】:2011-04-03 03:57:16
【问题描述】:

我已经使用 CodeIgniter 进行了分页,它将我的数据库中的结果列为标题、内容、价格和 postid。我想知道如何将它显示的内容(标题、内容、价格)显示为链接(

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);
    }

这是查看代码

</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>

【问题讨论】:

    标签: php codeigniter pagination hyperlink anchor


    【解决方案1】:

    更新抱歉没太明白

    我没有使用表格库,但您可以创建循环并使用以下内容输出链接:

    在控制器中:

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

    在视图文件中:

    </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>
    

    我没有测试过这个,比你的原始代码更难看,但会给你更直接的控制。>

    如果我理解正确的话

    我看不到您在哪里 echo 或存储 $this-&gt;pagination-&gt;create_links(); 的结果,这是从库中产生分页输出的原因,也是您需要显示的内容以显示链接。但是正如您指出的那样,您不知道如何用标签包围输出。我尝试了用户指南中的示例:

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

    它产生了:

    <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>
    

    确实有标签。因此,您应该查看用户指南中的示例,并可能将分页输出存储为posts 下的变量,以便您可以在页面上显示它。比如$posts["pagination"] = $this-&gt;pagination-&gt;create_links();,然后使用echo $pagination显示在页面上。

    文档位于 - http://codeigniter.com/user_guide/libraries/pagination.html - 因此您可以快速阅读所有选项。

    希望对您有所帮助...

    【讨论】:

    • 感谢您的回复。我想我不够清楚,因为我对分页链接没有任何问题,我对它正在管理的数据库内容有问题。因此,诸如标题、内容、价格之类的内容只是显示无法单击的文本。我希望这会有所帮助非常感谢您的帮助
    • 哇,非常感谢!非常感谢您抽出宝贵的时间来做这件事,如果没有您,我不可能这么快就解决这个问题!
    【解决方案2】:

    CI table class 的功能非常有限。

    你也许可以像这样实现你想要的:

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

    这将使您可以直接控制标题行中的任何内容。这是未经测试的,但应该可以工作。

    Johann 建议的另一个选项是手动创建表,这当然可以让您 100% 控制输出。

    【讨论】:

    • 感谢您的帖子!我喜欢 Johann 的回答,因为它让我能够灵活地真正控制桌子的各个方面。我很感激:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 2015-06-25
    • 2014-01-18
    • 2011-06-15
    • 1970-01-01
    • 2016-01-24
    相关资源
    最近更新 更多