codeigniter : update function model and controller - php

I don't know what I am doing wrong in this.
If anyone could help!
cause i have tried all solutions...
also undefined variable $data error
/************************/
public function edit_stud($student_id)
{
$this->form_validation->set_rules('full_name', 'full_name', 'required');
$this->form_validation->set_rules('father_name', 'father_name', 'required');
$this->form_validation->set_rules('roll_no', 'roll_no', 'required');
if($this->form_validation->run() === FALSE)
{
$data['student']= $this ->stud_model->update_student($student_id, $data);
$this->load->view('edit_form' , $data);
}
else
{
$full_name = $this->input->post('full_name', true);
$father_name = $this->input->post('father_name', true);
$roll_no = $this->input->post('roll_no', true);
$data = array(
'full_name'=>$full_name ,
'father_name'=>$father_name ,
'roll_no'=>$roll_no,
);
$student_id = $this->stud_model->update_student($student_id,$data);
redirect('/stud/edit_stud/'.student_id);
}
}
//***********************Model in CI***//////////////
public function update_student($student_id,$data)
{
$this->db->set($this->table_name,$student_id, $data);
return $this->db->update_id();
}

$data is never defined in the first part of your IF statement.
In particular this line
$data['student'] = $this->stud_model->update_student($student_id, $data);
is the problem. If the form validation is false, you don't want to update the student. Remove that line, and everything should be fine.
Unless you do want to update student even if the validation fails (This is a bad idea, IMO). Then your controller would look like this
public function edit_stud($student_id) {
$this->form_validation->set_rules('full_name', 'full_name', 'required');
$this->form_validation->set_rules('father_name', 'father_name', 'required');
$this->form_validation->set_rules('roll_no', 'roll_no', 'required');
$full_name = $this->input->post('full_name', true);
$father_name = $this->input->post('father_name', true);
$roll_no = $this->input->post('roll_no', true);
$data = array(
'full_name' => $full_name ,
'father_name' => $father_name ,
'roll_no' => $roll_no,
);
$student_id = $this->stud_model->update_student($student_id, $data);
if($this->form_validation->run() === FALSE)
$this->load->view('edit_form' , $data);
} else {
redirect('/stud/edit_stud/'.student_id);
}
}

Thanks for suggestions i have solved the problem i was having
in my code i did some renditions...posting the correct answer for
anyone else, having same sort of problem.
this is the controller code.
/*****controller code*****/
/***************************edit student ******************************/
public function edit_stud($student_id)
{
$this->form_validation->set_rules('full_name', 'full_name', 'required');
$this->form_validation->set_rules('father_name', 'father_name', 'required');
$this->form_validation->set_rules('roll_no', 'roll_no', 'required');
if($this->form_validation->run() === FALSE)
{
$data['student']= $this ->stud_model->get_student($student_id);
$this->load->view('edit_form' , $data);
}
else
{
$full_name = $this->input->post('full_name', true);
$father_name = $this->input->post('father_name', true);
$roll_no = $this->input->post('roll_no', true);
$data = array(
'full_name'=>$full_name ,
'father_name'=>$father_name ,
'roll_no'=>$roll_no,
);
$student_id = $this->stud_model->update_student($student_id,$data);
redirect('/stud/edit_stud/'.student_id);
}
}
/************************delete student ************************/
public function delete_stud($student_id)
{
$this ->stud_model->delete_student($student_id);
redirect('/stud/');
}
/***********************model code *******/
/************to get student id*********/
public function get_student($student_id)
{
$this->db->select('*');
$this->db->from($this->table_name);
$this->db->where('id',$student_id);
$query = $this->db->get();
if($query->num_rows() >0)
{
$return = $query->row();
}
else
{
$return = 0;
}
$query->free_result();
return $return;
}
/*****************update function***********************/
public function update_student($id, $data)
{
$where = array('id' => $id);
$this->db->update($this->table_name, $data, $where);
}

Related

Codeigniter Call to a member function result() on a non-object

