使用CodeIgniter进行搜索的分页错误

时间:2015-02-04 15:26:16

标签: ajax codeigniter search pagination

我正在尝试使用CodeIgniter实现分页搜索。到目前为止,我已经设法完成搜索部分以及Oracle数据库上的查询。我的问题是搜索功能与分页相结合。使用下面的代码,我设法显示正确分页的初始数据(每页10行)。后续页面也会显示正确的数据。

但是,如果我进行搜索,则只会正确显示新数据的第一页。例如,当我点击第二页时,显示的是初始数据第二页的行。

另外,我需要帮助才能使代码更“干净”。我的意思是我不需要对分页的设置进行两次编码(首先在索引函数上,第二次在getNewData函数上)。希望这是可能的。

class Controller extends CI_Controller {
function __construct() {
  parent::__construct();
  $this->load->model('Model','',TRUE);
  $this->load->library('pagination');
}

function index() {
  if ($this->session->userdata('logged_in')) {
    $session_data = $this->session->userdata('logged_in');
    $data['ID'] = $session_data['ID'];

    $config = array();
    $config["base_url"] = base_url()."index.php/Controller/index";
    $total_row = count($this->Model->getData($data['ID']));           
    $config["total_rows"] = $total_row;
    $config["per_page"] = 10;
    $config['use_page_numbers'] = TRUE;
    $config['num_links'] = 2;
    $config['cur_tag_open'] = '&nbsp;<a class="current">';
    $config['cur_tag_close'] = '</a>';
    $config['next_link'] = 'Next';
    $config['prev_link'] = 'Previous';

    $this->pagination->initialize($config);

    if ($this->uri->segment(3)) {
      $page = ($this->uri->segment(3)) ;
    } else {
      $page = 1;
    }

    $data["results"] = $this->Model->getPageData($data['ID'], $config["per_page"], $page);
    $str_links = $this->pagination->create_links();
    $data["links"] = explode('&nbsp;',$str_links );

    $this->load->view('View', $data);
  } else {
    redirect('loginController', 'refresh');
  }
}

function getNewData() {
  $newSearchID = $this->input->get('search');

  $data['NEW_DATA'] = $this->Model->getData($newSearchID);

  $config = array();
  $config["base_url"] = base_url()."index.php/Controller/Index";
  $total_row = count($data['NEW_DATA']);           
  $config["total_rows"] = $total_row;
  $config["per_page"] = 10;
  $config['use_page_numbers'] = TRUE;
  $config['num_links'] = 2;
  $config['cur_tag_open'] = '&nbsp;<a class="current">';
  $config['cur_tag_close'] = '</a>';
  $config['next_link'] = 'Next';
  $config['prev_link'] = 'Previous';

  $this->pagination->initialize($config);

  if ($this->uri->segment(3)) {
    $page = ($this->uri->segment(3)) ;
  } else {
    $page = 1;
  }

  $data["results"] = $this->Model->getPageData($newSearchID, $config["per_page"], $page); 
  $str_links = $this->pagination->create_links();
  $data["links"] = explode('&nbsp;',$str_links );

  echo json_encode($this->getDataInHTMLFormat($data['results'], $data["links"]));
}

protected function getOfferingsInHTMLFormat($NEW_DATA, $LINKS) {
  $html1 = '<thead><tr>';
  $html1 .= '<th style="text-align: center">EMPLOYEE ID</th>';
  $html1 .= '<th style="text-align: center">EMPLOYEE NAME</th>';
  $html1 .= '</tr></thead>';
  $html1 .= '<tbody>';

  foreach ($NEW_DATA as $key) {
    $html1 .= '<tr>';
    $html1 .= '<td style="text-align: center">'.$key->ID.'</td>';
    $html1 .= '<td>'.$key->NAME.'</td>';
    $html1 .= '</tr>';
  }

  $html1 .= '</tbody>';

  $html2 = '<ul>';

  foreach ($LINKS as $LINK) {
    $html2 .= '<li>'.$LINK.'</li>';
  }

  $html2 .= '</ul>';

  return array($html1, $html2);
}
}

View.php的一部分

<script>
    $('form').submit(function(event){
        event.preventDefault();
        $.get('offeringsController/getNewData', {id: $('[name=id').val()}, function(data){
        var html = $.parseJSON(data);
            $('#displayData table').html(html[0]);
            $('#pagination').html(html[1]);
        });
    })
</script>

1 个答案:

答案 0 :(得分:0)

尝试将$ config [&#39; uri_segment&#39;] = 4而不是3.也许段4代表数字页面,我的意思是段3只是一种方法,而不是数字。您可以手动设置:

$config['uri_segment'] = $this->uri->segment(4);