Commonly used db functions for CodeIgniter


/ Published in: PHP
Save to your folder(s)

This is a common model used to run some common functions like CRUD functions, get reference data from table, check for dependencies etc. So in other model, we don't have to add those function repeatedly


Copy this code and paste it in your HTML
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class General_model extends CI_Model
  4. {
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. }
  9.  
  10. // Return all records in the table
  11. public function get_all($table)
  12. {
  13. $q = $this->db->get($table);
  14. if($q->num_rows() > 0)
  15. {
  16. return $q->result();
  17. }
  18. return array();
  19. }
  20.  
  21. // Return only one row
  22. public function get_row($table,$primaryfield,$id)
  23. {
  24. $this->db->where($primaryfield,$id);
  25. $q = $this->db->get($table);
  26. if($q->num_rows() > 0)
  27. {
  28. return $q->row();
  29. }
  30. return false;
  31. }
  32.  
  33. // Return one only field value
  34. public function get_data($table,$primaryfield,$fieldname,$id)
  35. {
  36. $this->db->select($fieldname);
  37. $this->db->where($primaryfield,$id);
  38. $q = $this->db->get($table);
  39. if($q->num_rows() > 0)
  40. {
  41. return $q->result();
  42. }
  43. return array();
  44. }
  45.  
  46. // Insert into table
  47. public function add($table,$data)
  48. {
  49. return $this->db->insert($table, $data);
  50. }
  51.  
  52. // Update data to table
  53. public function update($table,$data,$primaryfield,$id)
  54. {
  55. $this->db->where($primaryfield, $id);
  56. $q = $this->db->update($table, $data);
  57. return $q;
  58. }
  59.  
  60. // Delete record from table
  61. public function delete($table,$primaryfield,$id)
  62. {
  63. $this->db->where($primaryfield,$id);
  64. $this->db->delete($table);
  65. }
  66.  
  67. // Check whether a value has duplicates in the database
  68. public function has_duplicate($value, $tabletocheck, $fieldtocheck)
  69. {
  70. $this->db->select($fieldtocheck);
  71. $this->db->where($fieldtocheck,$value);
  72. $result = $this->db->get($tabletocheck);
  73.  
  74. if($result->num_rows() > 0) {
  75. return true;
  76. }
  77. else {
  78. return false;
  79. }
  80. }
  81.  
  82. // Check whether the field has any reference from other table
  83. // Normally to check before delete a value that is a foreign key in another table
  84. public function has_child($value, $tabletocheck, $fieldtocheck)
  85. {
  86. $this->db->select($fieldtocheck);
  87. $this->db->where($fieldtocheck,$value);
  88. $result = $this->db->get($tabletocheck);
  89.  
  90. if($result->num_rows() > 0) {
  91. return true;
  92. }
  93. else {
  94. return false;
  95. }
  96. }
  97.  
  98. // Return an array to use as reference or dropdown selection
  99. public function get_ref($table,$key,$value,$dropdown=false)
  100. {
  101. $this->db->from($table);
  102. $this->db->order_by($value);
  103. $result = $this->db->get();
  104.  
  105. $array = array();
  106. if ($dropdown)
  107. $array = array("" => "Please Select");
  108.  
  109. if($result->num_rows() > 0) {
  110. foreach($result->result_array() as $row) {
  111. $array[$row[$key]] = $row[$value];
  112. }
  113. }
  114. return $array;
  115. }
  116. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.