• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping $_POST values into a CRUD Model

#1
[eluser]opel[/eluser]
I have used PHP to handcode my own CMS but have been playing bout with CI. I am aware there are plenty of CRUD libraries out there but I am trying to create my own controllers/models to add/edit/delete data so that I can understand my app and CI before.

I have tried to update my old class methods for insert and update into CI but I am not having much luck. What I am trying to do is take all the values of $_POST, unset the button value and then generate an array that I can use in AR to insert/update my db

the code that I am trying below is loosing the "$this" part when I try to print_r as a test.

Code:
function process_add() {
      unset($_POST['Submit']);

     foreach($_POST as $key=>$value){
        
        $data[$key] = $this->post->('$value');
     }

}


Could anyone give me any suggestions or point me in the way of a tutorial that may help?

#2
[eluser]Pascal Kriete[/eluser]
It looks like all you're trying to do is copy the post array into your $data array. Why? They're going to be identical. You could just unset the value and continue using POST. Or copy the whole POST array and use the copy.

I think what you're shooting for in the loop is this:
Code:
// old
$this->post->('$value');

// new
$this->input->post($key);

But once again, I don't see why you would do this.
You could expand it to filter disallowed keys. That would be a good idea, and could be accomplished with a lot more precision and less code:
Code:
function _filter_input($allowed, $data)
{
    $allowed = array_flip($allowed);
    return array_intersect_key($data, $allowed);
}

Then you can pass the $_POST array straight into the model. In the model you call said function:
Code:
function create($data)
{
    $allowed    = array('title', 'url_title', 'file_path');
    $data        = $this->_filter_input($allowed, $data);

    $this->db->insert('downloads', $data);
    return $this->db->insert_id();
}

Let me know if that answered any part of your question Wink .

#3
[eluser]opel[/eluser]
I'll try it tonight and let you know how I get on, thanks for the advice?

#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);

    
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.