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

[eluser]garycocs[/eluser]
Hi Lads,

I'm back to have another go at codeigniter, I'm basically starting a new site where the user can add in a youtube link and some data into a database, very very basic stuff. I figured I'd get this site going first with codeigniter and then develop more complex sites.

So I can sort out views and all of that:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;?php $this->load->view('head_view');?&gt;
&lt;body&gt;
&lt;?php $this->load->view('header_div_view');?&gt;


<div id="wrapper">
<div id="mainbody">
<div id="content">


    <div class="window">

    <div class="paneleft">
    <a href="http://karaokeyoutube.net/?p=1" title="Hello world!"><img src="" alt="Hello world!" width="130px" height="97px" /></a></div>

    <div class="paneright">
    <h2 class="indextitle"><a href="http://karaokeyoutube.net/?p=1" rel="bookmark" title="Hello world!">Hello world!</a></h2>

    <div>
    <p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!</p>
    <div class="postfeedback">
    <a href="http://karaokeyoutube.net/?p=1#comments" class="commentslink"  title="Comment on Hello world!">Comments (1)</a></div>
    </div>
    </div>
    </div>


    <div class="pagenavigation">
    <div class="alignleft"></div>
    <div class="alignright"></div>
    </div>

</div>

&lt;?php $this->load->view('sidebar_div_view');?&gt;    

</div>

&lt;?php $this->load->view('footer_div_view');?&gt;    

</div>

&lt;/body&gt;
&lt;/html&gt;


Which I call from the main controller.

My question is about form structure, I have the form set up like so:
Code:
<h2>Add Video</h2>

&lt;?php
    echo form_open('email/send');
    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 />";    

    $typearray = array();
    $this->db->select('id');
    $this->db->select('name');
    $this->db->from('type');
    $query = $this->db->get();

    if ($query->num_rows()>0)
    {
        foreach($query->result() as $row)
        {
            $typearray[$row->id] = $row->name;
        }
    }
    echo form_dropdown('type', $typearray);
    echo "</p><p>Song Year<br />";    

    $agearray = array();
    $this->db->select('id');
    $this->db->select('name');
    $this->db->from('age');
    $query = $this->db->get();

    if ($query->num_rows()>0)
    {
        foreach($query->result() as $row)
        {
            $agearray[$row->id] = $row->name;
        }
    }
    echo form_dropdown('type', $agearray);



    echo "</p><p>";
    echo form_submit('mysubmit', 'Add Video!');
    echo "</p>";    
    echo form_close();
?&gt;

Which to me is not really using the MVC as there are DB requests etc in the view.

Could someone please explain to me the proper conventions for form structure, where do I set it up? Where do I post the inputs to? Where does the validation go?

Any help would be much appreciated. I probably could hack it out I just want to do things right since I'm starting!

Thanks
Gary
#2

[eluser]bretticus[/eluser]
To be brief, but to the point: Your database calls should be called from your controller and not in your view. You can easily pass the results (or objects to let you iterate the results) via the second parameter in...
Code:
$this->load->view('your_view', $thisone);



Also, if your entire view is PHP code. That is, if you are rendering all the HTML via PHP, you're kinda missing the point. ;-)
#3

[eluser]garycocs[/eluser]
Nice one for that, changing it around now at the moment. Putting all DB queries into the controller.

What about the input form? Where does this go?? (Sorry for the probably stupid question)
#4

[eluser]bretticus[/eluser]
There are no stupid questions (sorry, not trying to come off snarky.)

Forms are HTML structures. Since HTML is your layout (what the user sees) forms belong in the view.

The point being...you are trying to separate your business logic from your layout.
#5

[eluser]garycocs[/eluser]
Starting to get it now slowly, the blog demo is a great help.

What do I do to shrink down the code here, this is an example of my controller for the site, you can see a fair bit of duplication in the code etc? What is the best way of getting rid of this?

Code:
&lt;?php

class Home extends Controller {

    var $base;
    var $css;
    
    function Home()
    {
        parent::Controller();    
        $this->load->scaffolding('videos');
        $this->load->helper('form');
        $this->load->helper('url');        
    }
    
    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');    
        
        $this->db->select('type.name, videos.type, Count(videos.type) AS totaltype');
        $this->db->join('type', 'type.id = videos.type');
        $this->db->group_by('videos.type');
        $data['queryheaderlist'] = $this->db->get('videos');
        
