0

CI 2.1での更新に問題があります。ユーザーガイド「mini-tut」に従ってeニュースを作成しましたが、フォームを使用してレコードを更新する方法がわかりません。

私の更新モデルは次のとおりです。

    // update dei record
public function update_news($id)
{
    $data = array(
        'title' => $this->input->post('title'),
        'slug' => $this->input->post('slug'),
        'text' => $this->input->post('text')
        );

    $this->db->where('id', $id);
    $this->db->update('news', $data); 
}

コントローラーを更新するにはどうすればよいですか?私は試してみます:

 public function update($id)
 {
  $this->load->helper('form');
  $this->load->library('form_validation');

  $data['title'] = 'Update an intem';

  $this->form_validation->set_rules('title', 'Title', 'required');
  $this->form_validation->set_rules('text', 'text', 'required');

  if ($this->form_validation->run() === FALSE)
  {
   $this->load->view('templates/header', $data); 
   $this->load->view('news/update');
   $this->load->view('templates/footer');
  }
 else
  {
   $this->news_model->update_news($id);
   $this->load->view('news/success');
  }
 }

しかし、私は404()ページを表示します。

更新のビューは次のとおりです。

    <h2>Update an item</h2>

<?php echo validation_errors(); ?>

<?php echo form_open('news/update') ?>

 <label for="title">Title</label> 
 <input type="input" name="title" /><br />

 <label for="slug">Slug</label> 
 <input type="input" name="slug" /><br />

 <label for="text">Text</label>
 <textarea name="text"></textarea><br />

 <input type="submit" name="submit" value="Update an item" /> 

</form>  

CIロジックを理解するための「単純な」更新方法を教えてくれる人はいますか?

4

3 に答える 3

1

あなたが何を望んでいるのか理解できたら、コントローラーからモデルに $this->input->post(x) を渡すだけです。個人的には以下のように使っています。

コントローラ:

$data = array(
   'title' => $this->input->post('title'),
   'text' => $this->input->post('text'),
   'slug' => $this->input->post('slug'),
);
if($this->my_model->exists($id)) {
    $this->my_model->update($id, $data);
} else {
    $this->my_model->insert($data);
}

モデルは次のようになります。

// update dei record
public function update($id, $data)
{
    $this->db->where('id', $id);
    $this->db->update('news', $data); 
}

コントローラーは、最初のセグメントをコントローラー メソッドの引数として使用します。

public function update($id)

使ってみることもできます

$id = $this->uri->segment(3);
于 2012-04-16T08:35:43.733 に答える
0

//Controller public function update($id) { $this->load->helper('form'); $this->load->library('form_validation');

    $data['title'] = 'Update an intem';

    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('text', 'text', 'required');
    $data['news'] = $this->news_model->get_news_by_id($id);


    if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('templates/header', $data); 
        $this->load->view('news/update', $data);
        $this->load->view('templates/footer');
    }
    else
    {
        $this->load->helper('url');
        $this->news_model->update_news($id);
        redirect('/news', 'refresh');
    }
}

//モデル public function update_news($id) {

    $data = array(
        'title' => $this->input->post('title'),
        'slug' => $this->input->post('title'),
        'text' => $this->input->post('text')
    );
    $this->db->where('id', $id);
    $this->db->update('news', $data);
}

意見

<label for="title">Title</label>
<input type="input" name="title" value="<?php echo $news[0]['title'];?>" /><br />
<input type = "hidden" name="id" value="<?php echo $news[0]['id'];?>" />
<label for="text">Text</label>
<textarea name="text"><?php echo $news[0]['text'];?></textarea><br />

<input type="submit" name="submit" value="Create news item" />

于 2015-05-07T04:04:52.763 に答える
-1

コードイグナイターでページを追加するには

私が行った例を挙げます

モデル:

function add()      
{        
   $name=$_POST['name'];        
   $department=$_POST['dept'];      
   $degree=$_POST['degree'];        
   $mark=$_POST['mark'];    

   $this->db->query("INSERT INTO `simple`(name,department,degree,mark) VALUES('$name','$department','$degree','$mark')");       
}

コントローラ:

public function add()   
{   
   $this->load->model('modd');      
   $this->modd->add();      
   $this->load->view('add');    
}

ビュー:

add.php

<form method="post" action="<?php base_url();?>index.php/contr/add">
    <table>
        <?php echo form_open('contr/add');?>
        <tr>
            <th>Name</th>
            <td><input type="text" name="name" /></td>
        </tr>
        <tr>
            <th>Department</th>
            <td><input type="text" name="dept" /></td>
        </tr>
        <tr>
            <th>Degree</th>
            <td><input type="text" name="degree" /></td>
        </tr>
        <tr>
            <th>Marks</th>
            <td><input type="text" name="mark" /></td>
        </tr>
        <tr>
            <td><input type="submit" name="add" value="Add" /></td>
        <?php echo form_close(); ?>
            <td>
                <a href="<?php echo base_url(); ?>index.php/contr/view"><input type="button" value="view" /></a>
            </td>
        </tr>
    </table>
于 2013-02-16T09:05:24.203 に答える