0

我是 CI 的新手,我尝试了很多方法来运行它,但它仍然无法正常工作。你能告诉我我做错了什么吗?

所以,控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->model('model_add','',TRUE);
}

public function _remap($a){
    if (isset($a) && !empty($a)):
        switch ($a) {
            case 'index':
                $this->index();
                break;

            default:
                $this->one_news($a);
                break;
        }
    endif;
}

public function index()
{
    $this->load->library('pagination');
    $home=$this->uri->segment(2);

    $limit=2;
    $offset=0;

    $query=$this->model_add->count_news($limit,$offset);

    $config['base_url'] = base_url().'/news/';
    $config['total_rows'] = $this->db->count_all('tdt_news');
    $config['per_page'] = 2;
    $config['uri_segment'] = 2;

    $this->pagination->initialize($config);
    $data = array('query' => $query,'page'=>$home);

    $data['news']           = $query;

    $this->load->view('main/header');
    $this->load->view('main/news', $data);
    $this->load->view('main/footer');
}
}

和模型:

    function count_news()
{
    $query=$this->db->query("SELECT * FROM `tdt_news` ORDER BY `id` DESC LIMIT $limit, $offset;");
    return $query->result();
}

我将非常感谢您的帮助,谢谢!

4

1 回答 1

0

你有一系列错误

  • 您模型中的计数函数缺少参数
  • 您应该重新格式化 SQL 字符串
  • 您认为的替代和标准语法
  • 将模型类重命名为空字符串

这是一个固定版本,

控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('model_add');
    }

    public function _remap($a){
        if (isset($a) && !empty($a)) {
            switch ($a) {
                case 'index':
                $this->index();
                break;

                default:
                $this->one_news($a);
                break;
            }
        }
    }

    public function index()
    {
        $this->load->library('pagination');
        $home = $this->uri->segment(2);

        $limit = 2;
        $offset = 0;

        $query=$this->model_add->count_news($limit,$offset);

        $config['base_url'] = base_url().'/news/';
        $config['total_rows'] = $this->db->count_all('tdt_news');
        $config['per_page'] = 2;
        $config['uri_segment'] = 2;

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

        $data = array(  'query' => $query,
                        'page'=>$home,
                        'news' => $query);

        $this->load->view('main/header');
        $this->load->view('main/news', $data);
        $this->load->view('main/footer');
    }
}

模型:

// with default parameters just in case
function count_news($limit = 50, $offset = 0)
{
    // and there is a nicer way to write your query
    // it works with multiple dbs thanks to active record
    $this->db->order_by('id','desc');
    $query = $this->db->get('tdt_news', $limit, $offset);
    return $query->result();
}
于 2013-10-17T19:51:24.303 回答