Mysql Two table join Query not working in codeigniter - 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.

Related

Why we need to create a function in model page for relation

public function single($category, $slug)
{
$category = Category::where('slug', $category)->first();
$article = Article::where('slug', $slug)->where('category_id', $category->id)->first();
$data['comments'] = Comment::where('article_id', $article->id)->get();
}
These codes are on my controller page. When I take the comments on my article page it is working fine.
But on the laravel webpage, they say we have to create function in Model. Why we need to create a function in Model page? My structure is working fine without extra function.
I will be glad if you explain, thank you.
the difference is you can do something like this with a single db query and alter the results:
public function single($category, $slug)
{
$category->where('slug', $category)->articles()->where('slug', $slug)->with('comments');
}

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);

laravel relation with 3 table using Eloquent?

i wanna use multiple hasMany and belongsTo that propositions uses two function, onece for return notes and another once for return actions
how can retrieve data note from Action::with('proposition ')
Model Proposition
public function proposition()
{
return $this-
>belongsTo('App\Proposition','proposition_num_proposition');
}
Model Action
public function action() {
return $this->hasMany('App\Action','proposition_num_proposition');
}
Model Note
public function notes(){
return $this->hasMany('App\Note','proposition_num_proposition');
}
And tables
proposition
num_proposition
proposition
Action
id_action
action
prposition_num_proposition
Note
id_note
note
proposition_num_proposition
in Controller
$actions = Action::all();
$propositions = Proposition::with('actions');
$notes = Note::all();
$propositions2 = Proposition::with('notes');
in view
i wanna retrieve data from proposition where(note.proposition_num_proposition = action.proposition_num_proposition)
using the laravel elequont
Any help plz ?
Have you tried to use it like this
$actions = Action::with(['proposition', notes])->get();
Or you can look at eager loading in the laravel docs
If you use array as input by with should solve your problem

codeigniter pass variable from controller to model

simple issue I presume.
My controller is getting the if to display from the url using $this->uri->segment(3). This will always be a single value. I am putting this in an array to pass to the model with:
$customerid = array(
'id' => $this->uri->segment(3)
);
The controller syntax is below:
function confirm_delete_customer()
{
$data['title']="Confirm Customer Deletion";
$customerid=array(
'id'=>$this->uri->segment(3)
);
//query model to get data results for form
$data=array();
if($query=$this->model_master_data->get_customer_records_to_delete()){
$data['records']=$query;
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_confirm_customer_deletion",$data);
$this->load->view("master_data/view_master_data_footer");
}
I am then trying to access this array value and pass it to my model to process. If I hard code the array into the model it works as per below syntax:
Model - Manual Syntax is:
function get_customer_records_to_delete()
{
$query = $this->db->get_where('customers', array('id'=>43));
return $query->result();
}
if I try replace this with the array from my controller it fails with error:
Undefined variable: customerid
idea of model that I want to get working:
function get_customer_records_to_delete()
{
$query = $this->db->get_where('customers', $customerid);
return $query->result();
}
I have a feeling it is something small. however is this the best way to get a single record from the database in order to output to a view?
Thanks in advance for the assistance.
The best way to do that is:
function confirm_delete_customer()
{
$data=array();
$data['title']="Confirm Customer Deletion";
$customerId = $this->uri->segment(3);
//Prevent SQL injections
if(!is_numeric($customerId) || empty($customerId)) {
show_error("Bad Request");
}
$query = $this->model_master_data->get_customer_records_to_delete($customerId);
if ($query){
$data['records']=$query;
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_confirm_customer_deletion",$data);
$this->load->view("master_data/view_master_data_footer");
}
}
and then you can simply call:
function get_customer_records_to_delete($customerId)
{
$query = $this->db->get_where('customers', array('id'=>$customerId));
return $query->result();
}
at your model.
You need to pass the value as an argument to the function so it can access it.
Ex:
get_customer_records_to_delete($customerid)
{
// now $customerid is accessible
$query = ....;
return $……;
}
You should heavily rely on function parameters. Grab the customer id from the controller and send it to the model. Moreover, you can use row() to get a single result from the database.
Controller:
function confirm_delete_customer(){
$data['title']="Confirm Customer Deletion";
$customerid=$this->uri->segment(3);
//query model to get data results for form
$data=array();
if($query=$this->model_master_data->get_customer_records_to_delete( $customerid)) //you are sending customer id as a parameter here
$data['records']=$query;
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_confirm_customer_deletion",$data);
$this->load->view("master_data/view_master_data_footer");
}}
Model
function get_customer_records_to_delete($customerid)
{
$query = $this->db->get_where('customers', array("id"=>$customerid)); //you are using the customer id sent from the controller here
return $query->row(); //this will return a single row
}
Old thread but the answer is to declare the variable as "public" in the controller (i.e. public $customerid;), in which case it'll be available to your model. In some cases it's probably safer to explicitly pass as an argument. However, when you have several variables, it's useful to have the option to declare them instead.

Resources