Welcome Guest, Not a member yet? Register   Sign In
Manage Records with Validation Class
#1

[eluser]section31[/eluser]
Are there any examples or open source scripts that show an extremely clean way to manage records using the validation class. Essentially what the scaffolding does, but writing it yourself manually. When I say clean, I mean proper and efficient I guess.

So, an example of adding, editing, and deleting a list of records.
#2

[eluser]imzyos[/eluser]
yep, not all functions of validation class, but a little example

http://www.michaelwales.com/2007/10/drun...t-us-form/
#3

[eluser]section31[/eluser]
Thanks for the reply but this is essentially the stuff that's on the user guide.

I wanted a sample CRUD Script. CRUD = Create, Read, Update, Delete Records Script.

I'm actually going to make one and post it, just to see if people can help me clean it up.

NOTE: I've only been using CI for a couple of days...still very new. I've just been browsing the thousands of lines of code, reading through the documentation, and just messing around. Haven't actually created a site or full script yet.
#4

[eluser]imzyos[/eluser]
Code:
<?php

    class CMS extends Controller
    {
        
        var $types = array(
            'text' => 'Text',
            'image' => 'Image',
            'quote' => 'Quote'
            );
    
        function CMS()
        {
            parent::Controller();
            $this->load->helper(array('url'));

        }
    
        function index()
        {    
            $this->load->database();
            $query = $this->db->get('posts');
            
            $posts = $this->db->get('posts');
        
            $data = array('posts' => $posts->result());
            
            $this->load->view('manager/actions', $data);
        }
        
        function insert()
        {    
            $this->load->helper(array('form'));
            
            $data = array('types' => $this->types);
                        
            $this->load->view('manager/insert', $data);                
        }
                
        function do_insert()
        {    
            $this->load->database();
            
            $date = $this->input->post('year').'-'.$this->input->post('month').'-'.$this->input->post('day');
            $time = $this->input->post('hour').':'.$this->input->post('minute').':'.$this->input->post('secound');    
            
            $post->title = $this->input->post('title');
            $post->date = $date;
            $post->time = $time;
            $post->type = $this->input->post('type');
            $post->comments = $this->input->post('comments');
            $post->body = $this->input->post('body');
                                    
            $this->db->insert('posts', $post);
            
            redirect('cms/index');
            
        }
        
        function edit($id)
        {
            $this->load->helper(array('form'));
            $this->load->database();
            $query = $this->db->getwhere('posts',array('id'=>$id));
            
            $query = $query->row();
            
            $post->id = $query->id;
            $post->title = $query->title;
            $post->body = $query->body;
            $post->day = substr($query->date,8,2);
            $post->month = substr($query->date,5,2);
            $post->year = substr($query->date,0,4);
            $post->hour = substr($query->time,0,2);
            $post->minute = substr($query->time,3,2);
            $post->secound = substr($query->time,6,4);
            $post->type = $query->type;
            $post->comments = $query->comments;
            
            $data = array('post' => $post,'types' => $this->types);
            $this->load->view('manager/edit', $data);
            
        }
        
        function do_edit()
        {
            
            $this->load->database();
            
            $date = $this->input->post('year').'-'.$this->input->post('month').'-'.$this->input->post('day');
            $time = $this->input->post('hour').':'.$this->input->post('minute').':'.$this->input->post('secound');    
            
            $post->title = $this->input->post('title');
            $post->date = $date;
            $post->time = $time;
            $post->type = $this->input->post('type');
            $post->comments = $this->input->post('comments');
            $post->body = $this->input->post('body');
                                    
            $this->db->where('id', $this->input->post('id'));
            $this->db->update('posts', $post);
        
            redirect('cms/index');
        }
        
        function delete($id)
        {
            $this->database();
            $this->db->delete('posts', array('id' => $id));
            redirect('cms/index');
        }
            
    }

?>

simple and maybe a little basic or some parts could be better but like an example are nice ^^
#5

[eluser]Majd Taby[/eluser]
section31, check out CodeExtinguisher (link in my signature), it's perfect for what you want. also watch http://codeextinguisher.com/screencasts/..._codex.mov

The only thing is, the version you see in the video isn't released yet, should be released in a few weeks though.
#6

[eluser]imzyos[/eluser]
yeah, CE rocks, but he want the 'clasic way' i guess :S
#7

[eluser]section31[/eluser]
hm, i didn't get any notification that someone had replied to this thread...sorry guys...

I haven't looked at code extinguisher yet, but I will.

I came up with the following as a sample CRUD. Can someone be so kind and look through the source and see if it's ok.
http://code9.info/codeigniter/index.php/records

I modified the validation class a tad because it needs it. I added a public property for the class called $_fields_keys which is used for building our validated fields array for pushing into our database and clearing our fields once a form has been submitted successfully.

clear_fields is a much needed method that clears all validated properties in case you want to display the form without the fields. It's used once the form is submitted and a record has been added successfully.

get_fields is also a much needed method that grabs all validated fields into an array so you can prep your array for either inserting or updated the record in your database.


Code:
function clear_fields() {
        foreach ($this->_fields_keys as $k) {
            $this->$k = '';
        }
    }

    function get_fields($insert = true) {
        $arr = array();
        foreach ($this->_fields_keys as $k) {
            // if we are prepping to insert, let's not include empty fields
            // to take advantage of default sql values
            if (!isset($_POST[$k]) || ($_POST[$k] == '' && $insert)) {
                continue;
            }
            $arr[$k] = $_POST[$k];
        }

        return $arr;
    }




Theme © iAndrew 2016 - Forum software by © MyBB