_table_.class.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * 数据库表:(可将本文件另存为)
  4. *
  5. * @createtime 2018/10/17
  6. * @author 空竹
  7. * @copyright 芝麻开发(http://www.zhimawork.com)
  8. */
  9. class Table_admin extends Table {
  10. protected $table_name = '';//表名,不带前缀
  11. protected $table_id = '';//指定ID字段名称,必须
  12. protected $table_status = '';//指定状态字段名称,如果有
  13. protected $table_order = '';//指定排序字段名称,如果有
  14. //数据库结构
  15. //完整展示数据库所有字段,替换字段名称
  16. protected function struct(){
  17. $attr = array();
  18. //$attr['id'] = 'xxx_id';
  19. return $attr;
  20. }
  21. //增
  22. //@param $attr array -- 键值同struct()返回的数组
  23. public function add($attr){
  24. $param = array (
  25. //'xxx' => array('string', $attr['yyy'])
  26. );
  27. return $this->pdo->sqlinsert($this->table_fullname, $param);
  28. }
  29. //改
  30. //@param $attr array -- 键值同struct()返回的数组
  31. public function edit($id, $attr){
  32. $param = array (
  33. //'xxx' => array('string', $attr['yyy'])
  34. );
  35. $where = array (
  36. //'xxx_id' => array('number', $id)
  37. );
  38. return $this->pdo->sqlupdate($this->table_fullname, $param, $where);
  39. }
  40. //获取列表(分页)
  41. //$count、$page和$pagesize都为0时,返回全部结果(适用于无需分页的情况)
  42. //
  43. //@param $filter array -- 过滤条件,格式见Table::filterToWhere
  44. //@param $count -- 0:返回列表 1:返回结果数量
  45. //@param $page -- 当前第几页
  46. //@param $pagesize -- 每页数量
  47. public function getList($filter = array(), $count = 0, $page = 0, $pagesize = 0){
  48. $where = $this->filterToWhere($filter);
  49. if($count == 0){//列表
  50. $sql = "select * from ". $this->table_fullname ." $where ";
  51. if($this->table_order){//排序
  52. $sql_order = " order by ". $this->table_order ." asc, ". $this->table_id ." desc ";
  53. }else{
  54. $sql_order = " order by ". $this->table_id ." desc ";
  55. }
  56. $sql .= $sql_order;
  57. if($page > 0){//分页
  58. $startrow = ($page - 1) * $pagesize;
  59. $sql_limit = " limit $startrow, $pagesize";
  60. $sql .= $sql_limit;
  61. }
  62. $rs = $this->pdo->sqlQuery($sql);
  63. $r = array();
  64. if($rs){
  65. foreach($rs as $key => $val){
  66. $r[$key] = $this->dataToAttr($val);
  67. }
  68. return $r;
  69. }else{
  70. return $r;
  71. }
  72. }else{//统计
  73. $sql = "select count(*) as c from ". $this->table_fullname . " $where ";
  74. $rs = $this->pdo->sqlQuery($sql);
  75. if($rs){
  76. return $rs[0]['c'];
  77. }else{
  78. return 0;
  79. }
  80. }
  81. }
  82. }
  83. ?>