CodeIgniter Forums

Full Version: Issue with Edit/View and functions.. Looking for a little help ;)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]masson[/eluser]
Hi,

I'm create a Snippet function in my application, i have created Add, Delete, View, Views. Controller and a Helper, Model without so much issues.

Howver i'm having hard time creating the Edit functions, there is something that i'm doing wrong. Probably something very idiotic and i'm getting frustrated.

The EDIT View file.
Code:
<div id="site-title">
    <h1>&lt;?php echo $title;?&gt;</h1>
    <div class="right">
        &lt;?= anchor('admin/snippet_add/', '<span>Add a new Snippet</span>', array('title' => 'Add a new Snippet!', 'class' => 'sub-button', 'onclick' => 'this.blur();')); ?&gt;
    </div>
    <br style="clear: both;" />
</div>
<div id="content">
    
    <div class="box round">
    &lt;?php echo $this->session->flashdata('message'); ?&gt;
    &lt;?php echo validation_errors('<div class="clean-error">', '</div>'); ?&gt;
    &lt;!-- Admin Content Starts --&gt;    
    
    &lt;form action="&lt;?php echo site_url('admin/snippet_save/' . $snippet-&gt;id); ?&gt;" method="post" class="subform" enctype="multipart/form-data">
    <ul>
    <li>
    <label>Title
    <span class="req">*</span>
    </label>
    &lt;input type="text" name="snippet_title" class="text large" value="&lt;?php echo $snippet-&gt;snippet_title; ?&gt;" /><br/><br/>
    </li>

    <li>
    <label>Short body (Max: 255 characters)</label>
    &lt;textarea name="body" id="body" class="large textarea h100"&gt;&lt;?php echo $snippet->body; ?&gt;&lt;/textarea&gt;&lt;br/>
    <br/>
    <p class="yellow">You have <span class="charsLeft"></span> chars left.</p>
    [removed]
        $('#body').limit('255','.charsLeft');
    [removed]
    </li>
    
    <br style="clear: both;" />
    <br />
    <p><button type="submit" class="button"><span>Update Entry</span></button> or <a href="[removed]history.go(-1)">Go Back</a></p>
</ul>
&lt;/form&gt;
    
    &lt;!-- Admin Content Ends --&gt;
    </div>

</div>

Looking forward to your feedback.

El Forum