I have this code:
public function getJccLineItem($id,$action)
{
$res = array();
$q = 'SELECT * FROM jcc_line_items jli,ipo_line_item ili ,line_items li
WHERE jli.line_item_id= ili.id and li.id = jli.line_item_id
and ili.dn_number_id in ( Select dn_number from ipo where project_id= '.$id.')';
$res = $this->db->query($q);
echo $this->db->last_query();
$jccLineItemArray = array();
echo $id;
print_r($res->result());
if($action == 'array')
{
foreach ( $res->result() as $key => $value) // The error comes in this line
{
$jccLineItemArray[ $value->id ] = $value->item_description;
}
$res = $jccLineItemArray;
}
else
{
$res = $res->result();
}
return $res;
}
The error is in the foreach loop. I have printed the result and it shows the result in object array but when it goes to foreach loop. It show this error
"Call to a member function result() on a non-object "
But when I set db['default']['db_debug']=true , it shows that the $id is missing from the query whereas when it was false it was showing result in object array and giving error at loop. Any Help would be appreciated.Thanks
Controller Code
public function createInvoice( $id = "" )
{
if (empty($id))
{
$id = $this->input->post('dataid');
}
echo $id;
$data['jcc_line_list'] = $this->product_model->getJccLineItem($id,'array');
$data['jcc_line_lists'] = $this->product_model->getJccLineItem($id,'');
$data['items'] = $this->product_model->getAllSubInvoice($id);
$data['single_project'] = $this->product_model->getSingleProject($id);
$data['site'] = $this->product_model->getAllSiteArray();
$data['job_types'] = $this->product_model->getAllJobTypeArray();
$data['title'] = 'Invoice';
$data['operation'] = 'Create';
$data['buttonText'] = 'Save';
$data['id'] = $id;
$this->load->helper(array('form', 'url'));
$this->load->helper('security');
$this->form_validation->set_rules('line_item_id', 'Line Item', 'required|xss_clean|max_length[50]');
$this->form_validation->set_rules('job_type_id', 'Job Type', 'required|xss_clean|max_length[50]');
$this->form_validation->set_rules('site_id', 'Site', 'required|xss_clean|max_length[50]');
$this->form_validation->set_rules('milestone', 'Milestone', 'required|xss_clean|max_length[50]');
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('error_message', validation_errors());
$this->load->view('admin/viewinvoicesub', $data);
} else if ($this->form_validation->run() == TRUE) {
$formData = array(
'invoice_id' => $id,
'line_item_id' => $this->form_validation->set_value('line_item_id'),
'job_type_id' => $this->form_validation->set_value('job_type_id'),
'site_id' => $this->form_validation->set_value('site_id'),
'milestone' => $this->form_validation->set_value('milestone'),
);
$this->product_model->insertInvoiceSub($formData);
$this->session->set_flashdata('sucess_message', "Data successfully save !");
redirect('Products/createInvoice', "refresh");
} else {
$this->load->view('admin/viewinvoicesub', $data);
}
}
Try this and let me know if that helps
public function getJccLineItem($id = '' ,$action = '')
{
if($id != '')
{
$res = array();
$q = 'SELECT * FROM jcc_line_items jli,ipo_line_item ili ,line_items li
WHERE jli.line_item_id= ili.id and li.id = jli.line_item_id
and ili.dn_number_id in ( Select dn_number from ipo where project_id= '.$id.')';
$res = $this->db->query($q)->result();
$jccLineItemArray = array();
if($action == 'array')
{
foreach($res as $key => $value) // The error comes in this line
{
$jccLineItemArray[ $value->id ] = $value->item_description;
}
$res = $jccLineItemArray;
}
return $res;
}
else
{
echo "id is null"; die();
}
}
And your Controller code should be
public function createInvoice( $id = "" )
{
$this->load->helper(array('form', 'url'));
$this->load->helper('security');
if ($id = "")
{
$id = (isset($this->input->post('dataid')))?$this->input->post('dataid'):3;// i am sure your error is from here
}
$data['jcc_line_list'] = $this->product_model->getJccLineItem($id,'array');
$data['jcc_line_lists'] = $this->product_model->getJccLineItem($id,'');
$data['items'] = $this->product_model->getAllSubInvoice($id);
$data['single_project'] = $this->product_model->getSingleProject($id);
$data['site'] = $this->product_model->getAllSiteArray();
$data['job_types'] = $this->product_model->getAllJobTypeArray();
$data['title'] = 'Invoice';
$data['operation'] = 'Create';
$data['buttonText'] = 'Save';
$data['id'] = $id;
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$this->form_validation->set_rules('line_item_id', 'Line Item', 'required|xss_clean|max_length[50]');
$this->form_validation->set_rules('job_type_id', 'Job Type', 'required|xss_clean|max_length[50]');
$this->form_validation->set_rules('site_id', 'Site', 'required|xss_clean|max_length[50]');
$this->form_validation->set_rules('milestone', 'Milestone', 'required|xss_clean|max_length[50]');
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
if ($this->form_validation->run() === FALSE)
{
$this->session->set_flashdata('error_message', validation_errors());
$this->load->view('admin/viewinvoicesub', $data);
}
else
{
$formData = array(
'invoice_id' => $id,
'line_item_id' => $this->form_validation->set_value('line_item_id'),
'job_type_id' => $this->form_validation->set_value('job_type_id'),
'site_id' => $this->form_validation->set_value('site_id'),
'milestone' => $this->form_validation->set_value('milestone'),
);
$this->product_model->insertInvoiceSub($formData);
$this->session->set_flashdata('sucess_message', "Data successfully save !");
redirect('Products/createInvoice/'.$id, "refresh");
}
}
else
{
$this->load->view('admin/viewinvoicesub', $data);
}
}

