繁体   English   中英

CakePHP分页在第一页之后无法正常工作

[英]CakePHP pagination not working beyond 1st page

我的网站上有两个divs。 一个包含一个搜索框,另一个包含搜索结果。 默认情况下,结果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>';
}

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

即使删除了要发布的第一个if语句,您仍将看不到任何数据,因为next按钮不会发布任何数据来使第二条语句正确。

PS:在CakePHP上包含include不是一件可取的事情。 另外,您还没有提到您使用的版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM