Welcome Guest, Not a member yet? Register   Sign In
Post Not updating
#1

[eluser]Unknown[/eluser]
I am new in codeigniter. I build a cms with codeigniter for blog. But when i want edit a article post and want to save it, it create new one post not update old post which i edited.

Please help me.

My Article Edit crontroller:



Code:
public function edit ($id = NULL)
     {
      // Fetch a article or set a new one
      if ($id) {
       $this->data['article'] = $this->article_m->get($id);
       count($this->data['article']) || $this->data['errors'][] = 'article could not be found';
      }
      else {
       $this->data['article'] = $this->article_m->get_new();
      }
      
    
      // categories for dropdown
      $this->data['all_categories'] = $this->article_m->join();
      
    
    
      // Set up the form
      $rules = $this->article_m->rules;
      $this->form_validation->set_rules($rules);
      
      // Process the form
      if ($this->form_validation->run() == TRUE) {
       $data = $this->article_m->array_from_post(array(
        'title',
        'extra_title',
        'slug',
        'image',
        'category_id',
        'body',
        'pubdate'
       ));
       $this->article_m->save($data, $id);
       redirect('admin/article');
      }
      
      // Load the view
      $this->data['subview'] = 'admin/article/edit';
      $this->load->view('admin/_layout_main', $this->data);
     }
My Models:

Code:
public function array_from_post($fields){
  $data = array();
  foreach ($fields as $field) {
   $data[$field] = $this->input->post($field);
  }
  return $data;
}

public function get($id = NULL, $single = FALSE){
  
  if ($id != NULL) {
   $filter = $this->_primary_filter;
   $id = $filter($id);
   $this->db->where($this->_primary_key, $id);
   $method = 'row';
  }
  elseif($single == TRUE) {
   $method = 'row';
  }
  else {
   $method = 'result';
  }
  
  if (!count($this->db->ar_orderby)) {
   $this->db->order_by($this->_order_by);
  }
  return $this->db->get($this->_table_name)->$method();
}

public function get_by($where, $single = FALSE){
  $this->db->where($where);
  return $this->get(NULL, $single);
}

public function save($data, $id = NULL){
  
  // Set timestamps
  if ($this->_timestamps == TRUE) {
   $now = date('Y-m-d H:i:s');
   $id || $data['created'] = $now;
   $data['modified'] = $now;
  }
  
  // Insert
  if ($id === NULL) {
   !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;

   $this->db->set($data);
   $this->db->insert($this->_table_name);
   $id = $this->db->insert_id();
  }
  // Update
  else {
   $filter = $this->_primary_filter;
   $id = $filter($id);
   $this->db->set($data);
   $this->db->where($this->_primary_key, $id);
   $this->db->update($this->_table_name);
  }
  
  return $id;
}
public function get_new ()
{
  $article = new stdClass();
  $article->title = '';
  $article->extra_title = '';
  $article->slug = '';
  $article->image = '';
  $article->category_id = '';
  $article->body = '';
  $article->pubdate = date('Y-m-d');
  return $article;
}

public function join()
{
  $this->db->select('name,categories.id as category_id');
  $this->db->from($this->_table_name);
  $this->db->join('categories', 'categories.id = category_id','right');
  $Q = $this->db->get();
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[$row['category_id']] = $row['name'];
       }
    }
    $Q->free_result();  
    return $data;
}

My View:

Code:
<?php echo validation_errors(); ?>
    <?php echo form_open_multipart('admin/article/edit'); ?>
    <table class="table">
     <tr>
      <td>Publication date</td>
      <td>&lt;?php echo form_input('pubdate', set_value('pubdate', $article->pubdate), 'class="datepicker"'); ?&gt;</td>
     </tr>
     <tr>
      <td>Category</td>
      <td>&lt;?php
    
      $js = 'id="category_id" onChange="some function();"';
    
    
       echo form_dropdown('category_id', $all_categories, set_value('category_id', $article->category_id), $js); ?&gt;</td>
    
     </tr>
     <tr>
      <td>Title</td>
      <td>&lt;?php echo form_input('title', set_value('title', $article->title)); ?&gt;</td>
     </tr>
    
     <tr>
      <td>Extra Title (Optional)</td>
      <td>&lt;?php echo form_input('extra_title', set_value('extra_title', $article->extra_title)); ?&gt;</td>
     </tr>
    
     <tr>
      <td>Slug</td>
      <td>&lt;?php echo form_input('slug', set_value('slug', $article->slug)); ?&gt;</td>
     </tr>
    
     <tr>
      <td>Upload Image</td>
      <td>
       <div class="input-append">
       &lt;?php echo form_input('image', set_value('image', $article->image),'id="fieldID"'); ?&gt;
           <a href="http://localhost/cms/filemanager/dialog.php?type=1&field_id=fieldID" class="btn iframe-btn" type="button">Select</a>
       </div>
      </td>
     </tr>
    
     <tr>
      <td>Body</td>
      <td>&lt;?php echo form_textarea('body', set_value('body', $article->body), 'class="tinymce"'); ?&gt;</td>
     </tr>
     <tr>
      <td></td>
      <td>&lt;?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?&gt;</td>
     </tr>
    </table>
    &lt;?php echo form_close();?&gt;

Please Help Me.
#2

[eluser]CroNiX[/eluser]
You're not passing the id in your form, so ID === NULL in your edit() method.
Code:
&lt;?php echo form_open_multipart('admin/article/edit'); ?&gt;
Code:
public function edit ($id = NULL)
#3

[eluser]Unknown[/eluser]
@CroNiX Thanks




Theme © iAndrew 2016 - Forum software by © MyBB