Codeigniter分页与db查询无法正常工作

时间:2014-01-13 13:23:23

标签: php codeigniter templates pagination codeigniter-2

我没有成功地将分页与我的select with codeigniter的结果进行整合。

我开始使用codeigniter而且我无法正确地进行分页。链接是正确创建的,它们没有限制,链接不会去任何地方。以下是所有代码

模型

public function listProvas($i = 0, $nRows = 0){
    //return $data['provas'] = $this->db->get('provas')->result_array();
    return $this->db->query('SELECT `idProva` , `provas`.`titulo` AS `nome_prova` , `provas`.`ativo` , `atividades`.`idAtividade` , `modulos_x_tarefas`.`fkModulo` , `modulos`.`titulo` AS `modulo` , `disciplinas`.`disciplina`
                             FROM `provas` , `atividades` , `modulos_x_tarefas` , `modulos` , `disciplinas`
                             WHERE `provas`.`fkAtividade` = `atividades`.`idAtividade`
                             AND `atividades`.`idAtividade` = `modulos_x_tarefas`.`fkTarefa`
    ')->result_array();
}

控制器

public function index($i = 0){
    $this->load->model('provasmodel');

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

    $nRows = 50;
    $config['base_url'] = 'http://URL/painel/listaprovas/';
    $config['total_rows'] = $this->db->get('provas')->num_rows();
    $config['per_page'] = $nRows;
    $config['num_links'] = 20;
    $config['full_tag_open'] = '<ul class="pagination">';
    $config['full_tag_close'] = '</ul>';
    $config['next_link'] = '&gt;';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['prev_link'] = '&lt;';
    $config['prev_tag_open'] = '<li>';
    $config['prev_tag_close'] = '</li>';


    $dados['provas'] = $this->provasmodel->listProvas($i, $nRows);
    $dados['total'] = count($this->provasmodel->listProvas($i, $nRows));
    $this->load->library('pagination');

    $datapag['records'] = $this->db->get('provas', $config['per_page'], $this->uri->segment(3));

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

    $data['provas'] = $this->provasmodel->listProvas();
    $this->template->load('templates/template_painel', 'painel/lista_provas', $data, $datapag);
}

视图

<?php foreach ($provas as $data): ?>
        <tr>
          <td><?php echo $data['idProva']; ?></td>
          <td><?php echo $data['nome_prova']; ?></td>
          <td><?php echo $data['modulo']; ?></td>
          <td><?php echo $data['disciplina']; ?></td>
          <td>numero</td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>

    <?php echo $this->pagination->create_links(); ?>

2 个答案:

答案 0 :(得分:0)

您在分页配置中缺少uri_segement选项。

$config['uri_segment'] = 3;

根据您的项目网址更改号码。

uri_segment适用于page number而不是per page count

分页功能自动确定URI的哪个部分包含页码。如果您需要不同的东西,可以指定它。

答案 1 :(得分:0)

代码中有2个错误,其中一个没有对我的SQL调用设置限制。 二,基本网址没有调用索引方法。

感谢您的提示。

模型

 public function listProvas($i = 0, $nRows = 0){
//return $data['provas'] = $this->db->get('provas')->result_array();
return $this->db->query('SELECT `idProva` , `provas`.`titulo` AS `nome_prova` , `provas`.`ativo` , `disciplinas`.`disciplina`
                         FROM `provas` , `atividades` , `modulos_x_tarefas` , `modulos` , `disciplinas`
                         WHERE `provas`.`fkAtividade` = `atividades`.`idAtividade`
                         AND `atividades`.`idAtividade` = `modulos_x_tarefas`.`fkTarefa´ LIMIT '.$i.','.$nRows.'

')->result_array();

控制器

public function index($i = 0, $nRows = 100){
    $this->load->model('provasmodel');
    $this->load->library('pagination');

    $config['base_url'] = 'http:/url/painel/listaprovas/index';
    $config['total_rows'] = $this->db->get('provas')->num_rows();
    $config['per_page'] = $nRows;
    $config['num_links'] = 20;
    $config['full_tag_open'] = '<ul class="pagination">';
    $config['full_tag_close'] = '</ul>';
    $config['next_link'] = '&gt;';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['prev_link'] = '&lt;';
    $config['prev_tag_open'] = '<li>';
    $config['prev_tag_close'] = '</li>';
    $data['provas'] = $this->provasmodel->listProvas($i, $nRows);
    $data['total'] = count($this->provasmodel->listProvas($i, $nRows));
    $datapag['records'] = $this->db->get('provas', $config['per_page'], $this->uri->segment(3));

    $this->pagination->initialize($config);
    $data['pagination'] = $this->pagination->create_links();

    $data['provas'] = $this->provasmodel->listProvas($i, $nRows);
    $this->template->load('templates/template_painel', 'painel/lista_provas', $data);
}

}

视图

<?php foreach ($provas as $data): ?>
        <tr>
          <td><?php echo $data['idProva']; ?></td>
          <td><?php echo $data['nome_prova']; ?></td>
          <td><?php echo $data['modulo']; ?></td>
          <td><?php echo $data['disciplina']; ?></td>
          <td>numero</td>
          <?php
            if ($data['ativo'] == '1') {
              echo '<td class="ativo">Ativo</td>';
            }else{
              echo '<td class="inativo">Inativo</td>';
            }
          ?>
          <td><a href="/painel/prova/<?php echo $data['idProva']; ?>" class="glyphicon glyphicon-pencil"></a></td>
        </tr>
        <?php endforeach; ?>

      </tbody>
    </table>


        </div><!--table-responsive--
  <?php echo $pagination; ?>