php - how to edit and insert data from the single form -
this controller. here function add_post()
function used in both adding , updating data. problem if want edit form can value database form of respective area when want add data using same function cannot. searched lot not find solution.
<?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
this model of project.. want insert , update data using single function i.e. 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); } } ?>
this view. in form want add data when user wants insert data , when user wants edit data should retrived in respective field of form , should updated...
<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>
this table respective editing field id passed can view data in form , can know field of data want edit ..
<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 $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>
you need validate form data sent controller , send these values view: here example:
function add_post() { if ($this->input->post()) { $this->form_validation->set_rules('text', 'text', 'trim|required|min_length[8]'); } if ($this->form_validation->run() == false) { // validation error } else { // success // code save title in db ... // , redirect success page redirect('admin/posts'); ] $this->load->view('admin/add_post', $data); }
so, reload view if it'n nothing sent in text field , min lenght not @ least 8 characters.
if it's ok, code in part exectued (you need own save db function there, haven't add there) , redirected admin/posts.
and in form instead of:
<input type="text" name="text" value=""/>
you need dome thing such as:
<input type="text" name="username" value="<?php echo set_value('text'); ?>"/>
Comments
Post a Comment