Home >Backend Development >PHP Tutorial >PHP data processing public class

PHP data processing public class

WBOY
WBOYOriginal
2016-07-25 08:45:371432browse
  1. /*======================================== ===========================*/
  2. /* File name: BaseLogic.class.php */
  3. /* Summary: Data processing public Class. */
  4. class BaseLogic extends MyDB {
  5. protected $tabName; //The name of the table
  6. protected $fieldList; //Field collection
  7. protected $messList;
  8. //============ ===============================
  9. // Function: add($postList)
  10. // Function: Add
  11. // Parameters: $postList Submitted variable list
  12. // Return: The newly inserted auto-increment ID
  13. //============================ ==============
  14. function add($postList) {
  15. $fieldList='';
  16. $value='';
  17. foreach ($postList as $k=>$v) {
  18. if(in_array($k, $this->fieldList)){
  19. $fieldList.=$k.",";
  20. if (!get_magic_quotes_gpc())
  21. $value .= "'".addslashes($ v)."',";
  22. else
  23. $value .= "'".$v."',";
  24. }
  25. }
  26. $fieldList=rtrim($fieldList, ",");
  27. $value= rtrim($value, ",");
  28. $sql = "INSERT INTO {$this->tabName} (".$fieldList.") VALUES(".$value.")";
  29. echo $sql;
  30. $result=$this->mysqli->query($sql);
  31. if($result && $this->mysqli->affected_rows >0 )
  32. return $this->mysqli-> insert_id;
  33. else
  34. return false;
  35. }
  36. //==================================== =======
  37. // Function: mod($postList)
  38. // Function: Modify table data
  39. // Parameter: $postList submitted variable list
  40. //============ ===============================
  41. function mod($postList) {
  42. $id=$postList["id"] ;
  43. unset($postList["id"]);
  44. $value='';
  45. foreach ($postList as $k=>$v) {
  46. if(in_array($k, $this->fieldList) ){
  47. if (!get_magic_quotes_gpc())
  48. $value .= $k." = '".addslashes($v)."',";
  49. else
  50. $value .= $k." = '".$ v."',";
  51. }
  52. }
  53. $value=rtrim($value, ",");
  54. $sql = "UPDATE {$this->tabName} SET {$value} WHERE id={$id }";
  55. return $this->mysqli->query($sql);
  56. }
  57. //======================== ==================
  58. // Function: del($id)
  59. // Function: Delete
  60. // Parameter: $id number or ID list array
  61. // Return : 0 Failure and success are the number of deleted records
  62. //======================================== ====
  63. function del($id) {
  64. if(is_array($id))
  65. $tmp = "IN (" . join(",", $id) . ")";
  66. else
  67. $tmp = "= $id";
  68. $sql = "DELETE FROM {$this->tabName} WHERE id " . $tmp ;
  69. return $this->mysqli->query($sql);
  70. }
  71. function get($id) {
  72. $sql = "SELECT * FROM {$this->tabName} WHERE id ={$id}";
  73. $result=$this->mysqli->query( $sql);
  74. if($result && $result->num_rows ==1){
  75. return $result->fetch_assoc();
  76. }else{
  77. return false;
  78. }
  79. }
  80. function getMessList( ){
  81. $message="";
  82. if(!empty($this->messList)){
  83. foreach($this->messList as $value){
  84. $message.=$value." }
  85. }
  86. return $message;
  87. }
  88. }
  89. ?>
Copy code

php


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn