【问题标题】:make paging table in codeigniter在codeigniter中制作分页表
【发布时间】:2017-11-08 12:45:01
【问题描述】:

我创建了一个搜索功能,我想在我已经尝试但无法正常工作的结果表中使用分页。我只想显示 10 行,但是当我尝试它时不起作用。

我在https://codeigniter.com/ 中阅读了如何分页,但我仍然不明白如何在我的代码中使用它:

$config['base_url'] = '';
$config['total_rows'] = ;

这是我的代码:

控制器:

function __construct() 
    {
        parent::__construct($securePage=false);
         $this->load->helper('form');
        $this->load->model('tracking_model');
    }

    function index()
    {
        $this->load->view('tracking_view');
                $this->load->library('pagination');
    }

function searchTiket(){
        // Retrieve the posted search term.
        $noTicket = $this->input->post('trackTicket');
        $monthTicket = $this->input->post('trackMonth');
        $yearTicket = $this->input->post('trackYear');
        // Use a model to retrieve the results.

        //pengaturan pagination
        $config['base_url'] = base_url().'';
        $config['total_rows'] = '200';
        $config['per_page'] = '5';

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

//buat pagination
        $data['page'] = $this->pagination->create_links();

// Use a model to retrieve the results.
        $data["result"]= $this->tracking_model->searchTiket($config['per_page'],$noTicket,$monthTicket,$yearTicket);

        // Pass the results to the view.
        $this->load->view('tracking/ticket_list_view',$data);
    }

查看:

<div>
<center>

<?php if(empty($result)){
    echo '<h2>Data not found!</h2>';
}else{
?>

    <table border='1' Width='1000'>  
        <tr bgcolor= #ff9900>
        <th style="color: white"> No. Tiket </th>
        <th style="color: white"> Kategori </th>
        <th style="color: white"> Status </th>
        <th style="color: white"> Tanggal dibuat </th>
        <th style="color: white"> Tanggal masuk ke IT </th>
        <th style="color: white"> Estimasi Selesai </th>
        <th style="color: white"> Tindakan </th>
        </tr>

        <?php
        foreach($result as $row){?>
<?php 
if ($row->Status =="OPEN") {
    $image = '<img alt="Brand" src="assets/img/remind.png" width="25" height="25" /> Reminder';
} elseif ($row->Status =="Masih menunggu approval Atasan" || $row->Status =="Masih menunggu approval PIC HO") {
    $image = '<img alt="Brand" src="assets/img/fast_forward.png" width="25" height="25" /> Percepat';
} elseif($row->Status =="Sudah Selesai"){
    $image = '<img alt="Brand" src="assets/img/Reopen.png" width="25" height="25" /> Reopen';
} else {
    $image ="";
}

?>
<tbody>
        <tr>
            <!-- <td bgcolor= #fff><?php echo $row->ticket_id; ?></td> -->
    <td><a id="btn_preview"  action="<?php echo base_url() ?>tracking/showDetail?ticket_id=<?php echo $row->ticket_id; ?>"><?php echo $row->ticket_id; ?></a></td>
            <td bgcolor= #fff><?php echo $row->name; ?></td>
            <td bgcolor= #fff><?php echo $row->Status; ?></td>
            <td bgcolor= #fff><?php echo $row->created_time; ?></td>
            <td bgcolor= #fff><?php echo $row->start_IT; ?></td>
            <td bgcolor= #fff><?php echo $row->estimasi_selesai; ?></td>
            <td bgcolor= #fff><?php echo $image; ?></td>
        </tr>
    </tbody>
        <?php }?>
                </table>
                <div class="halaman">Halaman : <?php echo $page;?></div>
                </center>
                </div>

                <?php
            } 
?>

型号:

function searchSpv()
{

$findTicket=$this->db->query("my query");

return $findTicket->result();
}

【问题讨论】:

  • 您当前的输出是多少?显示了多少行?
  • 我希望每页只显示 10 行,但所有行都显示在第一页。
  • 你能发布你的模型代码吗?
  • 我已经添加了我的型号代码@AdhanTimothyYounes
  • 您的查询是什么?

标签: php html codeigniter pagination


【解决方案1】:

对于$config['base_url'],您必须添加加载具有分页的视图页面的 url。

例如,如果www.example.com/index.php/controller_name/function_name 是分页的url,您在其中定义了分页并在控制器Controller_name 的函数function_name() 中加载具有分页的视图页面,那么,

$config['base_url'] = 'www.example.com/index.php/controller_name/function_name';

$config['base_url'] = site_url().'/controller_name/function_name';

那么,接下来是$config['total_rows']。这是包括所有页面在内的总行数或记录数。

假设有 1000 行数据,我们必须对其进行分页。不管有多少页,不管当前的活动页是多少,这 1000 行都不会改变。您可以在每页显示 10 条记录或 50 条记录或 100 条记录或其他您希望显示的记录,但此总记录数保持不变。

也就是说,

$config['total_rows']=1000;

在大多数情况下,这将是动态数据。因此,如果我们运行查询以从数据库中获取记录;为了获得$config['total_rows'],我们必须计算该查询检索到的记录总数。

如果分页是针对数据的静态记录,那么我们可以简单地静态(手动)添加总行数,因为我们对记录总数有一个初步的了解。

【讨论】:

    猜你喜欢
    • 2011-01-04
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 2017-03-25
    • 2017-05-03
    • 2014-10-07
    • 2013-03-28
    相关资源
    最近更新 更多