by
1
6
4,203
6
Top 1% !
Popular
Famous
Specified
OpenSource
MultiPlatform
Popularity: 2201st place
No tags for this snippet yet.
FrameworkCodeigniter
Languagephp
phpCodeigniter

CODEIGNITER | DB Functions

Commonly used database function using codeigniter
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/1175525/CODEIGNITER---DB-Functions?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class General_model extends CI_Model { public function __construct() { parent::__construct(); } // Return all records in the table public function get_all($table) { $q = $this->db->get($table); if($q->num_rows() > 0) { return $q->result(); } return array(); } // Return only one row public function get_row($table,$primaryfield,$id) { $this->db->where($primaryfield,$id); $q = $this->db->get($table); if($q->num_rows() > 0) { return $q->row(); } return false; } // Return one only field value public function get_data($table,$primaryfield,$fieldname,$id) { $this->db->select($fieldname); $this->db->where($primaryfield,$id); $q = $this->db->get($table); if($q->num_rows() > 0) { return $q->result(); } return array(); } // Insert into table public function add($table,$data) { return $this->db->insert($table, $data); } // Update data to table public function update($table,$data,$primaryfield,$id) { $this->db->where($primaryfield, $id); $q = $this->db->update($table, $data); return $q; } // Delete record from table public function delete($table,$primaryfield,$id) { $this->db->where($primaryfield,$id); $this->db->delete($table); } // Check whether a value has duplicates in the database public function has_duplicate($value, $tabletocheck, $fieldtocheck) { $this->db->select($fieldtocheck); $this->db->where($fieldtocheck,$value); $result = $this->db->get($tabletocheck); if($result->num_rows() > 0) { return true; } else { return false; } } // Check whether the field has any reference from other table // Normally to check before delete a value that is a foreign key in another table public function has_child($value, $tabletocheck, $fieldtocheck) { $this->db->select($fieldtocheck); $this->db->where($fieldtocheck,$value); $result = $this->db->get($tabletocheck); if($result->num_rows() > 0) { return true; } else { return false; } } // Return an array to use as reference or dropdown selection public function get_ref($table,$key,$value,$dropdown=false) { $this->db->from($table); $this->db->order_by($value); $result = $this->db->get(); $array = array(); if ($dropdown) $array = array("" => "Please Select"); if($result->num_rows() > 0) { foreach($result->result_array() as $row) { $array[$row[$key]] = $row[$value]; } } return $array; } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus