【问题标题】:Cake PHP Pagination changes to limit records per pageCake PHP 分页更改以限制每页的记录
【发布时间】:2014-05-24 10:02:52
【问题描述】:

我的调用/index.ctp 中有以下代码。目前它在一页中显示所有记录。我想限制每页 20 条记录。在我烘焙项目时,提供了分页,但我还是初学者,不知道如何更改分页。有人可以帮忙吗?

调用/index.ctp:

<div class="callsIndex">
    <h2><?php echo __('Call Details'); ?>   </h2>
    <div class="bottomButtonnew"><?php echo $this->Html->link(__('Add Calls'), array('action' => 'add')); ?></div>


    <table cellpadding="0" cellspacing="0">
        <tr>

            <th><?php echo $this->Paginator->sort('Call Date'); ?></th>
            <th><?php echo $this->Paginator->sort('Call Time'); ?></th>
            <th><?php echo $this->Paginator->sort('Comments'); ?></th>
            <th><?php echo $this->Paginator->sort('Next Call Date'); ?></th>
            <th><?php echo $this->Paginator->sort('Customer Name'); ?></th>
            <th><?php echo $this->Paginator->sort('Company Name'); ?></th>
            <th><?php echo $this->Paginator->sort('Employee Name'); ?></th>
            <th class="actions"><?php echo __(''); ?></th>
        </tr>
        <?php foreach ($calls as $call): ?>
            <tr>

                <td><?php echo date("d-m-Y", strtotime($call['Call']['call_date'])); ?>&nbsp;</td>
                <td><?php echo h($call['Call']['call_time']); ?>&nbsp;</td>
                <td><?php echo h($call['Call']['comments']); ?>&nbsp;</td>
                <td><?php echo date("d-m-Y", strtotime($call['Call']['next_call_date'])); ?>&nbsp;</td>
                <td>
                    <?php echo $this->Html->link($call['Customers']['customer_name'], array('controller' => 'customers', 'action' => 'view', $call['Customers']['id'])); ?>

                </td>
                <td>
                    <?php echo $this->Html->link($call['Companies']['company_name'], array('controller' => 'companies', 'action' => 'view', $call['Companies']['id'])); ?>
                </td>
                <td>
                    <?php echo $this->Html->link($call['Employees']['employee_name'], array('controller' => 'employees', 'action' => 'view', $call['Employees']['id'])); ?>
                </td>
                <td class="actions">


                    <?php echo $this->Html->link(__('View'), array('action' => 'view', $call['Call']['id'])); ?>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>
    <p>
        <?php
        echo $this->Paginator->counter(array(
            'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total')
        ));
        ?>  </p>
    <div class="paging">
        <?php
        echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
        echo $this->Paginator->numbers(array('separator' => ''));
        echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
        ?>
    </div>
    <br>

</div>

调用控制器:

<?php
App::uses('AppController', 'Controller');

class CallsController extends AppController {

public $components = array('Paginator');
public $paginate = array(
        'limit' => 10
    );

public function index() {
        $userid=$this->Session->read('User.userid');
        if(isset($userid)&&$userid!=null)
        {
            $this->Call->recursive = 0;
            $this->set('calls', $this->Paginator->paginate());
            $result=$this->Call->getcalls($userid);
            $this->set('result', $result);
        }
        else{
            $this->set('loggedout',"loggedout");
            $this->render("../Pages/home");
            $this->layout = '../Pages/home';
        }


    }
//some code
}

【问题讨论】:

  • 你的查找逻辑在哪里?

标签: cakephp paginator


【解决方案1】:

您可以使用分页组件轻松完成。您应该在您的 AppController 中执行此操作。在您的 AppController 中您可以输入以下代码

Public $components =array('paginator');

    public function beforeFilter()
    {
       parent::beforeFilter();
       $this->Paginator->settings=array(
              'limit'=>10
       );
    } 

比在你的索引方法中只需更改这一行

$this->set('calls', $this->Paginator->paginate());

我认为现在可以正常工作了。此代码适用于所有控制器。但如果您只想更改 CallsController。执行此代码。

<?php
App::uses('AppController', 'Controller');

class CallsController extends AppController {
public $components = array('Paginator');

public function index() {

      $this->Paginator->settings = array(
        'limit' => 10
    );
        $userid=$this->Session->read('User.userid');
        if(isset($userid)&&$userid!=null)
        {
            $this->Call->recursive = 0;
            $this->set('calls', $this->Paginator->paginate());
            $result=$this->Call->getcalls($userid);
            $this->set('result', $result);
        }
        else{
            $this->set('loggedout',"loggedout");
            $this->render("../Pages/home");
            $this->layout = '../Pages/home';
        }


    }
//some code
}

注意:分页有两种方法,一种是使用组件,另一种是在控制器中。

【讨论】:

    【解决方案2】:

    你的例子中正确的调用应该是这样的:

    $this->set('调用', $this->paginate());

    所以你的代码是:

    <?php
    App::uses('AppController', 'Controller');
    
    class CallsController extends AppController {
    
    public $components = array('Paginator');
    public $paginate = array(
            'limit' => 10 //Here can you change the default limit
        );
    
    public function index() {
            $userid=$this->Session->read('User.userid');
            if(isset($userid)&&$userid!=null)
            {
                $this->Call->recursive = 0;
                $this->set('calls', $this->paginate());
                $result=$this->Call->getcalls($userid);
                $this->set('result', $result);
            }
            else{
                $this->set('loggedout',"loggedout");
                $this->render("../Pages/home");
                $this->layout = '../Pages/home';
            }
    
    
        }
    //some code
    }
    

    【讨论】:

    • 我将我的代码更改为上述代码,但它仍然显示所有呼叫记录,并且不限于 10 条记录。 @lamasgergo
    • 分页记录在 $calls 变量中。忠告1:在你的视图文件中写出sql调试并检查mysql查询,有没有限制?!我认为您在某处覆盖了 $calls 变量。建议2:简单的 var_dump($this->paginate()); (或调试)在你的控制器中检查这个函数的结果是什么。
    • 这是错误的!您混合了组件分页和控制器分页!
    • 没有错,但会在 cake 3.0 中被弃用。在 cake 2.x 中,您可以在不加载分页器组件的情况下使用分页,但建议直接使用分页器组件。参考:lib/Cake.php @line 1068
    【解决方案3】:
    <?php
    App::uses('AppController', 'Controller');
    
    class CallsController extends AppController {
    
    public $components = array('Paginator');
    public $paginate = array(
        'limit' => 20
    );
    
    }
    

    【讨论】:

    • 我在我的调用控制器中添加了该代码,但没有任何变化,仍然显示商场记录。我已经用我的 callsController 索引函数更新了这个问题。有什么我需要改变的吗? @索尼
    • @sony 这不对,因为你这里用过组件,为什么要用$paginate。不要把组件分页和控制器分页混用!!
    猜你喜欢
    • 2021-08-07
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多