前面列出了MVC中的c和v 一直没有拿出来M,今天贴出来了。
class News_model extends CI_Model{
public function __construct(){
parent::__construct();
$this->load->database();//这调用数据库
}
public function get_news($pn){
$pagesize = 3;
if(!is_numeric($pn)||$pn<1)$pn=1;
$allnum = $this->db->count_all('news');//所有news数据量
if($allnum>0){
$pagemax =ceil($allnum/$pagesize);
$pn = min($pagemax,$pn);
$begin = ($pn-1)*$pagesize;
//条件查询$this->db->get_where('table', array('id' => $id), $limit, $offset);
$query = $this->db->order_by('id','desc')->get('news',$pagesize,$begin);
foreach ($query->result() as $row){
$temp[]=array('id'=>$row->id,'title'=>$row->title);
}
$query->free_result(); //释放资源
$data['list']=$temp;
$data['pagedown'] = $pn+1<$pagemax?$pn+1:$pagemax;
$data['pageup'] = $pn-1>1?$pn-1:1;
$data['pagemax'] = $pagemax;
}else{
$data['list']=$array();
$data['pageup'] = 1;
$data['pagedown'] = 1;
$data['pagemax'] = 1;
}
return $data;
}
public function addaction(){
$this->load->helper('url');
$data['title']=$this->input->post('title');
$data['content']=$this->input->post('content');
return $this->db->insert('news',$data);
}
public function get_news_id($id){
if($id==""||!is_numeric($id)) return false;
$this->db->where('id',$id);
$query = $this->db->get('news');
if($query->num_rows()>0){
return $query->row_array();
}else{
return false;
}
}
public function changeaction($id){
if($id==""||!is_numeric($id)) return false;
$data['title'] = $this->input->post('title');
$data['content'] = $this->input->post('content');
$this->db->where('id',$id);
$this->db->update('news',$data);
return true;
}
}
更多信息请查看IT技术专栏