[eluser]masson[/eluser]
The Controller (Edit is: "function snippet")
Code:
function snippets()
    {  
    
        $this->nav = 'Snippets';
        
        $data['title'] = "Snippets";
        $data['configs'] = $this->g->get_config();
        $data['query'] = $this->db->get('snippets');
        
        $data['flash_message'] = $this->session->flashdata('message');
        
        $this->template->load('admin/template', 'admin/snippets', $data);
    }
    
    function snippet($id)
    {  
    
        $this->nav = 'Snippets';
        
        $data['title'] = "Edit Snippet";
        $data['configs'] = $this->g->get_config();
        
        $data['query'] = $this->db->get('snippets',$id);
        
        $this->load->library('form_validation');
        
        $data['snippet'] = $this->Snippet_model->current_snippet($id);
        
        // validation rules
        $this->form_validation->set_rules('snippet_title', 'Snippet Title', 'required');
        $this->form_validation->set_rules('body', 'Body', 'max_length[255]');
        
        if ($this->form_validation->run())
        {
            $snippets = array(
                    'snippet_title'=>$this->input->post('snippet_title'),
                    'body'=>$this->input->post('body'),
                    );
            $this->Snippet_model->snippet_save($id);

            $this->session->set_flashdata('message', '<div class="clean-ok"><strong>Success!</strong> You have added new Snippet.</div>');            
            redirect('admin/snippets');
        }
        else
        {
            $this->template->load('admin/template', 'admin/snippet', $data);
        }
    }
    
    function snippet_add($id = FALSE)
    {  
    
        $this->nav = 'Snippets';
        
        $data['title'] = "Add a new Snippet";
        $data['configs'] = $this->g->get_config();
        
        if($id == NULL){ $id = $this->uri->segment(3); }
        
        $data['query'] = $this->db->get('snippets');
        
        $this->load->library('form_validation');
        
        // validation rules
        $this->form_validation->set_rules('snippet_title', 'Snippet Title', 'required');
        $this->form_validation->set_rules('body', 'Body', 'max_length[255]');
        
        if ($this->form_validation->run())
        {
            $snippets = array(
                    'snippet_title'=>$this->input->post('snippet_title'),
                    'body'=>$this->input->post('body'),
                    );
            $this->Snippet_model->snippet_add($snippets);

            $this->session->set_flashdata('message', '<div class="clean-ok"><strong>Success!</strong> You have added new Snippet.</div>');            
            redirect('admin/snippets');
        }
        else
        {
            $this->template->load('admin/template', 'admin/snippet_add', $data);
        }
    }
    
    
    
    
    public function snippet_delete($id = FALSE)
        {
            $query = $this->Snippet_model->snippet_get($id);
    
            if ($this->Snippet_model->snippet_delete($id)) {
                $this->session->set_flashdata('message', "<div class=\"clean-error\"><strong>Done.</strong> You have deleted Snippet entry $query->snippet_title..</div>");                        
            } else {
                $this->session->set_flashdata('message', "<div class=\"clean-error\"><strong>No data found.</strong> There was an error. Please contact support team.</div>");
            }
            redirect('admin/snippets');
    
        }
        
    public function snippet_save($id = FALSE)
        {
            $query = $this->Snippet_model->snippet_get($id);
    
            if ($this->Snippet_model->snippet_save($id)) {
                $this->session->set_flashdata('message', "<div class=\"clean-error\"><strong>Done.</strong> You have deleted Snippet entry $query->snippet_title..</div>");                        
            } else {
                $this->session->set_flashdata('message', "<div class=\"clean-error\"><strong>No data found.</strong> There was an error. Please contact support team.</div>");
            }
            redirect('admin/snippets');
    
        }

El Forum

[eluser]masson[/eluser]
The Model
Code:
&lt;?php
class Snippet_model extends Model {

    public function __construct()
    {
        // model constructor
        parent::__construct();
    }
    
    function snippet_add($data)
    {
        $this->db->insert('snippets', $data);
    }
    
    public function current_snippet($id)
    {
        $this->db->select('id, snippet_title, body');
        $this->db->from('snippets');
        $this->db->where('id', $id);
        $res = $this->db->get();
        
        if($res->num_rows() > 0)
            return $res->row();
        else
            return FALSE;
    }
    
    public function snippet_save($id)
    {

        $this->db->where('id', $id);
        $this->db->update('snippets', $id);
    }
    
    
    function snippet_get($id)
    {
        $query = $this->db->get_where('snippets', array('id'=>$id));  
        if ($query->num_rows()==0) {
            return false;
        }
        $result = $query->result();
        return $result[0];
    }
    
    function snippet_delete($id)
    {
        $query = $this->db->get_where('snippets', array('id'=>$id));  
        
        if ($query->num_rows()==0) {
            return false;
        }
        else {
            $this->db->delete('snippets', array('id' => $id));
            return true;
        }    
    }
}
?&gt;

El Forum

[eluser]masson[/eluser]
Anyone?..

El Forum

[eluser]mikelbring[/eluser]
You said your having trouble creating it? What is it doing wrong (haven't looked at the code much)

El Forum

[eluser]mikelbring[/eluser]
I think I found it. When you do:

Code:
$this->Snippet_model->snippet_save($id)

You did not include the update array. So

Code:
$this->Snippet_model->snippet_save($id,$snippits)

and then in your model:

Code:
public function snippet_save($id,$snippit)
    {

        $this->db->where('id', $id);
        $this->db->update('snippets', $snippit);
    }