0

大家好,我是 CodeIgniter 和 PHP 的新手。

我正在尝试在我的一个页面上创建分页,但由于某种原因,我的代码没有在页面上显示分页。

视图.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>

控制.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);

          }
    }

?>
4

1 回答 1

1

该函数page()永远不会被调用。

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

我以前从未使用过 CI 的分页内容,但是要将您的分页代码 ( page()) 包含在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();
    }
}
于 2013-02-14T09:45:39.720 回答