|
<?php
namespace daswork;
class Model
{
protected $db;
function __construct(){
//$this->db = mysqlidb::getInstance();
$this->db = new MysqlidbModel();
}
function add($data){
//列名数组
$cols=array();
//列值数组
$vals=array();
foreach($data as $key=>$val){
$cols[]="`".$key."`";
$vals[]="'".$val."'";
}
//$cols=array(0=>'`titlekey`',1=>'`contentkey`'); //
//$vals=array(0=>"'titleval'",1=>"'contentval'"); //
//用","将数组每个元素重新拼接起来
$str_cols=implode(',',$cols); //$str_cols= `titlekey`,`contentkey`
$str_vals=implode(',',$vals); //$str_vals= 'titleval','contentval'
//执行插入数据
$sql="insert into ".$this->tablename."($str_cols) values($str_vals);";
return $this->db->insert($sql );
}
function update($data,$where){
$wherestr = '';
if(!empty($where)){
$wherestr .=' where '.$where;
}
$par_array=array();
foreach($data as $key=>$val){
$par_array[]="`".$key."`="."'".$val."'";
}
//用","将修改列和列值的数组里每个元素重新拼接起来
$param=implode(',',$par_array);
$sql = "update ".$this->tablename." set ".$param." ".$wherestr." ";
return $this->db->query($sql);
}
function get_lists($where, $limit=''){
$wherestr = '';
if(!empty($where)){
$wherestr .=' where '.$where;
}
$sql = 'select * from '.$this->tablename.$wherestr . $limit;
// echo $sql;
return $this->db->select($sql);
}
function get_sum($where){
$wherestr = '';
if(!empty($where)){
$wherestr .=' where '.$where;
}
$sql = "select count(*) as sum from ".$this->tablename.$wherestr;
$data = $this->db->get_row($sql);
return $data['sum'];
}
function get_one($where){
$wherestr = '';
if(!empty($where)){
$wherestr .=' where '.$where;
}
$sql = "select * from ".$this->tablename.$wherestr;
//var_dump($sql);exit;
$data = $this->db->get_row($sql);
return $data;
}
function del($where){
$wherestr = '';
if(!empty($where)){
$wherestr .=' where '.$where;
}
$sql = "delete from ".$this->tablename.$wherestr;
//echo $sql;exit;
$flag = $this->db->query($sql);
return $flag;
}
}
|