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
#2

[eluser]solid9[/eluser]
Why not follow this one.
http://net.tutsplus.com/articles/news/co...ay-5-crud/

good luck.
#3

[eluser]Syllean[/eluser]
You're trying to define 2 methods in your model called getPost() that differ only in the fact that one requires a parameter $id and the other does not accept any parameters. This is called method overloading, and it's not possible in PHP.

You have a couple of options:

1) Change the name of one of your functions, maybe change the first getPost() to getPosts(). If you change the name in your model make sure to change it in your controller too.
2) Combine your functions into one using a default value for $id, in something like:

Code:
function getPost($id = null)
{
    if(!$id)
    {
        $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;
    }
    else
    {
        $this->db->select('title', 'content');
        $this->db->from('posts');
        $this->db->where('id', $id);
        $q = $this->db->get();
        $data = array();
  
        if ($q->num_rows() > 0)
        {
            foreach($q->result() as $row)
            {
                $data[] = $row;
            }
        }
        return $data;
    }
}
#4

[eluser]Unknown[/eluser]
@solid9 i already followed that one. i wanted to update dynamically, where i could fetch the data and put it in a textfield and insert into db, so i made this one. but, thanksssss for that one sir.

@Syllean ill try that one sir, thanks so much, btw, ive updated my post, ive managed to fetch the data, but my problem now is it wont store into db after i fetch the data. ;( thanksssss




Theme © iAndrew 2016 - Forum software by © MyBB