请帮我修改编辑和删除功能。
这是我的控制器:
class Recipe_controller extends CI_Controller{
function __construct(){
parent:: __construct();
$this->load->library('session');
$this->load->helper('url');
$this->load->helper('form');
$this->load->helper('date');
$this->load->helper('html');
$this->load->database('recipedb');
$this->load->library('form_validation');
$this->load->model('Recipe_model');
}
function index(){
$this->data['recipesview'] = $this->Recipe_model->get_recipe();
$this->data['featuredday'] = $this->Recipe_model->get_featuredday();
$this->load->view('recipehome_view', $this->data);
}
function submit_recipe(){
$data['category'] = $this->Recipe_model->get_category();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'png|jpg|gif';
$config['max_size'] = '150';
$config['max_width'] = '1024'; /* max width of the image file */
$config['max_height'] = '768';
$this->form_validation->set_rules('category', 'Category', 'callback_combo_check');
$this->form_validation->set_rules('recipename', 'Recipe Name', 'required');
$this->form_validation->set_rules('ingredient', 'Ingredient', 'required');
$this->form_validation->set_rules('direction', 'Direction', 'require');
$this->form_validation->set_rules('image', 'Image', 'required');
$this->form_validation->set_rules('time', 'Time', 'required');
$this->form_validation->set_rules('date', 'date', 'require');
if ($this->form_validation->run() == FALSE) {
$this->load->view('submitrecipe_view', $data);
}
else{
$data = array(
'category_id' => $this->input->post('category'),
'recipe_name' => $this->input->post('recipename'),
'ingredient' => $this->input->post('ingredient'),
'direction' => $this->input->post('direction'),
'image' => $this->input->post('image'),
'time' => $this->input->post('time'),
'date' => date('Y-m-d', now($this->input->post('date'))));
$upload_data = $this->upload->data();
$this->db->insert('recipe', $data);
$this->session->set_flashdata('msg', 'successful');
redirect('recipe/submit');
}
}
function combo_check($str){
if ($str == '-SELECT-'){
$this->form_validation->set_message('combo_check', 'Valid %s Name is required');
return FALSE;
}else{
return TRUE;
}
}
function edit_recipe($recipeid){
$recipeid = $this->uri->segment(3);
$data['category'] = $this->Recipe_model->get_category();
$data['reciperecord'] = $this->Recipe_model->getById($recipeid);
$this->form_validation->set_rules('category', 'Category', 'callback_combo_check');
$this->form_validation->set_rules('recipename', 'Recipe Name', 'required');
$this->form_validation->set_rules('ingredient', 'Ingredient', 'required');
$this->form_validation->set_rules('direction', 'Direction', 'require');
$this->form_validation->set_rules('time', 'Time', 'required');
$this->form_validation->set_rules('date', 'date', 'require');
if ($this->form_validation->run() == FALSE) {
$this->load->view('editrecipe_view', $data);
}
else{
//{
$data = array(
'category_id' => $this->input->post('category'),
'recipe_name' => $this->input->post('recipename'),
'ingredient' => $this->input->post('ingredient'),
'direction' => $this->input->post('direction'),
'image' => $this->input->post('image'),
'time' => $this->input->post('time'),
'date' => date('Y-m-d', now($this->input->post('date'))));
$this->db->where('recipe_id', $recipeid);
$this->db->update('recipe', $data);
$this->session->set_flashdata('msg', 'successful');
redirect('recipe/edit' . $recipeid, $data);
//}
}
}
function view_appetizer(){
$this->data['appetizer'] = $this->Recipe_model->get_appetizer();
$this->load->view('appetizer_view', $this->data);
}
}
MODEL:
class Recipe_model extends CI_Model{
function __construct(){
parent::__construct();
}
function getById($recipeid){
$query = $this->db->get_where('recipe', array('recipe_id' => $recipeid));
return $query->row_array();
}
function get_category(){
$this->db->select('category_id');
$this->db->select('category_name');
$this->db->from('recipe_category');
$query = $this->db->get();
$result = $query->result();
$cat_id = array ('-Select-');
$cat_name = array ('-Select-');
for($i = 0; $i < count($result); $i++){
array_push($cat_id, $result[$i]->category_id);
array_push($cat_name, $result[$i]->category_name);
}
return $category_result = array_combine($cat_id, $cat_name);
}
function get_recipe(){
$this->db->select('recipe_id, recipe_name, ingredient, direction');
$this->db->from('recipe');
$this->db->order_by('date', 'asc');
$this->db->limit(3);
$query = $this->db->get();
return $query->result();
}
function get_featuredday(){
$this->db->select('recipe_name');
$this->db->from('recipe');
$this->db->order_by('recipe_id', 'RANDOM');
$this->db->limit(1);
$query = $this->db->get();
return $query->result();
}
function get_appetizer(){
$this->db->select('recipe_id, recipe_name, ingredient, direction');
$query = $this->db->get_where('recipe',array('category_id'=>1));
return $query->result();
}
}
这是我的VIEW for edit:
<h1>Edit Recipe Details</h1>
<?php
$attributes = array("class" => "form-horizontal", "id" => "recipeform", "name" => "recipeform", "method" => "post");
echo form_open('recipe/edit/' .$recipeid, $attributes);
?>
<label for="category" class="control-label">Category</label>
<?php
$attributes = 'class = "form-control" id = "category"';
echo form_dropdown('category',$category,set_value('category', $reciperecord['category_id']), $attributes);?>
<span class="text-danger"><?php echo form_error('category'); ?></span>
<br/>
<br/>
<label for="recipename" class="control-label">Recipe Name</label>
<?php
// $attributes = 'class = "form-control" id = "recipename"';
echo form_input(array(
'name' => 'recipename',
'id' => 'recipename',
'value' => 'recipename', $reciperecord['recipe_name'])); ?>
<span class="text-danger"><?php echo form_error('recipename'); ?></span>
<br/>
<br/>
<label for="ingredient" class="control-label">Ingredient</label>
<?php
$attributes = 'class = "form-control" id = "ingredient"';
echo form_textarea(array(
'name' => 'ingredient',
'id' => 'ingredient',
'rows' => '10',
'cols' => '50',
'value' => 'ingredient', $reciperecord['ingredient'])); ?>
<span class="text-danger"><?php echo form_error('ingredient'); ?></span>
<br/>
<br/>
<label for="direction" class="control-label">Direction</label>
<?php
$attributes = 'class = "form-control" id = "direction"';
echo form_textarea(array(
'name' => 'direction',
'id' => 'direction',
'rows' => '10',
'cols' => '50',
'value' => 'direction', $reciperecord['direction'])); ?>
<span class="text-danger"><?php echo form_error('direction'); ?></span>
<br/>
<br/>
<label for="time" class="control-label">Time</label>
<?php
$attributes = 'class = "form-control" id = "time"';
echo form_input(array(
'name' => 'time',
'id' => 'time',
'value' => 'time', $reciperecord['time'])); ?>
<span class="text-danger"><?php echo form_error('time'); ?></span>
<br/>
<br/>
<label for="date" class="control-label">Date</label>
<?php
$attributes = 'class = "form-control" id = "date"';
echo form_input(array(
'name' => 'date',
'id' => 'date',
'value' => 'date', $reciperecord['date'])); ?>
<span class="text-danger"><?php echo form_error('date'); ?></span>
<br/>
<br/>
<input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Insert" />
<input id="btn_cancel" name="btn_cancel" type="reset" class="btn btn-danger" value="Cancel" />
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
<?php include 'footer.php' ;?>
</body>
我的编辑功能代码可能出现什么问题?忘记删除功能的内容是什么?如何上传图片将其保存在文件夹中,图片文件名将保存在数据库中?我希望有人可以帮助我,我是codeigniter的新人。谢谢
答案 0 :(得分:1)
对于codeigniter中的crud操作,请参阅下面的url,它非常简单易于编码
对于文件上传,您需要参考以下链接
http://codesamplez.com/development/codeigniter-file-upload
http://www.tutorialspoint.com/codeigniter/codeigniter_file_uploading.htm