Welcome Guest, Not a member yet? Register   Sign In
Basic Form Help
#18

[eluser]bretticus[/eluser]
Since much of your code repeats, here's one way to set the lists and css and base_url only once. This way you can opt for the data in a controller instead of loading it with every controller instance. You can still add to the data array before sending it to the view. Also, your rules shouldn't bet set in your constructor because you don't want that overhead each time you load the controller as all but one method actually use it. I left the configuration in the constructor in case you need to use it in another method later.

Code:
<?php

class Home extends Controller {

    var $data;
    var $video_form_config;
    
    function Home()
    {
        parent::Controller();    
        $this->load->scaffolding('videos');
        $this->load->model('home_model');
        $this->load->helper('form');
        $this->load->helper('url');    
        $this->load->library('validation');

        $this->validation->set_error_delimiters('<div class="error">', '</div>');        
        
        $this->video_form_config = array(
            array(
             'field'   => 'name',
             'label'   => 'Name',
             'rules'   => 'required'
            ),
            array(
             'field'   => 'link',
             'label'   => 'YouTube Link',
             'rules'   => 'callback__link_check'
            ),
            array(
             'field'   => 'artist',
             'label'   => 'Artist',
             'rules'   => 'required'
            ),
            array(
             'field'   => 'type',
             'label'   => 'Type',
             'rules'   => 'required'
            ),
            array(
             'field'   => 'age',
             'label'   => 'Age',
             'rules'   => 'required'
            )
         );

        $this->data = array();
        $this->data['base'] = $this->config->item('base_url');
        $this->data['css']  = $this->config->item('css');
    }  
    
    function index()
    {
        $data = $this->_get_global_data();
        $data['videolist'] = $this->home_model->get_video_list('10','0','0');
        $data['title']    = "Karaoke on YouTube - All your karaoke needs in one place!";        
        $data['index_specific_data'] = 'something more';
        $this->load->view('home_view',$data);
    }
    
    function type()
    {
        $data = $this->_get_global_data();
        $data['title']    = "Karaoke on YouTube - All your karaoke needs in one place!";      
        $data['videolist'] = $this->home_model->get_video_list('10000',$this->uri->segment(3),'0');        
        $this->load->view('type_view',$data);
    }
    
    
    function addvideo()
    {
        $this->form_validation->set_rules($this->video_form_config);
        if($this->validation->run() == FALSE) {
            $data = $this->_get_global_data();
            $data['title']    = "Karaoke on YouTube - All your karaoke needs in one place!";              
            $data['videolist'] = $this->home_model->get_video_list('10','0','0');
            $this->load->view('home_view',$data);
            
        } else {
            
            $link = $_POST['link'];
            $linkarray = explode("=", $link);        
            $link=$linkarray[1];
            $linkarray = explode("&", $link);        
            $link=$linkarray[0];
            $data = array(
                'name' => $_POST['name'],
                'link' => $link,
                'artist' => $_POST['artist'],
                'type' => $_POST['type'],
                'age' => $_POST['age']
                );
            $this->db->insert('videos', $data);

            $id = $this->db->insert_id();
            redirect('home/video/'.$id);            
        }
    }    
    
    function video()
    {
        $data = $this->_get_global_data();
        $data['title']    = "Karaoke on YouTube - All your karaoke needs in one place!";              
        $data['videolist'] = $this->home_model->get_video_list('1','0',$this->uri->segment(3));
        $this->load->view('video_view',$data);
    }
    
    function _link_check($str)
    {
        if (preg_match('/http:\/\/www\.youtube\.com\//', $str))
        {
            return TRUE;
        }
        else
        {
            $this->validation->set_message('link_check', 'The %s field must contain http://www.youtube.com/.....');
            return FALSE;
        }
    }

    function _get_global_data()
    {
        $data = $this->data;
        $data['queryheaderlist'] = $this->home_model->get_header_list();
        $data['querylatestlist'] = $this->home_model->get_latest_list();
        $data['formtypearray'] = $this->home_model->form_type();
        $data['formagearray'] = $this->home_model->form_age();
        return $data;
    }
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */


Hope this helps.


Messages In This Thread
Basic Form Help - by El Forum - 08-03-2009, 08:27 AM
Basic Form Help - by El Forum - 08-03-2009, 08:57 AM
Basic Form Help - by El Forum - 08-03-2009, 08:59 AM
Basic Form Help - by El Forum - 08-03-2009, 09:01 AM
Basic Form Help - by El Forum - 08-03-2009, 11:43 AM
Basic Form Help - by El Forum - 08-03-2009, 01:36 PM
Basic Form Help - by El Forum - 08-04-2009, 04:39 PM
Basic Form Help - by El Forum - 08-04-2009, 07:41 PM
Basic Form Help - by El Forum - 08-05-2009, 07:14 AM
Basic Form Help - by El Forum - 08-05-2009, 09:37 AM
Basic Form Help - by El Forum - 08-05-2009, 10:18 AM
Basic Form Help - by El Forum - 08-05-2009, 11:11 AM
Basic Form Help - by El Forum - 08-05-2009, 12:23 PM
Basic Form Help - by El Forum - 08-05-2009, 12:41 PM
Basic Form Help - by El Forum - 08-05-2009, 01:01 PM
Basic Form Help - by El Forum - 08-05-2009, 01:09 PM
Basic Form Help - by El Forum - 08-11-2009, 07:51 AM
Basic Form Help - by El Forum - 08-11-2009, 08:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB