how store multiple array value in data base? - php

view
error
model:
function add_wliempmap($params)
{
foreach ($params as$row)
{
$this->db->insert('tbl_wli_peremp_map',$row);
$query= $this->db->insert_id();
// return$query->result();
}
}
controller:
public function submit()
{
$data['data']=array('wid' => $this->input->post('wid'),'eid' => $this->input->post('eid')
);
$this->Wliempmap_model->add_wliempmap($data);
// var_dump($_REQUEST);
// exit;
}
Above model $row variable have array value I want to store this variable value in database how to pass this variable in controller multiple value with example ?

Change your code as per bellow code:
Model:
public function add_wliempmap($params)
{
$this->db->insert('tbl_wli_peremp_map',$params);
return $this->db->insert_id();
}
Controller:
public function submit()
{
$data = array('wid' => $this->input->post('wid'),'eid' => $this->input->post('eid'));
$this->Wliempmap_model->add_wliempmap($data);
}
For details info:
https://www.codeigniter.com/user_guide/database/query_builder.html#inserting-data

Related

Attempt to Read Property on String Error Codeigniter

I am trying to display data through a query on Codeigniter, but return an error
"ATTEMPT TO READ PROPERTY "JENIS_PRODUK" ON STRING"
Here's my code on controller :
public function minimalist($id = 'minimalist')
{
$data["minimalis"] = $this->welcome_model->getMinimalis($id);
$this->load->view('frontend/minimalis.php', $data);
}
My code on Model :
public function getMinimalis($id)
{
return $this->db->get_where($this->_table, ["jenis_produk" => $id])->row();
}
On view I added foreach to display those data
Can you tell me what's wrong?
Please change this
public function getMinimalis($id)
{
return $this->db->get_where($this->_table, ["jenis_produk" => $id])->row();
}
To
public function getMinimalis($id)
{
return $this->db->get_where($this->_table, ["jenis_produk" => $id])->result();
}

Why doesn't the controller recognise the data passed on from the model?

I'm trying to get data from my DB using a model, to then be used in the view. I return the query results to my controller and it gives me the undefined variable notice.
I tried first tried performing a select(get) statement in the controller, I then defined the result array as row before defining the specific row to be used, to which I pass on to my view. That threw an error, then I tried the same thing but with a model and a return to the controller:
controller.php
public function Home()
{
$this->load->model('Main');
$this->Main->getresults();
$this->load->view('header', $data);
}
model.php
public function getresults() {
$query = $this->db->get('table');
foreach ($query->result_array() as $row) {
$data = array(
'column' => $row["column"]
);
}
return $data;
}
view.php
<?php echo $column; ?>
I expect the return of $data to the controller for it to be used in the view, yet it still throws a notice of an undefined variable.
In your controller you don't assign and send data to view.
Change your code there:
public function Home()
{
$this->load->model('Main');
$data = $this->Main->getresults();
$this->load->view('header', $data);
}

Laravel shows blank page as the view

I have created a function in my HomeController.php beside the index method in Laravel 5.3. In the index method, there are some data retrieved from database, and the sent to the other method to be analyzed and sent to the home view to be shown.
My problem is that laravel displays a mere blank page instead of the actual home.blade.php view. Below is the code that I am using to retrieve data from database and send them to the view. Could you please tell me what am I doing wrong here?
public function index ()
{
$user_table_data = DB::table($table_name)->get();
$this->ChooseAction($user_table_data);
}
private function ChooseAction ($r_value)
{
foreach ($r_value[0] as $key => $value)
{
if ( $key !=='id' && $value !== '0' )
{
array_push($this->choose_action_result, $key);
}
}
if (!empty($this->choose_action_result))
{
return view('home', ['rvalue' => $this->choose_action_result]);
}else{
return view('home');
}
}
in your index method return the call of private value
public function index ()
{
$user_table_data = DB::table($table_name)->get();
return $this->ChooseAction($user_table_data);
}
mark the return keyword in the call statement if should return the view
This will be your code:
public function index ()
{
$user_table_data = DB::table($table_name)->get();
return $this->ChooseAction($user_table_data); //here you also need a return
}
private function ChooseAction ($r_value)
{
foreach ($r_value[0] as $key => $value)
{
if ( $key !=='id' && $value !== '0' )
{
array_push($this->choose_action_result, $key);
}
}
if (!empty($this->choose_action_result))
{
return view('home', ['rvalue' => $this->choose_action_result]);
}else{
return view('home');
}
}
You didn't return the view in your public function index() {} that you got from your private method.

Inserting multiple values in database codeigniter?

