CakePHP分页超出第1页

时间:2015-08-31 15:09:20

标签: php cakephp pagination

我的网站中有一个有两个div的视图。一个包含搜索框,另一个显示搜索结果。默认情况下,结果div上没有加载任何内容。仅当通过发布请求进行搜索时,才会填充结果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>';
}

1 个答案:

答案 0 :(得分:1)

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

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

PS: 在CakePHP包含不是一件好事。你还没有提到你正在使用的版本。