156 lines
2.9 KiB
PHP
156 lines
2.9 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
class Db_model extends CI_Model {
|
|
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->load->database();
|
|
}
|
|
|
|
/*
|
|
读取所有信息
|
|
$table:表名
|
|
*/
|
|
public function getData($table,$recommend,$order,$offset,$len)
|
|
{
|
|
$query = $this->db->query("SELECT * FROM ".$table." ORDER BY ".$recommend." ".$order." LIMIT ".$offset.",".$len);
|
|
$data = $query->result();
|
|
return $data;
|
|
}
|
|
|
|
/*
|
|
读取所有信息
|
|
$table:表名
|
|
*/
|
|
public function getAllData($table)
|
|
{
|
|
$query = $this->db->query("SELECT * FROM ".$table);
|
|
$data = $query->result();
|
|
return $data;
|
|
}
|
|
|
|
/*
|
|
按条件读取所有信息
|
|
$table:表名
|
|
$where:条件数组
|
|
*/
|
|
public function getSpecificData($table,$where)
|
|
{
|
|
$this->db->where($where);
|
|
$query = $this->db->get($table);
|
|
$data = $query->result();
|
|
return $data;
|
|
}
|
|
|
|
/*
|
|
插入信息
|
|
$table:表名
|
|
$data:数组
|
|
$data = array(
|
|
array(
|
|
'title' => 'My title' ,
|
|
'name' => 'My Name' ,
|
|
'date' => 'My date'
|
|
),
|
|
array(
|
|
'title' => 'My title1' ,
|
|
'name' => 'My Name1' ,
|
|
'date' => 'My date1'
|
|
)
|
|
);
|
|
*/
|
|
public function insertData($table,$data)
|
|
{
|
|
$this->db->insert($table, $data);
|
|
}
|
|
|
|
/*
|
|
修改
|
|
$table:表名
|
|
$data:数据数组
|
|
$where:条件数组
|
|
|
|
$data = array(
|
|
'title' => $title,
|
|
'name' => $name,
|
|
'date' => $date
|
|
);
|
|
*/
|
|
public function updateData($table,$data,$where)
|
|
{
|
|
$this->db->where($where);
|
|
$this->db->update($table,$data);
|
|
}
|
|
|
|
/*
|
|
获得表总行数
|
|
$table:表名
|
|
*/
|
|
public function getTotalRows($table)
|
|
{
|
|
$total_rows = $this->db->count_all($table);
|
|
return $total_rows;
|
|
}
|
|
|
|
/*
|
|
获得结果集
|
|
$table:表名
|
|
*/
|
|
public function getPage($table,$where,$like,$current)
|
|
{
|
|
$num = $this->Config_model->getPageNum();
|
|
|
|
$data = array();
|
|
$msg = $this->getSearchData($table,$where,$like,null,null,null,null);
|
|
$totalRows = count($msg);
|
|
$totalPage = ceil($totalRows / $num);
|
|
$data[0] = $totalRows;
|
|
$data[1] = $totalPage;
|
|
$data[2] = $current;
|
|
return $data;
|
|
}
|
|
|
|
/*
|
|
搜索
|
|
$table:表名
|
|
$where:搜索字段数组
|
|
$where = array('字段' => 数值);
|
|
$like:模糊搜索字段数组
|
|
//$like = array('title' => $match, 'page1' => $match, 'page2' => $match);
|
|
$recommend:排序字段
|
|
$order:排序
|
|
$offset:偏移量
|
|
$len:数据数量
|
|
*/
|
|
public function getSearchData($table,$where,$like,$recommend,$order,$offset,$len)
|
|
{
|
|
//$this->db->get($table);
|
|
if ($where != null)
|
|
$this->db->where($where);
|
|
if ($like != null)
|
|
$this->db->like($like);
|
|
if ($recommend != null)
|
|
$this->db->order_by($recommend,$order);
|
|
if ($len != null)
|
|
$this->db->limit($len,$offset);
|
|
$query = $this->db->get($table);
|
|
$data = $query->result();
|
|
return $data;
|
|
}
|
|
|
|
/*
|
|
删除
|
|
$table:表名
|
|
$where :条件数组
|
|
*/
|
|
public function delData($table,$where)
|
|
{
|
|
$this->db->where($where);
|
|
$this->db->delete($table);
|
|
}
|
|
|
|
}
|