Welcome Guest, Not a member yet? Register   Sign In
Looping $_POST values into a CRUD Model
#4

[eluser]opel[/eluser]
I managed to convert all my old functions which work fine. Not sure the best way to make them available to all my models though

My plan is to base a form on the add/edit example in the wiki where I have a "form" view and controller that processes add/edit functions and then use the crud functions of my model.

Is the best practice to create my own "Crud_model" that extends the CI model and then subclass that for my app, i noticed helpers are only for views and controllers.

My code at the moment is :

Code:
class Pages_model extends Model {
    
    
    
    function Pages_model()
    {
        parent::Model();
    }
    
    function view($table)
    {
        
        $query = $this->db->query('SELECT * FROM $table ');
        return $query->result_array();
        
    }
    
    
    function create($table)
    {
        
     $columns = '';
     $values = '';
        
    //remove last item (submit button)
    unset($_POST['submit']);

    //loop through post vars and remove any prefixed with __
    foreach ($_POST as $key => $value) { $columns .= "`".$key."`,"; }
    foreach ($_POST as $value)           { $values .= "'".$value."',";}
    $sql = "INSERT INTO $table (".rtrim($columns, ",").") VALUES (".rtrim($values, ",").")";
    return $this->db->query($sql);
    }
    
    function update($table, $id)
    {
        
        $value = '';
        
            unset($_POST['submit']);

            //loop through post vars and remove any prefixed with __
            foreach ($_POST as $key => $value) {
            $set .= "`".$key."`='".$value."',";  
            }

            $sql = "UPDATE $table SET ".rtrim($set, ",")." WHERE `id` = '$id' ";
            return $this->db->query($sql);
    }
    
    // Edit the Database
    function  delete($table, $id) {
    
    $sql = "DELETE FROM $table WHERE `id` = '$id' ";
    
    return $this->db->query($sql);

    
}


Messages In This Thread
Looping $_POST values into a CRUD Model - by El Forum - 09-23-2008, 05:16 PM
Looping $_POST values into a CRUD Model - by El Forum - 09-24-2008, 09:16 AM
Looping $_POST values into a CRUD Model - by El Forum - 09-24-2008, 10:16 AM
Looping $_POST values into a CRUD Model - by El Forum - 09-24-2008, 03:23 PM



Theme © iAndrew 2016 - Forum software by © MyBB