CodeIgniter 3 - joint data from another table - php

Original function:
function getRows($id = ""){
if(!empty($id)){
$query = $this->db->get_where('orders', array('id' => $id));
return $query->row_array();
}else{
$query = $this->db->get('orders');
return $query->result_array();
}
}
I need to JOIN data from table order_shipping and order_products where order_id = order_id
I do:
/*
* Fetch user data
*/
function getRows($id = ""){
if(!empty($id)){
$query = $this->db->get_where('orders', array('id' => $id));
return $query->row_array();
}else{
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_shipping', 'orders.id = order_shipping.id');
$this->db->join('order_products', 'order_shipping.id = order_products.id');
$query = $this->db->get('');
return $query->result_array();
}
}
This above code working correct. But currently JOIN only after else. I need do the same way before else:
* Fetch user data
*/
function getRows($id = ""){
if(!empty($id)){
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_shipping', 'orders.id = order_shipping.id');
$this->db->join('order_products', 'order_shipping.id = order_products.id');
$query = $this->db->get('', array('id' => $id));
return $query->row_array();
}else{
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_shipping', 'orders.id = order_shipping.id');
$this->db->join('order_products', 'order_shipping.id = order_products.id');
$query = $this->db->get('');
return $query->result_array();
}
}
But this code before else not working correct for me and currently display wrong data. When I try fetch all orders, then second function display correct all orders. But when I try fetch example only order ID 2, then is used first function and display wrong data, example when I fetch order ID2, then always display only order with ID 1.
This function is for REST API fetch orders.

Related

How to get previous and next posts in codeigniter blog post page

I'm making a blog in Codeigniter. Now on the blog post view page, I want the previous and next post link. This is my controller of blog details.
public function blog_details($slug = NULL){
//data
$data['blog_info'] = $this->Blog_model->get_posts($slug);
if(empty($data['blog_info'])){
show_404();
}
$data['post_name'] = $data['blog_info']['post_name'];//post name
$data['categories'] = $this->Blog_model->get_categories();
//meta title, discription, keywords
$data['meta_title'] = $data['blog_info']['post_name'];
$data['meta_description'] = $data['blog_info']['post_meta'];
$data['meta_keywords'] = $data['blog_info']['post_tags'];
//view
$data['main_content'] = 'blog_details';
$this->load->view('include/template',$data);
}
and my model is
public function get_posts($slug = FALSE, $limit = TRUE, $offset = FALSE){
if($limit){
$this->db->limit($limit, $offset);
}
if($slug === FALSE){
$this->db->order_by('blogs.post_id', 'DESC');
$this->db->join('category', 'category.category_name = blogs.category');
$query = $this->db->get('blogs');
return $query->result_array();
}
$query = $this->db->get_where('blogs', array('post_slug' => $slug));
return $query->row_array();
}
How to create previous and next post links with post names? Thanks in advance.
you can use direct query method to run below query and just pass your blog id to get previus and next blog name
public function get_prewnext($id){
$query = $this->db->query("SELECT name,
(SELECT name FROM blog s1
WHERE s1.id < s.id
ORDER BY id DESC LIMIT 1) as previous_name,
(SELECT name FROM blog s2
WHERE s2.id > s.id
ORDER BY id ASC LIMIT 1) as next_name
FROM blog s
WHERE id = $id;");
if($query->num_rows() > 0){
$data = $query->result_array();
}
$query->free_result();
return $data;
}
Note: If value is not found then it will return null.

how to using SQL INSERT INTO SELECT with codeigniter

How to use the SQL insert into select in CodeIgniter.
Model...
public function history($book_id)
{
$query = $this->db->query('INSERT orders (book_id, title)
SELECT book_id, book_title
FROM books
WHERE book_id = \'$book_id\'');
return true;
}
First of get all books from books table then INSERT order according to your $book_id
Example:
public function history($book_id)
{
$this->db->select('book_id, book_title');
$this->db->from('books');
$this->db->where('book_id', $book_id);
$query = $this->db->get();
if ( $query->num_rows() > 0 ) // if result found
{
$row = $query->result_array(); // get result in an array format
$data = array();
foreach($row as $values){
$data = array(
'book_id' => $values['book_id'],
'title' => $values['book_title']
);
$this->db->insert('orders', $data); // insert in order table
}
return true;
}
else{
return false;
}
}

After fetching a value from DB then assign it to a variable, then i can't use it to the next query

This is my model:
public function GetStudentData()
{
$sID = $this->input->get('id');
$class_id = $this->db->query("SELECT student.class_id FROM student WHERE student.sID = '$sID'");
$query = $this->db->query("SELECT * FROM student WHERE(sID='$id'AND class_id = '$class_id')");
if($query->num_rows() > 0)
{
return $query->row();
}else{
return false;
}
}
How can I use $class_id in the next query?
you could do it in one query... (from your example [SIC])
public function GetCSData() {
$id = $this->input->get('id');
$query = $this->db->query("SELECT student.*
FROM student
WHERE student.sID = '$id'
AND student.class_id = (SELECT class_id
FROM student
WHERE student.sID = '$id')");
if($query->num_rows() > 0){
return $query->row();
}else{
return false;
}
}//..GetCSData end
looking at it though, that will give the same result as
SELECT *
FROM student
WHERE sID = '$id'
maybe I am missing a point?

Use Equation Variable in Codeiginiter

Hi guys I tried to use the variable equation on Codeigniter as in the code I wrote below, Is my code writing correct? and here's my code so far I wanna using variable $amount in my controller
In Controller
$amount = $this->m_data->getTotalSales->$data['TOTAL_SALES'];
and this in Model
//GET TOTAL REVENUE
function getTotalSales(){
$this->db->select("(SELECT SUM(grand_total) FROM sales_order WHERE member = ".$this->session->userdata('ID').") - (SELECT SUM(amount) FROM payment WHERE member_id = ".$this->session->userdata('ID').") AS total_sales");
$query = $this->db->get();
if ($query->num_rows() >0){
foreach ($query->result() as $data) {
$hasilSemua[] = $data;
}
return $hasilSemua;
}
}
$amount = $this->m_data->getTotalSales();
function getTotalSales(){
$result1 = $this->db->select_sum("grand_total")
->from('sales_order')
->where("member = $this->session->userdata('ID')");
->get();
$result2 = $this->db->select_sum("amount")
->from('payment')
->where("member_id = $this->session->userdata('ID')")
->get();
return $result1->row()-$result2->row();
}
}
I believe this is what you are after. Note that I pass the UserID in as the second parameter to the WHERE function. This is in case there is any chance of the user having inputted it (always safter to do this anyway).
//GET TOTAL REVENUE
function getTotalSales(){
$db = $this->db;
$db->select('SUM(grand_total)');
$db->where('member',$this->session->userdata('ID'));
$q1 = $db->get_compiled_select('sales_order');
$db->select('SUM(amount)');
$db->where('member_id',$this->session->userdata('ID'));
$q2 = $db->get_compiled_select('payment');
$query = $db->query("SELECT ($q1) - ($q2) as total_sales");
$data = $query->row_array();
return $data['total_sales'];
}
You can then call $amount = $this->m_data->getTotalSales; to get your result.

CodeIgniter: get data after each 'foreach()' command

$query = $this->db->get("courses");
$data['courses'] = $query->result();
foreach($courses as $c){
$this->db->select('*');
$this->db->from('subjects');
$this->db->where('id',$c->course);
$query = $this->db->get("subjects");
$data['subjects'] = $query->result();
foreach($subjects as $s){
$this->db->select('*');
$this->db->from('tests');
$this->db->where('id',$t->test);
$query = $this->db->get("tests");
$data['tests'] = $query->result();
}
}
I want to print tables of Courses having top first row with single column with data $c->course
and bellow rows with two column having data $s->subject and $t->test respectively...
you can use like this for first row for courses and next two rows for subjects and tests
$query = $this->db->get("courses");
$data['courses'] = $query->result();
foreach($courses as $key=>$c){
$this->db->select('*');
$this->db->from('subjects');
$this->db->where('id',$c->course);
$query = $this->db->get("subjects");
$data['subjects'][$key] = $query->result();
foreach($subjects as $key=>$s){
$this->db->select('*');
$this->db->from('tests');
$this->db->where('id',$t->test);
$query = $this->db->get("tests");
$data['tests'][$key] = $query->result();
}
}

Categories