【实例简介】有数据操作类以及分页脚本等
【实例截图】
【核心代码】
require( "./mysql.class.php" );
class GoodsModel {
private $goods_tb_name = 'goods';
private $cat_tb_name = 'category';
private $gallery_tb_name = 'goods_gallery';
public $db;
public function __construct ( &$db ) {
$this->db = $db;
}
//添加商品
public function add_goods ( $data ) {
if ( $this->has_goods( $data['goods_name'], $data['goods_sku'] ) ) {
return false;
} else {
$rs = $this->db->add( $this->goods_tb_name, $data );
if ( $rs ) {
$insert_id = mysql_insert_id();
$data = array(
'goods_id' => $insert_id,
'goods_thumb' => $insert_id . '_1_thumb.jpg',
'goods_center' => $insert_id . '_2_center.jpg',
'goods_original' => $insert_id . '_3_original.jpg',
);
return $this->db->add( $this->gallery_tb_name, $data );
} else {
return false;
}
}
}
//添加商品之前,判断是否存在同样的商品名或者货号
public function has_goods ( $goods_name, $goods_sku ) {
$sql = "SELECT * FROM {$this->goods_tb_name} WHERE goods_name = '$goods_name' OR goods_sku = '$goods_sku'";
$row = $this->db->get_row( $sql );
if ( $row ) {
return true;
} else {
return false;
}
}
//取出一条商品所有的信息
public function get_goods_detail ( $goods_id ) {
return $this->db->get_row( "SELECT * FROM {$this->goods_tb_name} WHERE goods_id = $goods_id" );
}
//更新商品
public function update_goods ( $goods_id, $data ) {
return $this->db->update( $this->goods_tb_name, $data, "WHERE goods_id = $goods_id" );
}
//删除商品
public function del_goods ( $goods_id ) {
return $this->db->query( "DELETE FROM {$this->goods_tb_name} WHERE goods_id = $goods_id" );
}
//查找商品
public function get_goods_list ( $p = '', $page_size ='' ) {
$sql = "SELECT g.*,c.cat_name FROM {$this->goods_tb_name} g LEFT JOIN {$this->cat_tb_name} c ON
g.cat_id = c.cat_id";
return $this->db->get_all( $sql );
}
}
// $g = new GoodsModel( $db );
// // print_r( $g->get_goods_list() );
// $data = array(
// 'cat_id' => 7,
// // 'goods_name' => '荣耀 6 (H60-L01) 3GB内存标准版 白色 移动4G手机',
// // 'goods_name' => '华为 畅享5 朦胧灰 全网通版4G手机 双卡双待',
// 'goods_name' => '荣耀 6 Plus (PE-TL20) 3GB内存标准版 白色 移动4G手机 双卡双待双通',
// 'goods_desc' => '荣耀 6 (H60-L01) 3GB内存标准版 白色 移动4G手机',
// 'goods_sku' => 10016,
// 'goods_stock' => 100,
// 'goods_price' => 2000,
// 'is_show' => 1,
// );
// $res = $g->add_goods( $data );
// // $res = $g->update_goods( 16, $data );
// // $res = $g->del_goods( 16 );
// if ( $res ) {
// echo "ok";
// } else {
// echo "error";
// }
// print_r( $g->get_goods_detail( 16 ) );
?>