• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Form Help

#11
[eluser]garycocs[/eluser]
The form is actually on every page in the sidebar

Code:
<div id="sidebar">

<div id="adsidebar">
&lt;!--Put your 336 x 300 ad code here--&gt;
</div>

<div id="sidebarleft">

<h2>Add Video</h2>

&lt;?php
    echo form_open('home/addvideo');
    echo "<p>Artist<br />";
    echo form_input('artist', '');
    echo "</p><p>Title<br />";
    echo form_input('name', '');
    echo "</p><p>YouTube Link<br />";
    echo form_input('link', '');
    echo "</p><p>Song Type<br />";    
    echo form_dropdown('type', $formtypearray);
    echo "</p><p>Song Year<br />";    
    echo form_dropdown('age', $formagearray);
    echo "</p><p>";
    echo form_submit('submit', 'Add Video!');
    echo "</p>";    
    echo form_close();
?&gt;

<p>&nbsp;</p>
<h2>Latest Videos</h2>
<ul>    
    <li><a href='http://karaokeyoutube.net/?p=3' title='test'>test</a></li>
    <li><a href='http://karaokeyoutube.net/?p=1' title='Hello world!'>Hello world!</a></li>
</ul>



</div>

<div id="sidebarright">


<h2>Search</h2>
&lt;form method="get" action="/index.php"&gt;
&lt;input type="text" value=" Type and hit enter ..." name="s" id="s"&gt;
&lt;input type="hidden" id="searchsubmit" value="Search" /&gt;
&lt;/form&gt;

<h2>Pages</h2>
<ul><li class="page_item page-item-2"><a href="http://karaokeyoutube.net/?page_id=2" title="About">About</a></li>
</ul>

<h2>Meta</h2>
<ul>
<li><a href="http://karaokeyoutube.net/wp-admin/">Site Admin</a></li><li><a href="http://karaokeyoutube.net/wp-login.php?action=logout&amp;_wpnonce=9aa3b47dea">Log out</a></li>
</ul>


</div>
</div>

The controller function is then the following:

Code:
function addvideo()
    {
        $rules['name'] = "required";
        $rules['link'] = "required";
        $rules['artist'] = "required";
        $rules['type'] = "required";
        $rules['age'] = "required";
        
        $this->validation->set_rules($rules);
        
        if($this->validation->run() == FALSE) {
            redirect('home/');
        } 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);            
        }
    }

I'm not sure if I'm explaining things right? Does this make sense to you?

If they enter the wrong data I'd like to return to the homepage or something giving the errors? Do I have to embed the home page data here again or can I call the index() function passing in the errors?

Thanks for your patience!

#12
[eluser]bretticus[/eluser]
Quote:If they enter the wrong data I’d like to return to the homepage or something giving the errors? Do I have to embed the home page data here again or can I call the index() function passing in the errors?

I see you trying to do a redirect on form errors. This is not the right approach in CI. Unless you use sessions to serialize your errors, you lose access to them in your redirect. I always load the default view for the controller. Validation, by default, will always fail when the controller loads. This let's me load the form on a standard page load and reload the form (because it's the same view) on subsequent form validation errors. Best of all, the form values that did not fail are still filled in and I can use form_error() to show an error right next to the field(s) that failed. I'm not sure what your strategy is, but this should work regardless of whether or not the current controller is the path specified in the form action.

Code:
// rules, etc.
if($this->validation->run() == FALSE) {
//load default view for this controller action/method/function
} else {
//everything is great. upload video, call model methods and redirect to video.
}


If the form is available on all views in every sidebar, just make a view with the markup for that single form and "include" it into each of your views. You can use the standard php include function but I find it easier (allthough questionable form) to just call it as a view inside the view that is called from the controller. Because that form is always included, you can always send the errors back to the view via the form validation class and display them (although this scenario means most likely that they will always end up landing on your addvideo action of your controller. However, you could serialize the referrer and redirect to the original view upon a successful upload. Or, better yet, redirect to the page that has the newly-uploaded video.)

Another method people are found of that I don't particularly agree with is calling multiple views in the controller like so:

Code:
if($this->validation->run() == FALSE) {
$this->load->view('header', $data);
$this->load->view('sidenav', $data);
$this->load->view('videos', $data);
$this->load->view('footer', $data);
}

Each of your layouts are made up of these individual views. The reason I don't like it is because if you ever have a designer or someone less savvy of what is going on in your application that needs to work on it, he or she will be very confused (I work with designers.)

#13
[eluser]garycocs[/eluser]
So what you're saying is leave the redirects alone and go for:

Code:
function addvideo()
    {
        $rules['name'] = "required";
        $rules['link'] = "required";
        $rules['artist'] = "required";
        $rules['type'] = "required";
        $rules['age'] = "required";
        
        $this->validation->set_rules($rules);
        
        if($this->validation->run() == FALSE) {

            $data['title']    = "Karaoke on YouTube - All your karaoke needs in one place!";
            $data['base']       = $this->config->item('base_url');
            $data['css']        = $this->config->item('css');    
        
            $data['queryheaderlist'] = $this->home_model->get_header_list();
            $data['videolist'] = $this->home_model->get_video_list('10','0','0');
            $data['formtypearray'] = $this->home_model->form_type();
            $data['formagearray'] = $this->home_model->form_age();
        
            $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();

            $data['title']    = "Karaoke on YouTube - All your karaoke needs in one place!";
            $data['base']       = $this->config->item('base_url');
            $data['css']        = $this->config->item('css');    
        
            $data['queryheaderlist'] = $this->home_model->get_header_list();
            $data['videolist'] = $this->home_model->get_video_list('1','0',$this->uri->segment(3));
            $data['formtypearray'] = $this->home_model->form_type();
            $data['formagearray'] = $this->home_model->form_age();
        
            $this->load->view('video_view',$data);

        }
    }

I can understand this now, that's pretty straight forward, but what I'm wondering is, isn't this duplicating the code? As in my home page controller looks like this:

Code:
function index()
    {        
        $data['title']    = "Karaoke on YouTube - All your karaoke needs in one place!";
        $data['base']       = $this->config->item('base_url');
        $data['css']        = $this->config->item('css');    
        
        $data['queryheaderlist'] = $this->home_model->get_header_list();
        $data['videolist'] = $this->home_model->get_video_list('10','0','0');
        $data['formtypearray'] = $this->home_model->form_type();
        $data['formagearray'] = $this->home_model->form_age();
        
        $this->load->view('home_view',$data);
    }

Is there a way of calling the home page and all the lingo without duplicating the code???

#14
[eluser]bretticus[/eluser]
Actually, I'd probably load your video controller as the default for addvideo() (failed validation and initial page load.) and then redirect in place of your video_view load.

#15
[eluser]garycocs[/eluser]
Sorry could you explain this another bit?? Not getting that last post Sad

#16
[eluser]bretticus[/eluser]
[quote author="garycocs" date="1249516903"]Sorry could you explain this another bit?? Not getting that last post Sad[/quote]

Code:
function index()
    {        
        $data['title']    = "Karaoke on YouTube - All your karaoke needs in one place!";
        $data['base']       = $this->config->item('base_url');
        $data['css']        = $this->config->item('css');    
        
        $data['queryheaderlist'] = $this->home_model->get_header_list();
        $data['videolist'] = $this->home_model->get_video_list('10','0','0');
        $data['formtypearray'] = $this->home_model->form_type();
        $data['formagearray'] = $this->home_model->form_age();
        
        $this->load->view('home_view',$data);
    }  

