Welcome Guest, Not a member yet? Register   Sign In
New to CI and MVC - Just have a few questions
#1

[eluser]wwendorf[/eluser]
Hi all,

I'm just getting started with CI. So far, I like the simplicity of the MVC concepts built into CI. I've had experience with some MVC projects in the past, and they weren't a pleasant experience at all. I think CI is going to change my opinion (finally).

That being said, I was wondering if there is some guidelines as to the amount of code that should be in a view. For example.

I was going thru Jeffrey Way's tutorials and I got to tut#5 where he went the CRUD methods. His update method was hard coded, so I decided to add an update link to each record in the list, and use the same form for adding new records to edit an existing record.

Essentially what I am doing is when the user clicks the "update" button, I add the ID value to the existing data array and i'm adding a "get_one_record" function to the model. I'm just trying to figure out how much coding is actually allowed in the view in order to try and use it for double duty.

I know what I'd do, but I don't know if it's the "right" way.

Thanks,
Wade
#2

[eluser]InsiteFX[/eluser]
I would have a seperate method for each.

Makes it easier to maintain later on.

InsiteFX
#3

[eluser]wwendorf[/eluser]
Below is the code for the program I was talking about with my changes in it to allow editing on the same form as is used for creating a record.

Any comments on the methods I used would be most appreciated.

Thank you,
Wade


Code:
<?php

class Site extends Controller {

    function index($inval = "") {
        $data = array();
        
        if ($query = $this->site_model->get_records()) {
            $data['records'] = $query;
        }
        
        $this->load->view('options_view',$data);
    }
    
    function create() {
    
        if ($this->input->post('pid')) {
        
            $data = array(
                'id'         => $this->input->post('pid'),
                'title'     => $this->input->post('title'),
                'contents'     => $this->input->post('contents')
            );
            
            $this->site_model->update_record($data);
            $this->index();
            
        } else {
    
            $data = array(
                'title' => $this->input->post('title'),
                'contents' => $this->input->post('contents')
            );
            
            $this->site_model->add_record($data);
            
            $this->index();
        }
    }

    function delete() {
        $this->site_model->delete_row();
        $this->index();
    }
    
    function update() {
        $id_to_update = $this->uri->segment(3);
        
        //echo $id_to_update;
        
        $return_data = $this->site_model->get_one_record($id_to_update);
        
        /*echo "<pre>";
        print_r($return_data);
        echo "</pre>";*/
        
        $data['records'] = $this->site_model->get_records();
        
        $data['update'] = $return_data[0];

        /*echo "<hr><pre>";
        print_r($data);
        echo "</pre>";*/
        
        $this->load->view('options_view', $data);
    }
}

?&gt;

Code:
&lt;?php

class Site_model extends Model {

    function get_records() {
        $this->db->order_by("id", "desc");
        $query = $this->db->get("data");
        return $query->result();
    }
    
    function add_record($data) {
        $this->db->insert('data',$data);
        return;
    }

    function update_record($data) {
        
        /*
        echo "<pre>";
        print_r($data);
        echo "</pre>";
        */
        
        $this->db->where('id',$data['id']);
        $this->db->update('data', $data);
    }
    
    function delete_row() {
        $this->db->where('id', $this->uri->segment(3));
        $this->db->delete('data');
    }
    
    function get_one_record($data) {
        
        $this->db->where('id',$data);
        $query = $this->db->get("data");
        
        return $query->result();
    }
}

?&gt;

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled&lt;/title&gt;

&lt;link rel="stylesheet" href="&lt;?php echo base_url();?&gt;css/style.css" type="text/css" media="screen" charset="utf-8"&gt;

&lt;/head&gt;

&lt;?php
//echo "<pre>";print_r($update->title);echo "</pre>";
?&gt;

&lt;body&gt;

    <div id="enter_data">
    &lt;?php echo anchor("site/","<h2>Create</h2>");?&gt;
    &lt;?php echo form_open('site/create');?&gt;

    <p>
        <label for="title">Title:</label>
        &lt;input type="text" name="title" id="title" value="&lt;?php if (isset($update-&gt;title)) {echo $update->title;} ?&gt;" />
    </p>
    
    <p>
        <label for="content">Contents:</label>
        &lt;textarea name="contents" id="contents" rows=10&gt;&lt;?php if (isset($update->contents)) {echo $update->contents;} ?&gt;&lt;/textarea&gt;
    </p>
    
    <p>
        &lt;input type=submit name="submit" value="Submit"/&gt;
    </p>
    &lt;?php if (isset($update->id)) { ?&gt;
    &lt;input type=hidden name="pid" id="pid" value='&lt;?php echo $update-&gt;id; ?&gt;'>
    &lt;?php } ?&gt;
    
    &lt;?php echo form_close();?&gt;
    </div>
    <div id="title">
        <h2>Read</h2>
    </div>
    
    &lt;?php
    if (isset($records)) {
        $i = 1;
        foreach ($records as $row) {
            ?&gt;<div id=display>&lt;?php
            echo "<h2>[Post #$i]" . anchor("site/delete/$row->id", $row->title); $row->title . "</h2>";
            echo "<div id='contents'>" . $row->contents . "<br />";
            echo anchor("site/update/$row->id","[update]") . "</div>";
            echo "</div>";
            $i++;
        }
    } else {
        echo "<h2>No Records Returned</h2>";
    }
    ?&gt;
    
    <hr />
    
    <div id="title">
        <h2>Delete</h2>
    </div>
    
    <div id="display">
        <p>To sample the delete method, simply click on one of the headings listed above.</p>
    </div>
    

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




Theme © iAndrew 2016 - Forum software by © MyBB