CodeIgniter 自带分类详解及如何一直显示首页和尾页

控制器

如想一直显示首页和尾页--》打开分页类318行改成if ($this->last_link !== FALSE)
首页229行改成 if  ($this->first_link !== FALSE )
就是说去掉后边的and判断


  public function index($p=1){
     // $data['news'] = $this->news_model->get_news();
     //echo $p;
      $data['title'] = '标题';
 
      //分页使用
      $res = $this->db->get('news');//进行一次查询
      $config['base_url'] = '/ci/index.php/news/index';//设置分页的url路径
      $config['uri_segment'] = 3; //设置url上第几段用于传递分页器的偏移量
      $config['total_rows'] = $res->num_rows();//得到数据库中的记录的总条数
      $config['per_page'] = '2';//每页记录数
      $config['first_link'] = '首页';
      $config['last_link'] = '尾页';
      $config['full_tag_open'] = '<p>';
      $config['full_tag_close'] = '</p>'; 
      $config['next_link'] = '下一页';
      $config['prev_link'] = '上一页';
 
 
      $config['use_page_numbers'] = TRUE;//必须加上要不页码会乱...
       
      //$config['num_links'] = 4;//配置分页导航当前页两边显示的条数
      $start=($p-1)*$config['per_page'];
 
      $this->pagination->initialize($config);//分页的初始化
      //$data['news']= $this->news_model->get_news($config['per_page'],$this->uri->segment(3));//得到数据库记录
      $data['news']= $this->news_model->get_news($config['per_page'],$start);//得到数据库记录
 
 
      //分页结束
      $this->load->view('templates/header', $data);
      $this->load->view('news/index', $data);
      $this->load->view('templates/footer');
    }
 
  public function view($id){
      $data['news_item'] = $this->news_model->view_news($id);
 
      if (empty($data['news_item']))
      {
        show_404();
      }
 
      $data['title'] = $data['news_item']['title'];
 
      $this->load->view('templates/header', $data);
      $this->load->view('news/view', $data);
      $this->load->view('templates/footer');
    }
 
    public function create()
    {
      $this->load->helper('form');
      $this->load->library('form_validation');
      
      $data['title'] = 'Create a news item';
      
      $this->form_validation->set_rules('title', 'Title', 'required');
      $this->form_validation->set_rules('text', 'text', 'required');
      
      if ($this->form_validation->run() === FALSE)
      {
        $this->load->view('templates/header', $data);  
        $this->load->view('news/create');
        $this->load->view('templates/footer');
        
      }
      else
      {
        $this->news_model->set_news();
        $this->load->view('news/success');
      }
    }

模型

<?php
class News_model extends CI_Model {
 
  public function __construct()
  {
    $this->load->database();
  }
 
  
  public function get_news($num,$offset){
  	//从数据库中取数据,$num表示取得的数目,$offset表示从什么地方开始取
  	$query = $this->db->get('news',$num,$offset);
    return $query->result_array();
 
 
 
	/*  if ($slug === FALSE)
	  {
	    $query = $this->db->get('news');
	    return $query->result_array();
	  }
	  
	  $query = $this->db->get_where('news', array('slug' => $slug));
	  return $query->row_array();
	  */
	}
 
	public function view_news($id)
	{
		$query = $this->db->get_where('news', array('id' => $id));
		return $query->row_array();
	}
 
	public function set_news()
		{
		  $this->load->helper('url');
		  
		  $slug = url_title($this->input->post('title'), 'dash', TRUE);
		  
		  $data = array(
		    'title' => $this->input->post('title'),
		    'slug' => $slug,
		    'text' => $this->input->post('text')
		  );
		  
		  return $this->db->insert('news', $data);
		}
}

视图

<?php foreach ($news as $news_item): ?>
 
    <h2><?php echo $news_item['title'] ?></h2>
    <div id="main">
        <?php echo $news_item['text'] ?>
    </div>
    <p><a href="/ci/index.php/news/view/<?php echo $news_item['id'] ?>">
    	View article 详细信息</a></p>
 
<?php endforeach ?>
 
 
<hr>
<?php echo $this->pagination->create_links();?>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值