Using multiple controllers cause session error codeigniter

When I use only 1 controller in my application, user, everything works fine. When I try to use 2 or 3 to divide the code and create some structure my application always give the following error :
A PHP Error was encountered
Severity: Warning
Message: session_start(): Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/BT_dashboard/application/controllers/Project.php:99)
Filename: Session/Session.php
Line Number: 140
Backtrace:
File: /Applications/MAMP/htdocs/BT_dashboard/application/controllers/Project.php
Line: 11
Function: __construct
File: /Applications/MAMP/htdocs/BT_dashboard/index.php
Line: 292
Function: require_once
Googled it and tried many things but can't find an answer. I'm using codeigniter 3. My User controller looks like :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* User class.
*
* #extends CI_Controller
*/
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url'));
$this->load->model('user_model');
$this->load->model('employee_model');
$this->load->model('customer_model');
$this->load->model('project_model');
}
public function createProject() {
if ($_SESSION['userlevel']) {
if ($_SESSION['userlevel'] < 3) {
$userid = $this->uri->segment(3);
} else {
$userid = $this->input->post('userproject');
}
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('project_name', 'project name', 'trim|required|min_length[2]|callback_is_project_name_unique[' . $this->input->post('project_name') . ']');
$this->form_validation->set_rules('project_address', 'project address', 'trim|required|min_length[2]');
$this->form_validation->set_rules('project_description', 'project description', 'trim|required|min_length[2]');
$this->form_validation->set_rules('project_city', 'project city', 'trim|required|min_length[2]');
if ($this->form_validation->run() == FALSE) {
$data['userdata'] = $this->session->userdata;
$this->load->view('header', $data);
if ($_SESSION['userlevel'] < 3) {
$this->load->view('dashboard_add_project', $data);
} else {
$data['userslist'] = $this->user_model->get_users_list();
$this->load->view('dashboard_add_project_admin', $data);
}
$this->load->view('wrapper', $data);
} else {
$Address = urlencode($this->input->post('project_address'));
$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=" . $Address . "&sensor=true";
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->status;
if ($status == "OK") {
$Lat = $xml->result->geometry->location->lat;
$Lon = $xml->result->geometry->location->lng;
$LatLng = "$Lat,$Lon";
}
//pass validation
$data = array(
'project_name' => $this->input->post('project_name'),
'project_address' => $this->input->post('project_address'),
'project_description' => $this->input->post('project_description'),
'project_city' => $this->input->post('project_city'),
'project_finished' => $this->input->post('project_finished'),
'lat' => $Lat,
'lng' => $Lon,
);
//$this->db->insert('tbl_user', $data);
if ($this->user_model->create_project($data, $userid, $this->input->post('project_name'))) {
if ($_SESSION['userlevel'] > 1) {
$data['projectlist'] = $this->user_model->get_project_list();
$data['uncompleted_projects'] = $this->user_model->get_uncompleted_projects();
$data['completed_projects'] = $this->user_model->get_completed_projects();
} else {
$data['projectlist'] = $this->user_model->get_project_list_userid($userid);
}
$data['userdata'] = $this->session->userdata;
$this->load->view('header', $data);
$this->load->view('dashboard_projects', $data);
$this->load->view('wrapper', $data);
} else {
$data->error = 'There was a problem creating your new employee. Please try again.';
$data['userdata'] = $this->session->userdata;
// send error to the view
$this->load->view('header', $data);
$this->load->view('dashboard_add_project', $data);
$this->load->view('wrapper', $data);
}
}
}
}
My second controller, the project controller. This is the controller I want to use know to paste the createproject code so this gives more structure. But when I paste it here and adapt my views it gives the error. here's my project_controller code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
* File Name: employee.php
*/
class Project extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url'));
$this->load->model('user_model');
$this->load->model('employee_model');
$this->load->model('customer_model');
}
public function createProject() {
//same as usercontroller
My User/login code in User controller :
public function login() {
$loggedin = $this->session->userdata('logged_in');
if (!$loggedin) {
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('user_mail', 'user mail', 'required');
$this->form_validation->set_rules('user_password', 'user password', 'required');
if ($this->form_validation->run() == false) {
$data = new stdClass();
$data->error = 'Check your user and password';
$this->load->view('dashboard_login', $data);
} else {
$usermail = $this->input->post('user_mail');
$password = $this->input->post('user_password');
if ($this->user_model->resolve_user_login($usermail, $password)) {
$user_id = $this->user_model->get_user_id_from_mail($usermail);
$user = $this->user_model->get_user($user_id);
$_SESSION['user_id'] = $user_id;
$_SESSION['user_name'] = (string) $user->user_name;
$_SESSION['logged_in'] = (bool) true;
$_SESSION['user_gsm'] = (string) $user->user_gsm;
$_SESSION['user_address'] = (string) $user->user_address;
$_SESSION['user_city'] = (string) $user->user_city;
$_SESSION['userlevel'] = $this->user_model->get_user_level((int) $user->user_id);
$_SESSION['user_mail'] = $usermail;
$data['userdata'] = $this->session->userdata;
if ($_SESSION['userlevel'] == "3") {
$data['employeetotal'] = $this->user_model->get_amount_employees();
$data['customertotal'] = $this->user_model->get_amount_customers();
$data['projectstotal'] = $this->user_model->get_amount_projects();
}
$this->load->view('header', $data);
$this->load->view('dashboard_index', $data);
$this->load->view('wrapper', $data);
} else {
$data = new stdClass();
// login failed
$data->error = 'Wrong username or password.';
// send error to the view
$this->load->view('dashboard_login', $data);
}
}
} else {
$data['userdata'] = $this->session->userdata;
$data['employeetotal'] = $this->user_model->get_amount_employees();
$data['customertotal'] = $this->user_model->get_amount_customers();
$data['projectstotal'] = $this->user_model->get_amount_projects();
$this->load->view('header', $data);
$this->load->view('dashboard_index', $data);
$this->load->view('wrapper', $data);
}
}
In config/autoload I've got following:
$autoload['libraries'] = array('database','session');
The session library should be automatically loaded for you. There's no reason for you to include it in each of your constructor functions.
Remove it from both controllers, and it'll work fine.
If it does not work, check in your config/autoload.php file to make sure it is being loaded there correctly.
This is a common problem , notice that the error is on line 99 on your controller Project.php (you should have only 76 lines if I am correct). I bet your controller doesn't have 99 lines ... delete the extra lines and the error will vanish.
or if you have an echo on line 99 comment it
hope that helps!

i couldn't insert data to database in codeigniter

i couldn't insert data to database and no error display. i try var_dump($this->mberita->get_berita()); but array(0){}. I am a newbie in Codeigniter and couldn't really figure out how to solve this.
modal
function get_berita()
{
$this->db->order_by('id_berita','asc');
$data = $this->db->get('berita_ukm');
return $data->result();
}
//untuk menambah berita
function insert_berita($data)
{
$this->db->insert('berita_ukm', $data);
}
controller
function index()
{
$this->data['berita'] = $this->mberita->get_berita();
$this->data['title'] ='UKM Taekwondo | berita';
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('admin/berita/view_berita', $this->data, true);
$this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}
function tambah_berita()
{
$this->form_validation->set_rules('id_berita', 'Id Berita', 'required|numeric');
$this->form_validation->set_rules('tanggal', 'Tanggal', 'required');
$this->form_validation->set_rules('judul_berita', 'Judul Berita', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin/berita/tambah_berita');
}else{
$this->load->model('mberita');
$data = array(
'id_berita' => $this->input->post('id_berita'),
'tanggal' => $this->input->post('tanggal'),
'judul_berita' => $this->input->post('judul_berita'),
'content' => $this->input->post('content')
);
$this->mberita->insert_berita($data);
}
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('admin/berita/tambah_berita', '', true);
$this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}
Please help me what to do. Thank you.
Seems you may be missing the data you want to insert:
$this->mberita->insert_berita($data);
Your data is in the array data.But you don't pass it to the model.
So rewrite the code in controller as
$this->mberita->insert_berita($data);

Codeigniter: Validate form field on edit

I am having trouble and since more than a week trying to find a solution to disallow duplicate form content if it is already exists in database.
So it will check all rows excluding the id (row) what currently I am editing and if same value exists it should give error message.
Here is my Code.
Position Controller
public function position_edit($id = NULL)
{
$this->data['title'] = '<i class="fa fa-user"></i> ' . lang('position_edit');
$this->data['position'] = $this->positions_model->get($id);
count($this->data['position']) || $this->data['errors'][] = 'position could not be found';
$id = $this->uri->segment(4);
$this->db->where('position', $this->input->post('position'));
!$id || $this->db->where('id !=', $id);
$pos = $this->positions_model->get();
echo '<pre>', print_r($pos), '</pre>';
if (count($pos) > 0) {
$this->form_validation->set_rules('position', 'lang:position_code', 'trim|required|max_length[10]|is_unique[positions.position]|xss_clean');
$this->form_validation->set_message('is_unique', lang('error_position_exists'));
}
if ($this->form_validation->run() === TRUE) {
$data = $this->positions_model->array_from_post(array('position', 'label'));
$this->positions_model->save($data, $id);
$this->session->set_flashdata('message', lang('position_record_updated'));
$this->data['message'] = $this->session->flashdata('message');
$this->session->set_flashdata('message_type', 'success');
$this->data['message_type'] = $this->session->flashdata('message_type');
//redirect('admin/hr/positions', 'refresh');
}
// Load the view
$this->load->view('hr/positions/edit', $this->data);
}
Position Model
class Positions_Model extends MY_Model
{
protected $_table_name = 'positions';
protected $_order_by = 'label ASC';
// This $rules currently not in use since it has been
// set directly to the controller edit method code
public $rules = array(
'position' => array(
'field' => 'position',
'label' => 'Position Code',
'rules' => 'trim|required|max_length[10]|xss_clean'
),
'label' => array(
'field' => 'label',
'label' => 'Position Label',
'rules' => 'trim|required|max_length[50]|xss_clean'
),
);
public function get_new()
{
$position = new stdClass();
$position->position = '';
$position->label = '';
return $position;
}
public function get_positions($id = NULL, $single = FALSE)
{
$this->db->get($this->_table_name);
return parent::get($id, $single);
}
public function get_positions_array($id = NULL, $single = FALSE)
{
$this->db->get($this->_table_name);
$positions = parent::get($id, $single);
$array = array();
foreach($positions as $pos){
$array[] = get_object_vars($pos);
}
return $array;
}
public function delete($id)
{
// Delete a position
parent::delete($id);
}
}
DB Model
class MY_Model extends CI_Model
{
protected $_table_name = '';
protected $_primary_key = 'id';
protected $_primary_filter = 'intval';
protected $_order_by = '';
public $rules = array();
protected $_timestamps = FALSE;
function __construct()
{
parent::__construct();
}
public function array_from_post($fields)
{
$data = array();
foreach ($fields as $field) {
$data[$field] = $this->input->post($field);
}
return $data;
}
public function get($id = NULL, $single = FALSE)
{
if($id != NULL) {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key, $id);
$method = 'row';
} elseif($single == TRUE) {
$method = 'row';
} else {
$method = 'result';
}
if(!count($this->db->ar_orderby)) {
$this->db->Order_by($this->_order_by);
}
return $this->db->get($this->_table_name)->$method();
}
public function get_by($where, $single = FALSE)
{
$this->db->where($where);
return $this->get(NULL, $single);
}
public function save($data, $id = NULL)
{
// Set timestamps
if ($this->_timestamps == TRUE) {
$now = date('Y-m-d H:i:s');
$id || $data['created'] = $now;
$data['modified'] = $now;
}
// Insert
if ($id === NULL) {
!isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
$this->db->set($data);
$this->db->insert($this->_table_name);
$id = $this->db->insert_id();
} else {
// Update
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->set($data);
$this->db->where($this->_primary_key, $id);
$this->db->update($this->_table_name);
}
return $id;
}
public function delete($id)
{
$filter = $this->_primary_filter;
$id = $filter($id);
if (!$id) {
return FALSE;
}
$this->db->where($this->_primary_key, $id);
$this->db->limit(1);
$this->db->delete($this->_table_name);
}
}
I have tried callback function also but it is not working at all and couldn't find what causing the issue.
EDIT:
Please note it The above code is giving message if I am inserting the value which already exists but it is not validating and storing the data if the row is not exists
Updated
if ($this->form_validation->run() === TRUE) {
//print_r($this->positions_model->unique_value('position', $this->uri->segment(4)));
if($this->positions_model->unique_value('position', $this->uri->segment(4))) {
$this->form_validation->set_message('unique_value', lang('error_position_exists'));
} else {
$data = $this->positions_model->array_from_post(array('position', 'label'));
$this->positions_model->save($data, $id);
$this->session->set_flashdata('message', lang('position_record_updated'));
$this->data['message'] = $this->session->flashdata('message');
$this->session->set_flashdata('message_type', 'success');
$this->data['message_type'] = $this->session->flashdata('message_type');
redirect('admin/hr/positions', 'refresh');
}
}
In Controller
public function unique_value($field, $id)
{
$id = $this->uri->segment(4);
$this->db->where($field, $this->input->post($field));
!$id || $this->db->where('id !=', $id);
$position = $this->positions_model->get();
if (count($position)) {
return TRUE;
}
return FALSE;
}
Please Note: I don't know what exactly happens to your code, but I wouldn't check between insert or update into the model, just in the controller
I would solve it using a checkExist Model function, that would check if all the values you want to check exists into the DDBB, and work according to it. I would also do it in the controller instead of the model. First, you validate the fields, and then you check if values exists excluding the edit id:
$values_from_post = $this->positions_model->array_from_post(array('position', 'label'));
// $editId is to avoid the id of the row you were editing
if ($this->form_validation->run() === TRUE ) {
if ( !$this->positions_model->check_duplicate( $values_from_post, $editId ) ) ){
// Insert Value
} else {
// Update Value
}
And in your model, you check the duplicate via a where if the values exists:
public function check_duplicate( $values_from_post, $editId ) {
foreach ( $values_from_post as $key => $value ) {
$this->db->where( $key, $value );
}
$this->db->where('id !=', $editId );
$result = $this->db->get($this->_table_name);
return ( ( $result->num_rows > 0 ) ? true : false );
}
Please, I didn't check the code, but that is the idea, instead of doing it in the model, control it in the controller and then insert or update depending of what happens.
Here is the default validation rule of CodeIgniter for checking the duplicate entries in two columns.
$this->form_validation->set_rules('form_field_name','Title here','required|max_length[255]|unique[table.table_column1,table.table_column2]');