I want to insert multiple rows in the database on one click if row is checked using multiple checkbox
Here is my code=>
1)Controller: guard.php
(Here I take a array of list of student id's and pass them one by one to the another function get_leave_data which take id of a student and return more information about the student from another table leave_application).
public function students_out(){
$request=$this->input->post('approve_leave_status');
if($request=="OUT"){
$check_list_out[]=$_POST['check_list_out'];
if(!empty($check_list_out)){
foreach ($check_list_out as $check_list_id) {
$student_data=$this->get_leave_data($check_list_id);
$this->insert_student_outside($student_data);
}
$this->load->helper('url');
$this->load->view('view_guard');
}
}else{
$this->load->helper('url');
$this->load->view('view_guard');
}
} `
public function get_leave_data($id){
$this->load->model('model_guard');
$data=$this->model_guard->get_data($id);
return $data;
}
public function insert_student_outside($std_data){
$this->load->model('model_guard');
$data=$this->model_guard->insert_student_out($std_data);
return $data;
}
2)Model: model_guard.php
(The functions get_data() and get_data2() return more informations about student and function insert_student_out() insert the student to the student_outside table)
public function get_data($id){
$this->db->select('leave_id,leave_from_roll_no,leave_student_name,leave_going_to,leave_from_date,leave_till_date,leave_hostel_no,leave_status');
$this->db->from('leave_application');
$this->db->where('leave_id',$id[0]);
$query=$this->db->get();
$data1=$query->result();
$data2=$this->get_data2($data1);
$final_array=array_merge($data1,$data2);
return $final_array;
}
public function get_data2($array){
foreach ($array as $key) {
$roll_no=$key->leave_from_roll_no;
}
$this->db->select('student_year,student_semester,student_parent_email');
$this->db->from('students');
$this->db->where('student_roll_no',$roll_no);
$query=$this->db->get();
$data=$query->result();
return $data;
}
public function insert_student_out($std_data){
$roll_no=$std_data[0]->leave_from_roll_no;
$id=$std_data[0]->leave_id;
$date_out=date('Y-m-d');
$inside_date=NULL;
$date_allowed=$std_data[0]->leave_till_date;
$array=array(
'outside_id'=>$id,
'outside_roll_no'=>$roll_no,
'outside_date_out'=>$date_out,
'outside_date_in'=>$inside_date,
'outside_date_allowed'=>$date_allowed
);
if($this->db->insert('students_outside',$array)){
return true;
}else{
false;
}
}
you are trying to insert batch_array using codeignator
the right syntax to using batch_array is:-
$array=array(
'coloum_name'=>$id,
'coloum_name'=>$roll_no,
'coloum_name'=>$date_out,
'coloum_name'=>$inside_date,
'coloum_name'=>$date_allowed
);
if($this->db->insert_batch('students_outside',$array)){
return true;
}else{
false;
}
you can read manual insert_batch here
ok, i am assuming that in $std_data you have more than one record.then in your model's insert_student_out() function try this-
public function insert_student_out($std_data){
foreach($std_data as $row){
$roll_no=$row->leave_from_roll_no;
$id=$row->leave_id;
$date_out=date('Y-m-d');
$inside_date=NULL;
$date_allowed=$row->leave_till_date;
$array=array(
'outside_id'=>$id,
'outside_roll_no'=>$roll_no,
'outside_date_out'=>$date_out,
'outside_date_in'=>$inside_date,
'outside_date_allowed'=>$date_allowed);
$this->db->insert('students_outside',$array);
}
}

pass variable between functions in same controller

class AppointmentsController extends AppController {
public function view($id = null) {
if (!$this->Appointment->exists($id)) {
throw new NotFoundException(__('Invalid appointment'));
}
$options = array('conditions' => array('Appointment.' . $this->Appointment->primaryKey => $id));
$this->set('appointment', $this->Appointment->find('first', $options));
$kk = $this->Appointment->find('first', array('fields' => 'status', 'conditions' => array('Appointment.id' => $id)));
$ss = reset($kk);
$stats = reset($ss);
}
}
I have set $stats via getting value from DB and i want to use in in another function in same controller
then I want to use like this
class AppointmentsController extends AppController {
function confirm(){
$stats = 'New';
}
}
Seeing that it's the same class, have you tried using $this->stats instead of a local variable?
You can call another function in your first function like
$this->confirm($status); // calling confirm function
function confirm($status)
{
//use status as you want
}
Or you can set status as global variable then this can you accessible in any function.
I think, you've to create function in the model that will return the value of the field by id.
<?php
// model/AppModel.php
public function getFieldById($field='id', $id){
return $this->field($field, array('id' => $id));
}
// in controller function you can access this like.
$status = $this->Appointment->getFieldById('status', $id); // $id will be appointment id.
?>

Categories