        $this->db->select('type.name As typename, age.name As agename, videos.name, videos.id, videos.link, videos.artist, videos.type, videos.age');
        $this->db->limit(10);
        $this->db->order_by("id", "desc");
        $this->db->join('type', 'type.id = videos.type');
        $this->db->join('age', 'age.id = videos.age');
        $data['videolist'] = $this->db->get('videos');
        
        $typearray=array();
        $this->db->select('id, name');
        $query = $this->db->get('type');
        foreach ($query->result() as $row) $typearray[$row->id] = $row->name;
        $data['typearray'] = $typearray;

        $agearray=array();
        $this->db->select('id, name');
        $query = $this->db->get('age');
        foreach ($query->result() as $row) $agearray[$row->id] = $row->name;
        $data['agearray'] = $agearray;
        
        $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');    
        
        $this->db->select('type.name, videos.type, Count(videos.type) AS totaltype');
        $this->db->join('type', 'type.id = videos.type');
        $this->db->group_by('videos.type');
        $data['queryheaderlist'] = $this->db->get('videos');
        
        $this->db->select('type.name As typename, age.name As agename, videos.name, videos.id, videos.link, videos.artist, videos.type, videos.age');
        $this->db->join('type', 'type.id = videos.type');
        $this->db->join('age', 'age.id = videos.age');
        $data['videolist'] = $this->db->get('videos');
        
        $typearray=array();
        $this->db->select('id, name');
        $query = $this->db->get('type');
        foreach ($query->result() as $row) $typearray[$row->id] = $row->name;
        $data['typearray'] = $typearray;

        $agearray=array();
        $this->db->select('id, name');
        $query = $this->db->get('age');
        foreach ($query->result() as $row) $agearray[$row->id] = $row->name;
        $data['agearray'] = $agearray;
        
        $this->load->view('type_view',$data);
    }
    
    
    function addvideo()
    {
        $this->db->insert('videos', $_POST);
        $id = $this->db->insert_id();
        redirect('home/video/'.$id);
    }    
}
#6

[eluser]bretticus[/eluser]
You should turn your videolist and queryheaderlist into models. You can call them from your controller. That way you are not duplicating code. In the blog video, the author elected to not show an example of model usage but the website docs show several examples.
#7

[eluser]garycocs[/eluser]
Ah right, so all repetitive DB info should become models??

What about inputting data from forms? Should this be model based or controller based?

I've been reading Rapid Dev for CodeIgniter and he actually puts the whole MainPage display into a model???????

Thanks again for your help and advice. I think I'm starting to get the hang of it now.
#8

[eluser]bretticus[/eluser]
I've seen several examples where they just send the whole $_POST array as an argument to the model/db method. However, it's not always the case tat your form completely represents the database. I still like to write a little extra code and have parameters for my models. Any yes, models are a wonderful method for avoiding DNRY when it comes to doing data tier transactions. Of course, everything has to go through the controller at some point. Even my first example has to be called from the controller in some fashion. Making display elements in a model is completely nullifying the entire purpose of MVC. Not so sure about that book. Smile

I see someone corroborates my suspicions.Big Grin
#9

[eluser]garycocs[/eluser]
Cool, that kinda makes things clearer. Thank god I don't have to go putting displays into the model.

Just working on validating my form now, I get that ok it's just returning errors is the issue now.

The example is as follows

Code:
function index()
{
    $this->load->helper(array('form', 'url'));
    
    $this->load->library('validation');
        
    $rules['username']    = "required";
    $rules['password']    = "required";
    $rules['passconf']    = "required";
    $rules['email']        = "required";
    
    $this->validation->set_rules($rules);
    
    $fields['username']    = 'Username';
    $fields['password']    = 'Password';
    $fields['passconf']    = 'Password Confirmation';
    $fields['email']    = 'Email Address';

    $this->validation->set_fields($fields);
        
    if ($this->validation->run() == FALSE)
    {
        $this->load->view('myform');
    }
    else
    {
        $this->load->view('formsuccess');
    }
}

Which is fine when the page I'm returning is a new page but what if the form is only a small part of the page, i.e. if I want to return to the home page, the form being on the left of it?

It seems like a lot of work to go through the load->view('home') plus running all the functions in the model for the data etc.
#10

[eluser]bretticus[/eluser]
Why do you have the form handler as the index method/function anyways? Doesn't it make more sense to put it in login() or some other function? All you have to do is change the action in the html form (or the one parameter in form_open() if you use that) to "controller/login." If you use sessions to keep authentication state (which is the norm) you can simply pass a boolean variable to your home page view to determine whether to show your login form or not.




Theme © iAndrew 2016 - Forum software by © MyBB