error updating data in codeigniter

i getting error when i want to update data. I am a newbie in Codeigniter and couldn't really figure out how to solve this. error display
Severity: Notice
Message: Undefined variable: tanggal, judul_berita, content
Filename: admin/berita.php
model
function get_berita($limit = 1, $offset = 0)
{
$this->db->order_by('id_berita','asc');
$data = $this->db->get($this->tbl_berita, $limit, $offset);
return $data->result();
}
function update_berita($id_berita,$data, $limit = 1, $offset = 0)
{
$this->db->where('id_berita', $id_berita);
$this->db->update($this->tbl_berita, $data);
$data = $this->db->get_where($this->tbl_berita, array('id_berita' => $id_berita), $limit, $offset);
return $data->result();
}
controller
function edit_berita()
{
$this->form_validation->set_rules('tanggal', 'Tanggal', 'required');
$this->form_validation->set_rules('judul_berita', 'Judul Berita', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
$id_berita = $this->uri->segment(4);
if ($this->form_validation->run() == FALSE)
{
$data['id_berita'] = $id_berita;
$data['tanggal'] = $tanggal; //line error
$data['judul_berita'] = $judul_berita; //line error
$data['content'] = $content; // line error
$this->data['contents'] = $this->load->view('admin/berita/edit_berita', $data, true);
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}else{
$this->load->model('mberita');
$data = array(
'id_berita' => $id_berita,
'tanggal' => $this->input->post('tanggal'),
'judul_berita' => $this->input->post('judul_berita'),
'content' => $this->input->post('content')
);
$this->mberita->update_berita($id_berita,$data);
redirect(site_url('admin/berita'));
}
}
please help me what to do. thank you

Categories