2013-02-14 100 views
0

大家好我是CodeIgniter和PHP的新手。分页不显示在网页上

我想在我的一个页面上创建分页,但是由于某种原因,我的代码不会在页面上显示分页。

view.php

</head> 
    <body> 


      <h1>Answer</h1> 
      <?php if (isset($records)) : foreach($records as $row) : ?> 

     <div = 'container'> 


      <?php 
      if (isset($pagination)) 
      { 
      echo $pagination; 
      } 
      ?> 

      <ul> 
      <h1><?php echo $row->question; ?></h1> 
      <li><?php echo $row->answer1; ?></li> 
      <li><?php echo $row->answer2; ?></li> 
      <li><?php echo $row->answer3; ?></li> 
      <li><?php echo $row->answer4; ?></li> 
      <li><?php echo $row->answer5; ?></li> 
      <li><?php echo $row->answer6; ?></li> 
      <ul> 

     </div> 

      <?php endforeach; ?> 
      <?php else : ?> 
      <h2>no records were returned</h2> 
      <?php endif; ?> 
    </body> 
</html> 

control.php

<?php 

class Survey extends CI_Controller{ 

function index() 
{ 
      $data = array(
     'question' => $this->input->post('question'), 
      'answer1' => $this->input->post('answer1'), 
      'answer2' => $this->input->post('answer2'), 
      'answer3' => $this->input->post('answer3'), 
      'answer4' => $this->input->post('answer4'), 
      'answer5' => $this->input->post('answer5'), 
      'answer6' => $this->input->post('answer6'), 
      ); 


      if($query = $this->membership_model->get_records()) 
      { 
       $data['records'] = $query; 
      } 

      $this->load->view('survey_view', $data); 

      } 

//pagination 
     function page() 
      { 

      $this->load->library('pagination'); 


      $config['base_url'] = 'http://localhost/admin/index.php/survey/index'; 
      $config['total_rows'] = $this->db->get('save_survey')->num_rows(); 
      $config['per_page'] = 1; 
      $config['num_links'] =10; 

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

      $data['records'] = $this->db-get('save_survey', $config['per_page'], $this->uri->segment(3)); 
      $data['pagination'] = $this->pagination->create_links(); 
      $this->load->view('survey_view', $data); 

      } 
    } 

?> 

回答

1

功能page()不会被调用。

当您导航到控制器时,将会触发默认的index()功能。并且在那个函数中没有调用page();的方法。

我从来没有使用CI的分页东西之前,但包括在index()方法您的分页代码(page()),你可以做这样的(我删除从页面法的观点):

class Survay extends CI_Controller{ 
    function index() { 
     $data = array(
      'question' => $this->input->post('question'), 
      'answer1' => $this->input->post('answer1'), 
      'answer2' => $this->input->post('answer2'), 
      'answer3' => $this->input->post('answer3'), 
      'answer4' => $this->input->post('answer4'), 
      'answer5' => $this->input->post('answer5'), 
      'answer6' => $this->input->post('answer6'), 
     ); 


     if($query = $this->membership_model->get_records()) { 
      $data['records'] = $query; 
     } 

     //call your page method and set the pagination variables to this object 
     $this->page(); 

     //show the view 
     $this->load->view('survay_view', $data); 
    } 

    //pagination ONLY sets the pagination variables 
    function page() { 
     $this->load->library('pagination'); 

     $config['base_url'] = 'http://localhost/admin/index.php/survay/index'; 
     $config['total_rows'] = $this->db->get('save_survay')->num_rows(); 
     $config['per_page'] = 1; 
     $config['num_links'] = 10; 

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

     $data['records'] = $this->db-get('save_survay', $config['per_page'], $this->uri->segment(3)); 
     $data['pagination'] = $this->pagination->create_links(); 
    } 
} 
+0

tnx为你的帮助我做了你说的,但只要我打电话给页面方法一个错误强加“调用未定义的函数get()”任何想法,为什么发生这种情况? – 2013-02-14 10:01:33

+0

get方法来自db类。你是否自动加载数据库库(config - > autoload.php)?如果没有,那么你需要在使用'$ this-> load-> database();'之前加载它。 – 2013-02-14 10:24:19

+0

是我没有TNX乌拉圭回合帮助ü为我节省了大量的时间:)但是遇到现在我得到一个不同势错误 一个PHP错误 严重性:注意 消息:试图获得的非对象 属性文件名:views/survay_view.php 行数:31 你有什么想法是什么导致这个错误? :) – 2013-02-14 10:44:30