これは私のコントローラーです。ここで関数add_post()
は、データの追加と更新の両方で使用される関数です。問題は、フォームを編集したい場合、データベースからそれぞれの領域のフォームに値を取得できることですが、同じ関数を使用してデータを追加したい場合はできません。私はたくさん検索しましたが、解決策を見つけることができませんでした。
<?php
class Blog_controller extends CI_Controller {
public function index() {
$this->get_post();
}
public function __construct() {
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
$this->load->model('blog_model');
}
function add_post($id) {
if($id==NULL) {
return $this->load->view('admin/add_post');
} else {
$data['query']=$this->blog_model->get_single_post($id);
$this->load->view('admin/add_post',$data);
}
}
function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('premium/main', $error);
} else {
$this->upload->do_upload();
$data=$this->upload->data();
$filename=$data['file_name'];
$this->blog_model->insert_post($filename);
}
}
function get_post() {
$data['query']=$this->blog_model->get_post();
$this->load->view('premium/main',$data);
}
function view_table() {
$data['query']=$this->blog_model->get_post(); $this->load->view('admin/view_table',$data);
}
}
?>
MODEL
これは私のプロジェクトのモデルです。単一の関数、つまり insert_post() を使用してデータを挿入および更新したいと考えています。
<?php
Class Blog_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
function insert_post($filename)
{
$data=array(
'title'=>$this->input->post('title'),
'description'=>$this->input->post('description'),
'publish'=>$this->input->post('publish'),
'image'=>$filename,
);
$this->db->insert('post',$data);
redirect('blog_controller');
}
function get_post()
{
$query = $this->db->get_where('post');
return $query->result();
}
function get_single_post($id)
{
$query = $this->db->get_where('post',array('id'=>$id));
return $query->row($id);
}
}
?>
これが私の見解です。このフォームでは、ユーザーがデータを挿入したいときにデータを追加し、ユーザーが編集したいときにデータをフォームのそれぞれのフィールドで取得して更新する必要があります...
<html>
<body>
<h1>Add Blog Title</h1>
<?php function add_post($id);?>
<?php echo form_open_multipart('blog_controller/do_upload');?>
<p>Title : <input type="text" name="title" value="<?php if($id==0){echo $query->title;}?>"></br></p>
<p>Description : <textarea name="description" rows="5"> <?php echo $query->title;?></textarea></br/></p>
<p>Current Image : <img src="<?php echo base_url();?>/uploads/<?php echo $query->image;?>"</b></p>
<p>Image : <?php echo form_upload('userfile');?></b></p>
<p>Publish ?: <input type="checkbox" name="publish" value="1" <?php if($query->publish == 1) echo 'checked="checked"';?>></br></p>
<input type="submit" value="submit">
</form>
</body>
</html>
これは、それぞれの編集フィールド ID が渡されるテーブルであり、フォームでデータを表示し、どのデータのどのフィールドを編集するかを知ることができます..
<html>
<body>
<table border="2px">
<tr>
<td>ID</td>
<td>Title</td>
<td>Description</td>
<td>image</td>
<td>Action</td>
</tr>
<?php foreach($query as $a):?>
<tr>
<td><?php echo $a->id;?></td>
<td><?php echo $a->title;?></td>
<td><?php echo $a->description;?></td>
<td><img src="<?php echo base_url();?>/uploads/<?php echo $a->image?>" height="100px" width="100px"></td>
<td><?php echo anchor('blog_controller/add_post/'.$a->id,"Edit");?></td>
<td><?php echo anchor('blog_controler/delete_post/'.$a->id,"Delete");?></td>
<?php endforeach;?>
</tr>
</table>
</body>
</html>