Problem in updating field in codeigniter please help - codeigniter

It says , you must use the “set” method to update an entry. Pls help
My model is
$this->db->where('id', $this->uri->segment(3));
$this->db->update('mytable', $data);
My controller is
$data = $this->db->select('mytable', $_POST);
$this->contact_model->model_update_function($data);

Your $data variable doesn't contain a valid array. This is because $this->db->select(); doesn't actually run the query, you need $this->db->get(); or $this->db->get_where(); in order to do so. Even then, you also need to call $query->result(); to retrieve the data from the result.
Your controller should be
$query = $this->db->get_where('mytable', $_POST);
$data = $query->result();
$this->contact_model->model_update_function($data);

Related

how to add information from difrent table?

i am new in CI i have a data the result from a process and i want to additional information on that from difretnt table like this picture.
both table are linked to an id (idhn). i make query join in model like this
function gabung(){
$this->db->select('*');
$this->db->from('metode');
$this->db->join('hasil_inden','hasil_inden.idhn=metode.idhn');
$query = $this->db->get();
return $query->result();
}
i called $metode in controller like this
' $metode = $this->Proses_model->gabung();'
but it is not work undefined variablev $metode please give me solution to fix it

how to fetch all records from order table in Codeigniter

$data is an array which contain user post data for fetch record from orders table
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$this->db->get();
$data = array(
'customer_id' => $this->input->post('custId')],
'paided' => 2
);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$this->db->get();
try this :
public function function_name (){
$data = array (
'customer_id' => $this->input->post('custId'),
'paided' => 2
);
$this->db->select('*');
$this->db->from('ordere');
$this->db->where($data);
$query = $this->db->get();
return $query->result_array();
}
You have done all good just need to put result() if you get multiple row or row() if you get one row
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$result= $this->db->get()->result(); //added result()
print_r($result);
as simple use
$custId = $_post['custId'];
$query = $this->db->query("SELECT * FROM orders WHERE customer_id= '$custId' AND paided='2'");
$result = $query->result_array();
return $result;//result will be array
This is the plus of using framework, you don't need to write that much of code,
$where = array('customer_id' => $this->input->post('custId'),'paided'=>2)
$result = $this->db->get_where('orders', $where);
and for fetching them, use $result->row() for single record retrieval.
If you want all records, use $result->result()
Here is documentation link, if you want to learn more.
What You should need to be Correct And Why
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*'); // by defaul select all so no need to pass *
$this->db->from('orders');
$this->db->where($data);
$this->db->get(); // this will not return data this is just return object
So Your Code Should be
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select(); // by defaul select all so no need to pass *
$this->db->from('orders');
$this->db->where($data);
$query = $this->db->get();
$data = $query->result_array();
// or You can
$data= $this->db->get()->result_array();
here result_array() return pure array where you can also use result()
this will return array of object

CI pagination syntax for getting results

Assume that the config is done and it is intialized, then time for the results to be paginated.
On this line of code.
$data['records'] = $this->db->get('tablename', $config['per_page'], $this->uri->segment(3));
Is there anyway of changing the above line of code. Since I have 3 tables with ERD relationship. So in my 3rd table consists lots of rows that are connected to the other 2 tables. I want to make a model function where i will manually query the results. How am i supposed to write it?
$data['records'] = $this->load->model('manual_querying'), $config['per_page'], $this->uri->segment(3);
Im not sure if its correct, i am still unable to test it if its correct since im not done with my pagination yet.
In model, write a function
public function get_records($per_page,$page)
{
$this->db->select("*");
$this->db->from("table1 as t1");
$this->db->join("table2 as t2","t1.id = t2.t1_id");
$this->db->join("table3 as t3","t1.id = t3.t1_id");
$this->db->limit($per_page,$page);
$res = $this->db->get();
return $res;
}
In controller,
$this->load->model('your_model');
$data['records'] = $this->your_model->get_records($config['per_page'], $this->uri->segment(3));

Codeigniter, how to pass COUNT result too my view

i have problem with passing data from model to my view. In Database i have Project with a few tasks i need to COUNT it. My problem is: How to pass the results to the view with foreach.
(echo $this->db->count_all_results(); in MODEL show good results)
Project 1: 5 tasks
Project 2: 10 tasks
Project 3: 1 task
MODEL:
function wyswietl($arrayid){
$query = $this->db->query("SELECT COUNT( id_zadania ) AS zlicz
FROM projekty_zadania
WHERE id =$arrayid");
return $query->result();
CONTROLLER
function WszystkieProjekty(){
$data['moje'] = $this->Todo_model->wyswietlWszystkieProjekty();
$arrayid = array();
foreach( $data['moje'] as $row ){
$row->id;
$aaa = $row->id;
$arrayid[] = $this->Todo_model->wyswietl($aaa);
}
$data['tabid'] = $arrayid;
$this->load->view('Projekty.html', $data);
}
MODEL:
function wyswietlWszystkieProjekty(){
$query = $this->db->query("SELECT * FROM todo");
return $query->result();
}
You're passing back to your controller a result set, so all you need to do is iterate over the result set, generating actual rows. It's not too hard, and you've got the right idea.
You have:
function wyswietl($arrayid){
$query = $this->db->query("SELECT COUNT( id_zadania ) AS zlicz
FROM projekty_zadania
WHERE id =$arrayid");
return $query->result();
As your model function that is supposed to return the count, right? Easiest way to do this is right from the model to keep your controller from having to do the legwork. When you run the count function in mysql, you'll only get back one row in your case, so using num_rows() isn't going to help because this query will always generate 1 row.
function wyswietl($arrayid){
$query = $this->db->query("SELECT COUNT(id_zadania) AS zlicz
FROM projekty_zadania
WHERE id =$arrayid");
$rs = $query->result();
return $rs[0]['COUNT(id_zaedania)'];
}
In your query, you have counted the data, so i think you can get the total data in the model from $query->rows()->zlics. And in the controller, just pass it to the view like usual
$data['moje'] = $this->Todo_model->wyswietlWszystkieProjekty(); $this->load->view('Projekty.html', $data);

Codeigniter Limit not working

I'm nearly reaching rage mode!
Can anyone help me with this??
I setted a limit on a CodeIgniter Model so I can only that set of results. Why is it not working? The records are displaying correctly on function call, but I'm getting more than how it's supposed to
$this->CI->dbClient->select('id,title,description,url,date,id_first_image')
->from('posts')
->where('deleted',"0")
->where('date >',$date)
//Neither does work here!
->order_by('date','asc')
->limit(5);
$query = $this->CI->dbClient->get();
return $query->result();
1. Use db
Use db for the Active Record class.
$this->db->select('id, title, description, url, date, id_first_image')
->from('posts')
->where('deleted', "0")
->where('date >', $date)
->order_by('date', 'asc')
->limit(5);
$query = $this->db->get();
return $query->result();
2. $data is Valid
Ensure that $data is a valid MySQL date (or whatever your underlying database technology might be).

Resources