function addvideo()
    {
        $rules['name'] = "required";
        $rules['link'] = "required";
        $rules['artist'] = "required";
        $rules['type'] = "required";
        $rules['age'] = "required";
        
        $this->validation->set_rules($rules);
        
        if($this->validation->run() == FALSE) {

            $data['title']    = "Karaoke on YouTube - All your karaoke needs in one place!";
            $data['base']       = $this->config->item('base_url');
            $data['css']        = $this->config->item('css');    
        
            $data['queryheaderlist'] = $this->home_model->get_header_list();
            $data['videolist'] = $this->home_model->get_video_list('10','0','0');
            $data['formtypearray'] = $this->home_model->form_type();
            $data['formagearray'] = $this->home_model->form_age();
        
           $this->load->view('addvideo_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('controller/viewvideo/' . $id);

        }
    }
    
    function viewvideo()
    {
         $data['title']    = "Karaoke on YouTube - All your karaoke needs in one place!";
            $data['base']       = $this->config->item('base_url');
            $data['css']        = $this->config->item('css');    
        
            $data['queryheaderlist'] = $this->home_model->get_header_list();
            $data['videolist'] = $this->home_model->get_video_list('1','0',$this->uri->segment(3));
            $data['formtypearray'] = $this->home_model->form_type();
            $data['formagearray'] = $this->home_model->form_age();
        
            $this->load->view('video_view',$data);
    }

#17
[eluser]garycocs[/eluser]
So have it up and running now, it was great practice learning the ins and outs of CI, next step will be another site and ajax.

So here it is:

http://www.karaokeyoutube.net/

This is my controller:
Code:
&lt;?php

class Home extends Controller {

    var $base;
    var $css;
    
    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>');

        $rules['name'] = "required";
        $rules['link'] = "callback_link_check";
        $rules['artist'] = "required";
        $rules['type'] = "required";
        $rules['age'] = "required";

        $this->validation->set_rules($rules);
                
        $fields['name'] = 'Name';
        $fields['link'] = 'YouTube Link';
        $fields['artist'] = 'Artist';
        $fields['type'] = "Type";
        $fields['age'] = 'Age';

        $this->validation->set_fields($fields);
        


    }
    
    function index()
    {        
        
        $data['title']    = "Karaoke on YouTube - All your karaoke needs in one place!";
        $data['base']       = $this->config->item('base_url');
        $data['css']        = $this->config->item('css');    
        
        $data['queryheaderlist'] = $this->home_model->get_header_list();
        $data['querylatestlist'] = $this->home_model->get_latest_list();    
        $data['videolist'] = $this->home_model->get_video_list('10','0','0');
        $data['formtypearray'] = $this->home_model->form_type();
        $data['formagearray'] = $this->home_model->form_age();
        
        $this->load->view('home_view',$data);
    }
    
    function type()
    {        
        $data['title']    = "Karaoke on YouTube - All your karaoke needs in one place!";
        $data['base']       = $this->config->item('base_url');
        $data['css']        = $this->config->item('css');    
        
        $data['queryheaderlist'] = $this->home_model->get_header_list();
        $data['querylatestlist'] = $this->home_model->get_latest_list();            
        $data['videolist'] = $this->home_model->get_video_list('10000',$this->uri->segment(3),'0');
        $data['formtypearray'] = $this->home_model->form_type();
        $data['formagearray'] = $this->home_model->form_age();
        
        $this->load->view('type_view',$data);
    }
    
    
    function addvideo()
    {
        
        if($this->validation->run() == FALSE) {
            
            $data['title']    = "Karaoke on YouTube - All your karaoke needs in one place!";
            $data['base']       = $this->config->item('base_url');
            $data['css']        = $this->config->item('css');    

            $data['queryheaderlist'] = $this->home_model->get_header_list();
            $data['querylatestlist'] = $this->home_model->get_latest_list();                
            $data['videolist'] = $this->home_model->get_video_list('10','0','0');
            $data['formtypearray'] = $this->home_model->form_type();
            $data['formagearray'] = $this->home_model->form_age();

            $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['title']    = "Karaoke on YouTube - All your karaoke needs in one place!";
        $data['base']       = $this->config->item('base_url');
        $data['css']        = $this->config->item('css');    
        
        $data['queryheaderlist'] = $this->home_model->get_header_list();
        $data['querylatestlist'] = $this->home_model->get_latest_list();            
        $data['videolist'] = $this->home_model->get_video_list('1','0',$this->uri->segment(3));
        $data['formtypearray'] = $this->home_model->form_type();
        $data['formagearray'] = $this->home_model->form_age();
        
        $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;
        }
    }
}

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

As you can see there is some duplication in the code?

Is there ways of shortening this code?? Embedding one controller in another or something??

Maybe I should be putting more of the code in Home() ???

Thanks again for your help, it was great!

#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:
&lt;?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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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