2014-01-29 58 views
0

我是codeigniter中的新成員。我使用codeigniter爲博客構建了一個cms。但是,當我想編輯一篇文章文章,並希望保存它,它創建新的職位不更新我編輯的舊帖子。文章帖子未在codeigniter博客中更新

請幫幫我。

我的文章編輯crontroller:

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); 
    } 

我的模型:

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; 
} 

筆者認爲:

<?php echo validation_errors(); ?> 
<?php echo form_open_multipart('admin/article/edit'); ?> 
<table class="table"> 
    <tr> 
     <td>Publication date</td> 
     <td><?php echo form_input('pubdate', set_value('pubdate', $article->pubdate), 'class="datepicker"'); ?></td> 
    </tr> 
    <tr> 
     <td>Category</td> 
     <td><?php 

     $js = 'id="category_id" onChange="some function();"'; 


     echo form_dropdown('category_id', $all_categories, set_value('category_id', $article->category_id), $js); ?></td> 

    </tr> 
    <tr> 
     <td>Title</td> 
     <td><?php echo form_input('title', set_value('title', $article->title)); ?></td> 
    </tr> 

    <tr> 
     <td>Extra Title (Optional)</td> 
     <td><?php echo form_input('extra_title', set_value('extra_title', $article->extra_title)); ?></td> 
    </tr> 

    <tr> 
     <td>Slug</td> 
     <td><?php echo form_input('slug', set_value('slug', $article->slug)); ?></td> 
    </tr> 

    <tr> 
     <td>Upload Image</td> 
     <td> 
      <div class="input-append"> 
      <?php echo form_input('image', set_value('image', $article->image),'id="fieldID"'); ?> 
       <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><?php echo form_textarea('body', set_value('body', $article->body), 'class="tinymce"'); ?></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td> 
    </tr> 
</table> 
<?php echo form_close();?> 

請幫助我。

回答

1

您必須將ID傳遞給控制器​​。在你看來,你正在告訴表格提交到 文章/編輯,但不是ID。

<?php echo validation_errors(); ?> 
<?php echo form_open_multipart('admin/article/edit/'.$article->id); ?> 
... 

你可能會得到一些警告,如果$文章沒有設置,所以你可能希望把一些檢查,當你加入新的項目的。