Welcome Guest, Not a member yet? Register   Sign In
update dynamically CRUD
#1

[eluser]Unknown[/eluser]
posts_model.php
Code:
<?php
class Posts_model extends CI_Model {

private $posts= 'posts';

    function addPost($title, $content)
{
        $data = array(
            'title' => $title,
            'content' => $content
        );

        $this->db->insert('posts', $data);
    }

function getPosts()
{
    $query = $this->db->get('posts');
    $posts = array();

    foreach ($query->result() as $row) {
        $posts[] = array(
            'id' => $row->id,
            'title' => $row->title,
            'content' => $row->content
        );
    }
    return $posts;
}

function updatePost($id, $title, $content) {
$data = array(
   'title' => $title,
   'content' => $content
  );
    $this->db->where('id', $id);
    $this->db->update('posts', $data);
}

function deletePost($id) {
    $this->db->where('id', $id);
    $this->db->delete('posts');
}

function get_by_id($id){
  $this->db->where('id', $id);
  return $this->db->get($this->posts);
}


function update($id, $post){
  $this->db->where('id', $id);
  $this->db->update($this->posts, $post);
}

}


crud.php
Code:
<?php
class Crud extends CI_Controller {

function __construct()
{
  parent::__construct();
  
  // load library
  $this->load->library(array('table','form_validation'));
  
}

    function index()
{
        // Check if form is submitted
        if ($this->input->post('submit')) {
   $title = $this->input->post('title', TRUE);
   $content = $this->input->post('content', TRUE);
            //$title = $this->input->xss_clean($this->input->post('title'));
            //$content = $this->input->xss_clean($this->input->post('content'));

            // Add the post
            $this->posts_model->addPost($title, $content);
        }

        //$this->load->view('crud_view');
  $data = array();
  $data['posts'] = $this->posts_model->getPosts();
  $this->load->view('crud_view', $data);
    }

/*
function updates() {
    $id = $this->uri->segment(3);

    if ($this->input->post('update'))
{
  $title = $this->input->post('title', TRUE);
  $content = $this->input->post('content', TRUE);
        //$title = $this->input->xss_clean($this->input->post('title'));
        //$content = $this->input->xss_clean($this->input->post('content'));

        $this->posts_model->updatePost($id, $title, $content);
        $data['posts'] = $this->posts_model->getPosts();
        $this->load->view('crud_view', $data);
    } else {
        $data = array('id' => $id);
        $this->load->view('updateform', $data);
    }
}
*/

function delete() {
    $id = $this->uri->segment(3);
    $this->posts_model->deletePost($id);

    $data['posts'] = $this->posts_model->getPosts();
    $this->load->view('crud_view', $data);
}

// set empty default form field values
function _set_fields()
{
  $this->form_data->id = '';
  $this->form_data->name = '';
  $this->form_data->title = '';
}

function update($id)
{
  $data=array();
  $this->_set_fields();
  // prefill form values
  $post = $this->posts_model->get_by_id($id)->row();
  $this->form_data->id = $id;
  $this->form_data->title = $post->title;
  $this->form_data->content = $post->content;
  
  $data['action'] = site_url('crud/updatePost');

  // load view
  $this->load->view('updateform', $data);
}

function updatePost()
{
  $data=array();
  $data['action'] = site_url('crud/updatePost');
  $this->load->library('form_validation');

  // run validation
  if ($this->form_validation->run() === TRUE)
  {
   // save data
   $id = $this->input->post('id');
   $data = array(
     'name' => $this->form_data->title,
     'content' => $this->form_data->content
     );
   $this->posts_model->update($id,$data);
  
   // set user message
   //$data['message'] = '<div class="success">update person success</div>';
  }
  // load view
   $data['posts'] = $this->posts_model->getPosts();
  $this->load->view('crud_view', $data);
}


}

updateform.php
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html;charset=UTF-8"&gt;
&lt;title&gt;&lt;/title>
&lt;/head&gt;
&lt;body&gt;

<div class="content">
<h1>Update Post</h1>
  &lt;form method="post" action="&lt;?php echo $action; ?&gt;"&gt;
  <div class="data">
  <table>
   <tr>
    <h1>&lt;?php echo set_value('title',$this->form_data->id); ?&gt;</h1>
   </tr>
   <tr>
    <td valign="top">Title<span>*</span></td>
    <td>&lt;input type="text" name="title" class="text" value="&lt;?php echo set_value('title', $this-&gt;form_data-&gt;title); ?&gt;"/&gt;
    </td>
   </tr>
   <tr>
    <td valign="top">Content<span>*</span></td>
    <td>&lt;input type="text" name="content" class="text" value="&lt;?php echo set_value('content', $this-&gt;form_data-&gt;content); ?&gt;"/&gt;
    </td>
   </tr>
   <tr>
    <td>&nbsp;</td>
    <td>&lt;input type="submit" id="update" value="Update"/&gt;&lt;/td>
   </tr>
  </table>
  </div>
  &lt;/form&gt;
</div>
&lt;/body&gt;
&lt;/html&gt;



Updated my post, i already manage to fetch the data when updating ang put it in the text field, but my problem now is that it wont insert the updated data into the database,
need help badly? Sad


Messages In This Thread
update dynamically CRUD - by El Forum - 06-25-2012, 04:39 AM
update dynamically CRUD - by El Forum - 06-25-2012, 10:27 AM
update dynamically CRUD - by El Forum - 06-25-2012, 10:30 AM
update dynamically CRUD - by El Forum - 06-25-2012, 11:03 AM



Theme © iAndrew 2016 - Forum software by © MyBB