amin365
amin365

Reputation: 196

CakePHP pagination not working beyond 1st page

I have a view in my site that has two divs. one houses a search box and the other is where the result of the search is shown. by default, nothing is loaded on the result div. only when there is a search via post request, the result div is populated. However, this is causing problem with the pagination functionality as the second time the page loads, it doesnt have the data to go around with listing the rest of the results. How can i modify my controller/view to meet this need?

My action in the controller:

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'));
            }
        }
    }

My View:

<?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>';
}

Upvotes: 0

Views: 1211

Answers (1)

gmponos
gmponos

Reputation: 2277

You are searching for results only on post request. But when you press the next button you are making a get request. So the first if statement doen't return true so you are not passing any data to your view.

Even if you remove the first if statement for post you'll still won't see any data because the next button doesn't post any data to make true the second statement.

PS: On CakePHP include is not a preferable thing to do. Also you haven't mentioned the version you are using.

Upvotes: 1

Related Questions