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
Related
my controller look like
public function react(Request $request){
//echo "hi";
//return;
//return response()->json($request);
$this->validate($request, [
'postid' => 'required',
'react' => 'required'
]);
$reaction = Reaction::firstOrNew([
'post_id'=>$request->postid,
'user_id'=> Auth::id()
]);
$reaction->user_id = Auth::id();
$reaction->type = $request->react;
$reactType = "";
if ($request->react === "l"){$reactType = "liked";}
else if ($request->react === "d"){$reactType = "disliked";}
else if ($request->react === "h"){$reactType = "loved";}
else if ($request->react === "s"){$reactType = "Smiled";}
else{}
$post = Post::find($request->postid);
$postuser = $post->user->name;
if($post->reactions()->save($reaction)){
$data['message'] = Auth::user()->name.' '.$reactType. ' a Post from' . $postuser;
$data['type'] = 'reaction';
**$this->p->trigger('user-'.$post->user_id, 'new-post', $data);**
return response()->json([
'success' => true,
'message' => 'Reacted'
]);
}
else{
return response()->json([
'success' => false,
'message' => 'Error'
]);
}
AND I AM UNABLE TO LINK WITH PUSHER CHANNEL
But I keep getting an error
Undefined property: App\Http\Controllers\MyController::$p
What am I doing wrong? Would highly appreciate any possible help!
property $this->p (p) , You have not set the value
$this->p = Value ;
After setting, you can use the information.
$this->p->trigger('user-'.$post->user_id, 'new-post', $data);
In the code written above, $this->p has an null value
$this->null->trigger('user-'.$post->user_id, 'new-post', $data);
In fact, this is how you use it
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);
}
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);
}
}
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);
Excuse me master, I had an error in my controller, I'm a newbie PHP CI programmer.
Can you help me where is my mistake on this code? The error says : "Call to a member function produk_list() on a non-object"
And here's my code on my controller :
function index()
{
$data = array();
$data['template'] = 'produk/index';
$data['query'] = $this->produk_model->produk_list($this->uri->segment(4),5,true);
$data['page_title'] = 'Produk';
$this->pagination->initialize(paging_admin($this->produk_model->count(true),'admin/produk/index'));
$data['pagination'] = $this->pagination->create_links();
$data['breadcrum'] = array(array("MIS Admin Panel",'admin'),
array('Produk','admin/produk'),
array('List','')
);
$data = array_merge($data,admin_info());
$this->parser->parse('admin/index',$data);
}
And here's my code on my models (produk_model)
function produk_list($limit,$offset,$admin = false)
{
if($admin === false) $this->db->select('id,title,image,content,created_at');
if($admin === false) $this->db->where('publish',1);
$this->db->order_by('id','DESC');
($limit == '')? $this->db->limit($offset,0) : $this->db->limit($offset,$limit);
$query = $this->db->get('produk');
if($admin === false){
$data = array();
foreach($query->result() as $row){
$data[] = array('produkid' => $row->id,
'produktitle' => $row->title,
'produkurltitle' => url_title($row->title,'dash',true),
'produkdate' => human_date_time($row->created_at),
'produkfrontdate' => human_date($row->created_at),
'produkimage' => ($row->image != '')? '<img src="{produk_tpath}'.$row->image.'" alt="'.$row->title.'" title="'.$row->title.'" />' : '',
'produkcontent' => character_limiter(strip_tags($row->content),200),
);
}
return $data;
}else{
return $query->result();
}
}
Thank you master...
Hope you can help me...
You must either load the model in your controller or autoload the model in autoload.php under config before calling the model function
$this->load->model('produk_model');