working with models in CodeIgniter

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

Advertisements

11 Responses

  1. want more examples on using code ifgniter – a set of tutorials will be good!

  2. I like code igniter. Carry on 🙂

  3. […] Working with models in CodeIgniter […]

  4. 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

  5. It helped me a lot.

  6. Trim is not working…. any other method is there to remove the space in php(codeigniter) not javascript

  7. Hey this is one of the best articles that I have read till date. I m really very much impressed with your work. It has been a pleasure reading this article of yours. I never knew that there was so much to learn in this subject. I am glad to have read your article.

  8. Thanks for sharing this group of lesson learned, you have added a few points that I need to go away and consider.

  9. […] Working with models in CodeIgniter […]

  10. Thanks for the article! Just browsing around online I get into some cool stuff. Anyways, back to school work…

  11. Thanks for every one of your labor on this website. Kate loves working on internet research and it’s really easy to understand why. My spouse and i notice all of the compelling mode you make valuable things by means of your website and therefore foster contribution from the others on the area so our favorite simple princess is truly learning a lot. Take pleasure in the remaining portion of the year. You’re carrying out a splendid job.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: