Welcome Guest, Not a member yet? Register   Sign In
Universal Save & Edit methods?
#6

(This post was last modified: 03-12-2016, 06:30 PM by solidcodes.)

Here is a good example of a universal save method.

Code:
function save($op=NULL,$where=array())
   {
        $model_for="Product ";
        $return=(object)array('status'=>false,'data'=>'Error Occured');
        $this->create(); //Initializes a model for writing.
        $posted_data=$this->input->post();
        $data=array();
        if(isset($posted_data) && is_array($posted_data) && count($posted_data))
        {
           foreach($this->input->post() as $field=>$value)
           {
              $data[trim($field)]=(is_array($value) || is_object($value)) ? $value : trim($value);
           }
        }
        switch($op)
        {
          case 'add':
            $this->data=$data;
            if($this->insert())
            {
              $return->status=true;
              $return->data=$model_for."added successfully";
            }
          break;
          case 'edit':
            $this->data=$data;  
            $this->where=$where;
            if($this->update())
            {
              $return->status=true;
              $return->data=$model_for."edited successfully";
            }
          break;
          case 'del':
             $this->primaryKey='id';
             $this->id=$where['id'];
             $this->remove();
             $return->status=true;
             $return->data=$model_for."removed successfully";
          
          break;
          default:
          break;
        }
        return $return;    
    }

The reason I'm asking because perhaps their codes are better than what I have here.
@Narf
Disappointed to you Sad
No SEO spam
Reply


Messages In This Thread
Universal Save & Edit methods? - by solidcodes - 03-12-2016, 12:06 AM
RE: Universal Save & Edit methods? - by dmyers - 03-12-2016, 08:55 AM
RE: Universal Save & Edit methods? - by solidcodes - 03-12-2016, 06:24 PM
RE: Universal Save & Edit methods? - by Narf - 03-13-2016, 04:50 AM



Theme © iAndrew 2016 - Forum software by © MyBB