Welcome Guest, Not a member yet? Register   Sign In
New to CI and MVC - Just have a few questions
#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;


Messages In This Thread
New to CI and MVC - Just have a few questions - by El Forum - 09-23-2010, 09:40 AM
New to CI and MVC - Just have a few questions - by El Forum - 09-23-2010, 11:59 PM
New to CI and MVC - Just have a few questions - by El Forum - 09-24-2010, 07:22 AM



Theme © iAndrew 2016 - Forum software by © MyBB