please help me work my edit and delete function.
here's my CONTROLLER:
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();
}
}
and this is my 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>
What is the possible wrong in my code for edit function and what did i forgot to put in my delete function? And how can i upload a picture saving it in a folder and the picture filename will be saved in the database? I hope someone might be able to help me I'm new in codeigniter. Thank you
For crud operation in codeigniter refer below url its very simple and easy to code
For File Upload you need to refer below link
http://codesamplez.com/development/codeigniter-file-upload
http://www.tutorialspoint.com/codeigniter/codeigniter_file_uploading.htm