【问题标题】:CakePHP pagination not working beyond 1st pageCakePHP 分页不能超出第一页
【发布时间】:2015-08-31 15:09:19
【问题描述】:

我的站点中有一个视图,它有两个 div。一个是搜索框,另一个是显示搜索结果的地方。默认情况下,结果 div 上不加载任何内容。仅当通过 post 请求进行搜索时,才会填充结果 div。但是,这会导致分页功能出现问题,因为页面第二次加载时,它没有数据可以列出其余的结果。我如何修改我的控制器/视图以满足这种需求?

我在控制器中的操作:

public function search(){
        if($this->request->is('post')){
            if($this->request->data['User']['search']!=null){
                $search_name=$this->request->data['User']['search'];

                $this->Paginator->settings = array(
                    'conditions'=>array('User.name LIKE'=>'%'.$search_name.'%'),
                    'order' => array('User.datetime' => 'desc'),
                    'limit' => 10
                );

                $feed = $this->Paginator->paginate('User');
                $this->set('search_result',$feed);
            }

            else{
                $this->Session->setFlash(__("Cant make an empty search"));
                return $this->redirect(array('action'=>'search'));
            }
        }
    }

我的观点:

<?php
include 'header.ctp';

echo '<div id="feed">';
echo $this->Form->create(null, array('url' => array('controller' => 'users', 'action' => 'search')));
echo $this->Form->input('search');
echo $this->Form->end('Search');
echo 'search by name';
echo '</div>';

if(isset($search_result)){
    echo '<div id="tweet_records">';

    if(!empty($search_result)){

        foreach ($search_result as $user) : 

            $formatted_text;
            $latest_tweet_time;
            $formatted_time;

            if(!empty($user['Tweet'])){
                $formatted_text=$this->Text->autoLinkUrls($user['Tweet']['0']['tweet']);
                $latest_tweet_time=$user['Tweet']['0']['datetime'];
                $formatted_time;
                if(strlen($latest_tweet_time)!=0){
                    $formatted_time='Tweeted at '.$latest_tweet_time;
                }
                else{
                    $formatted_time='No tweets yet';    
                }
            }
            else if(empty($user['Tweet'])){
                $formatted_text='No tweets yet';
                $formatted_time='';     
            }
            echo '<table id="t01">';
            echo'<tr>';

            echo '<td>'.
                 $user['User']['name'].'@'.$this->HTML->link($user['User']['username'], array('controller'=>'tweets','action'=>'profile',$user['User']['id'])).'<br>'.$formatted_text.'&nbsp;'.'<br>'.'<font color="blue">'.$formatted_time.'</font>';

                 echo '<div id="delete">';
                 $selector='true';

                 foreach ($user['Follower'] as $follower) :
                    if($follower['follower_user_id']==AuthComponent::user('id')){
                        $selector='false';
                        echo $this->Form->postlink('Unfollow',array('controller'=>'followers','action'=>'unfollow',$user['User']['id']),array('confirm'=>'Do you really want to unfollow this person?'));
                    }
                 endforeach;

                 if($selector=='true' & $user['User']['id']!=AuthComponent::user('id')){
                    echo $this->Form->postlink('Follow',array('controller'=>'followers','action'=>'follow',$user['User']['id']));
                 }
                 echo '</div>';

                 echo '</td>';


            echo '</tr>';
        endforeach;

        echo'</table>';


        echo 'Pages: ';

        echo $this->Paginator->prev(
          ' < ',
          array(),
          null,
          array('class' => 'prev disabled')
        );

        echo $this->Paginator->numbers();

        echo $this->Paginator->next(
            ' > ' , 
            array(), 
            null, 
            array('class' => 'next disabled')
        );

    }
    else{
        echo '<font color="red"> No results found </font>';
    }
    echo '</div>';
}

【问题讨论】:

标签: php cakephp pagination


【解决方案1】:

您仅在发布请求时搜索结果。但是当您按下下一个按钮时,您正在发出获取请求。因此,第一个 if 语句不会返回 true,因此您不会将任何数据传递给您的视图。

即使您删除了第一个 if 语句,您仍然不会看到任何数据,因为下一个按钮不会发布任何数据以使第二个语句为真。

PS: 在 CakePHP 上包含不是一个可取的事情。你也没有提到你正在使用的版本。

【讨论】:

  • 感谢您的回复,我实际上对 CakePHP 很陌生,所以我对所有的约定和编码实践并不太熟悉。我正在使用 2.5。你能给我一些想法,告诉我如何在尽可能少的改动的情况下修复它吗?
  • 这与问题主题无关。如果您需要任何帮助,请开始一个新的,但我认为它太笼统了。
猜你喜欢
  • 2011-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
  • 2011-01-14
  • 1970-01-01
相关资源
最近更新 更多