how to add information from difrent table? - codeigniter

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

Related

Mysql Two table join Query not working in codeigniter

I want to integrate a two tables order and orderdetails table with an id of ord_id. My code is not working.
public function orderview()
{
$orderid = $this->uri->segment(3);
$data["order"]=$this->db->join('tbl__order', 'tbl__order_detail.ord_id = tbl__order.ord_id',$Query=' where ord_id='. $orderid);
$this->load->view('manage/orderpayment/view',$data);
}
Are you ever work with Codeigniter before?
Your Model function must look like this. Call this function from your Controller to return data, then you can process this data and send it to your View.
public function orderview(){
$orderid = $this->uri->segment(3);
$this->db->select('tbl__order.*, tbl__order_detail.ord_id as tbl__order_id');
$this->db->from('tbl__order');
$this->db->join('tbl__order_detail', 'tbl__order_detail.tbl__order_id = tbl__order.ord_id');
$this->db->where('tbl__order.ord_id', $orderid);
return $this->db->get()->result_array();
}
In "join" you can use as third argument left, right, outer etc.

How to get data from 2 related tables in the same controller in codeigniter?

I'm trying to get data from 2 DB tables that are related. Departments and Functions: Each Department has multiple Functions.
I want to show all data in an accordion on the same page.
Until now, the code works like this:
Declare the variable departments in the controller, and in view, in the foreach loop, I have called the Functions model in a variable.
Controller:
public function index(){
//Data variable declarations
$data['page_title'] = "Departments and Functions";
$data['page_subtitle'] = "choose an occupation for the list of users assigned";
//Data variable declaration - from models
$data['departments'] = $this->Department->get_all('departments');
$this->load->view('layouts/header');
$this->load->view('layouts/title', $data);
$this->load->view('layouts/aside');
$this->load->view('departments/index', $data);
$this->load->view('layouts/footer');
}
Here I want to add the Function->get_by_department_id() method. How to get the ID dynamically?
The occupation model methond:
public function get_by_fk($table = 'occupations', $fk = "department_id" , $fk_value=$department->department_id){
$this->db->select('*');
$this->db->from($table);
$this->db->where($fk, $fk_value);
$query = $this->db->get();
return $result = $query->result();
}
Try using a left join
function get_departments_and_functions() {
$sql = 'SELECT `deptartments`.*, `functions`.*
FROM `departments` LEFT JOIN `functions`
ON `functions`.`deptartments_id` = `departments`.`id`';
return $this->db->query($sql)->result();
}
You could also query the departments and then foreach over the departments and query to get the functions. Then you would manually build the result set.

Pass specific id to model and take data from database using codeigniter?

My controller method like below
public function index($book_id) {
print_r($book_id);
}
I will get the id from view. View like below
I need to get specific row according id on model how can I do that if I use query in controller its not working
If I use like below in controller it gives something else as result
public function index($book_id) {
print_r($book_id);
$this->db->select('*');
$this->db->from('books')->where('book_id', $book_id);
$query = $this->db->get();
print_r($query);
}
but if I use same query in model with hard coded id for testing it gives the expected out put
Please help me with this
You don't need to include "/index" in anchor tag href as controller default method will be index if found. And "print_r($book_id)" should be "echo $book_id" as $book_id is variable not array.
Please try this code (either in model/controller):
$query = $this->db->get_where('books', array('book_id' => $book_id));
$result = $query->row_array();
print_r($result);

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 - Changing a regular query to an active record query

I was wondering if someone could show me how to change the following sql query to codeigniters active record structure?
$query = $this->db->query('SELECT * FROM '.$this->table_name.' WHERE firstname LIKE "%'.$searchterm.'%" OR lastname LIKE "%'.$searchterm.'%"');
return $query->result();
Cheers
Try:
$this->db->like('firstname', $searchterm);
$this->db->or_like('lastname', $searchterm);
$query = $this->db->get($this->table_name);
return $query->result();
$query = $this->db->get($this->table_name)->like('firstname', $searchterm)->or_like('lastname', $searchterm);
For more information on CodeIgniter's database library, see this documentation.

Resources