Welcome Guest, Not a member yet? Register   Sign In
Basic 'Update Entry' Script help...
#1

[eluser]invision[/eluser]
Hi,

I'm working on a very basic Guestbook and I'm 90% of the way there.

However, I'd like to be able to allow Guests to edit posts. They can even edit others' posts, for now Smile

View:
Code:
<html>
<head>
<title>Update Entry | Guestbook | CodeIgniter</title>

<style type="text/css">

body {
background-color: #fff;
margin: 40px;
font-family: Lucida Grande, Verdana, Sans-serif;
font-size: 12px;
color: #4F5155;
}

</style>
</head>
<body>

<h1>Welcome to the Guestbook!</h1>

<p>Edit an entry on my Guestbook.</p>

&lt;?php

echo validation_errors('<div class="error">', '</div>');

echo '<p>&nbsp;</p>';

echo form_open('guestbook/update/');

echo form_label('What is your Name?', 'author');
echo '<br />';
echo form_input('author', '');

echo '<br /><br />';

echo form_label('Your Comments', 'comments');
echo '<br />';
echo form_textarea('comments', '');

echo '<br /><br />';

echo form_submit('mysubmit', 'Update Entry!');

form_close();

?&gt;

<p>&nbsp;</p>

<p><br />Page rendered in {elapsed_time} seconds</p>

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

Controller:
Code:
function update()
    {
    $this->load->helper(array('form', 'url'));        
        $this->load->library('form_validation');        
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
                    
        /* Form Validation */            
        $this->form_validation->set_rules('author', 'Author', 'trim|required');
        $this->form_validation->set_rules('comments', 'Comments', 'trim|required');  
    
        #Input and textarea field attributes
        $data["author"] = array('name' => 'author', 'id' => 'author');
        $data['comments'] = array('name' => 'comments', 'id' => 'comments');
        
                
        if ($this->form_validation->run() == FALSE)
        {    
          $data['post'] = $this->Guestbook_model->getEntry($id);
          $this->load->vars($data);
          $this->load->view('update');
        }
        else
        {        
        $this->load->model('Guestbook_model');
        $data['cats'] = $this->Guestbook_model->updateEntry();
        $this->load->vars($data);
        $this->load->view('update_success');
        }
    }

Model:
Code:
function updateEntry(){
        $data = array('author' => $this->input->post('author'),
        'content' => $this->input->post('content')
        );
        $this->db->where('id',$this->input->post('id'));
        $this->db->update('data',$data);
    }


I basically want to know how to:

1) pre-fill my input fields with the existing author/comments values for that post
2) update the guestbook entry when submitted correctly.


Many thanks for any help you can give.
#2

[eluser]srpurdy[/eluser]
simply create an extraction db call like below

Model
Code:
function edit_post()
{
$edit_post = $this->db->getwhere('guestbook', array('id' => $this->uri->segment(4)), '', '');
return $edit_post
}

the 4th uri segment should be the post ID number. however use this however you need it. if you use segment 3 than do that. Smile

controller
Code:
$data['edit_post'] = $this->your_model->edit_post();

than do a foreach loop over the html &lt;form&gt;. and echo the results inside the value of the text box or textarea.

for example

Code:
&lt;?php foreach($edit_post->result() as $edit): ?&gt;
&lt;form action="your_form_action" method="post" enctype="multipart/form-data">
&lt;input type="text" class="field" name="somefield" value="&lt;?=$edit-&gt;somefield?&gt;" /><br />
&lt;/form&gt;
&lt;?php endforeach;?&gt;

I noticed your using the ci form feature, but its not different just echo your field into the value="";

For update
Code:
function update_post()
{
    $this->db->where('id', $_POST['id']);
    $this->db->update('guestbook', $this->db->escape($_POST));
}




Theme © iAndrew 2016 - Forum software by © MyBB