I develop a dynamic model for our http://www.votebd.org website to work with database with minimum smarter code. It can insert, update, delete, select data from database through only one related function. The code example given below:
votebd_model-
class Votebd_model extends Model
{
function Votebd_model()
{
parent::Model();
}
/**
* Generates an insert sql query from the parameters
*
* @param string $table The name of the table
* @param array $array An associative array with the values similar to column=>value
* @return string The sql query
*/
function getInsertSQL($table, $array) {
$sql = ‘INSERT INTO ‘.$table;
$columns = ”;
$values = ”;
foreach($array as $key => $value) {
$columns .= $key.’, ‘;
if($value != “”) {
$values .= “‘”.addslashes($value).”‘, “;
}
else {
$values .= “‘NULL’, “;
}
}
$columns = substr($columns, 0, -2);
$values = substr($values, 0, -2);
$sql .= “($columns) VALUES ($values)”;
$query = $this->db->query($sql);
return $query;
}
/**
* Generates an update sql query from the parameters
*
* @param string $table The name of the table
* @param array $array An associative array with the values similar to column=>value
* @param string $where What to limit the update to. Cannot be blank.
* @return string The sql query
*/
function getUpdateSQL($table, $array, $where) {
if(trim($where) == “”) {
return;
}
$sql = ‘UPDATE ‘.$table.’ SET ‘;
foreach($array as $key => $value) {
if($value != “”) {
$sql .= $key.”=’”.addslashes($value).”‘, “;
}
else {
$sql .= $key.”=’NULL’, “;
}
}
$sql = substr($sql, 0, -2);
$sql .= ” WHERE $where”;
$query = $this->db->query($sql);
return $query;
}
/**
* Generates an delete sql query from the parameters
*
* @param string $table The name of the table
* @param string $where What to limit the delete to. Cannot be blank.
* @return string The sql query
*/
function getDeleteSQL($table, $where) {
if(trim($where) == “”) {
return;
}
$sql = ‘DELETE * FROM ‘.$table;
$sql .= ” WHERE $where”;
$query = $this->db->query($sql);
return $query;
}
/**
* Generates an delete all sql query from the parameters
*
* @param string $table The name of the table. Cannot be blank.
* @return string The sql query
*/
function getDeleteAllSQL($table) {
if(trim($table) == “”) {
return;
}
$query = $this->db->query(‘DELETE * FROM ‘.$table);
return $query;
}
/**
* Generates an select sql query from the parameters
*
* @param string $table The name of the table
* @param string $where What to limit the delete to. Cannot be blank.
* @return string The sql query
*/
function getSelectSQL($table, $where) {
if(trim($where) == “”) {
return;
}
$sql = ‘SELECT * FROM ‘.$table;
$sql .= ” WHERE $where”;
$query = $this->db->query($sql);
return $query;
}
}//end class
implementation in districtList controller class :
#Get POST data
$DISTRICT_NAME = $this->input->post(‘DISTRICT_NAME’);
$DISTRICT_NAME_BNG = $this->input->post(‘DISTRICT_NAME_BNG’);
$DISTRICT_CODE = $this->input->post(‘DISTRICT_CODE’);
$DIVISION_ID = $this->input->post(‘DIVISION_ID’);
$TOTAL_PEOPLE = $this->input->post(‘TOTAL_PEOPLE’);
$TOTAL_VOTER_MALE = $this->input->post(‘TOTAL_VOTER_MALE’);
$TOTAL_VOTER_FEMALE = $this->input->post(‘TOTAL_VOTER_FEMALE’);
$DISTRICT_INFO = $this->input->post(‘DISTRICT_INFO’);
$table = “district_list”;
$array = array(
‘DISTRICT_CODE’ => $DISTRICT_CODE,
‘DIVISION_ID’ => $DIVISION_ID,
‘DISTRICT_NAME’ => $DISTRICT_NAME,
‘TOTAL_PEOPLE’ => $TOTAL_PEOPLE,
‘TOTAL_VOTER_MALE’ => $TOTAL_VOTER_MALE,
‘TOTAL_VOTER_FEMALE’ => $TOTAL_VOTER_FEMALE,
‘DISTRICT_NAME_BNG’ => $DISTRICT_NAME_BNG,
‘DISTRICT_INFO’ => $DISTRICT_INFO,
);
#work with our database
$this->load->model(‘Votebd_model’,”, TRUE);
$data['query_division'] = $this->Votebd_model->getSelectAllSQL(“division_list”);
$this->Votebd_model->getInsertSQL($table, $array);
$this->load->view(‘admin/adddistrictlist_view’, $data);
Same way u can update, delete data from table
Filed under: CodeIgniter.model





want more examples on using code ifgniter – a set of tutorials will be good!
I like code igniter. Carry on
[...] Working with models in CodeIgniter [...]
Good one.
If you’re interested in something more robust, you can check my one as well: http://www.phpfour.com/blog/2008/07/12/extended-model-for-codeigniter/
Thanks
It helped me a lot.