Codeigniter getting data by passing comma separated values - php

Here is my application details:
In mysql database, I have products table with id, description and price. And in basket table, I have products_id in the form 1,2,3 i.e. comma separated values.
Now, in controller, I got the product_id list from basket table by this function:
$data['baskets'] = $this->baskets_model->get_all();
$pr_id=$this->baskets_model->get_all();
foreach ($pr_id as $pr){
$get_pr_info=$this->products_model->get_all($pr->product_id);
}
I have this function in products_model model:
public function get_all($ids=NULL) {
if($ids !== NULL) {
echo $ids;
}
else {
$query = $this->db->get('products');
return $query->result_array();
}
}
The value $ids echoes the products_id as 1,2,3. Now, how to get the details of each product by passing this products_id from product table and show in basket? Please drop comment if my question is not clear.

Hope this may help you
public function get_all($ids=NULL)
{
$this->db->from('products');
if($ids !== NULL)
{
$this->db->where("id in ($ids)");//use this
//$this->db->where_in('id',$ids);//This may work but I think it will not give your right answer
//if your id like 1,2,3. this will not work i think but if you send only one id it will work.
}
$query = $this->db->get();
return $query->result_array();
}

Add where condition in to the database query i.e.
$this->db->where_in('your_column_name',$ids);

Related

Inner join query in codeigniter

code:
public function draft_post($idd)
{
$this->db->select('*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id','INNER');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
In this codes, I have two table i.e. registration and draft_registration. Now, What am I doing here I want to run inner join in Codeigniter. Now, What happening when I hit this query on phpmyadmin it shows wrong data i.e. I have two rows in draft_registration and one row in registration table but it always shows two table which is wrong and my query looks like when I was print as mention below:
SELECT *
FROM `registration`
INNER JOIN `draft_registration` ON `registration`.`user_id`= `draft_registration`.`user_id`
WHERE `registration`.`user_id` = '20181121064044'
So, How can I resolve this issue? Please help me.
Thank You
$this->db->select('*'); //This code get all rows from both table.If you want a particular row you mention the column name.
For example:
$this->db->select('registration.name,draft_registration.city,draft_registration.state');
Specify column that you want to select. Or if you want select all column of your table, you can use :
SELECT registration.* with backticks `` on column name
Use the Below Code
public function draft_post($idd)
{
$this->db->select('registration.*,draft_registration.*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
Or you can use with objects
public function draft_post($idd)
{
$this->db->select('a.*,b.*');
$this->db->from('registration a');
$this->db->join('draft_registration b', 'a.user_id= b.user_id');
$this->db->where('a.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}

Show join table results even on condition is null

I have five table name books,categories,publications,author,reviews.
in my app user can review the book after login. so reviews table stay empty until user post a review.
I am trying to run the flowing query in codeigniter model to get book details by category_id and it's working perfect when their review is exist otherwise it's return empty array. this is happening because of this $this>db>join('reviews','reviews.book_id = books.book_id'); condition return false
how can i show result even reviews table on condition is not match?
public function get_book_details($cat_id) {
$this->db->select('*');
$this->db->from('books');
$this->db->join('categories', 'categories.id = books.category_id');
$this->db->join('publications', 'publications.id = books.publication_id');
$this->db->join('author', 'author.id = books.author_id');
$this->db->join('reviews', 'reviews.book_id = books.book_id');
$this->db->where('categories.id', $cat_id);
$query = $this->db->get();
return $query->result();
}
Use LEFT JOIN to get the result if there are no matching associations are found
$this->db->join('reviews', 'reviews.book_id = books.book_id','LEFT');

i want rows with different category id using codeigniter

i passed employee id and category id to this function.
this employee posted some requirements for some users under severval category. and employees posted for same users with same category multiple times.so i want that repeated ones as single record.anybody knows,please help
public function get_data_print($id,$cat_id) {
$this->db->from('jil_requirementbrief');
$this->db->where('jil_requirementbrief.rqm_managedby', $id);
$this->db->where('jil_requirementbrief.rqm_category', $cat_id);
$query = $this->db->get();
$this->db->from('jil_users');
$this->db->where('jil_users.usr_id',$row->rqm_customerid);
$query2= $this->db->get()->row_object();
$row->users_name = $query2->usr_name;
$row->users_comp = $query2->usr_company;
}
use group_by to avoid duplication
$this->db->group_by('column_name');
Or you can try distict
$this->db->distinct();

Select from 2 tables in Laravel 4

I have a problem with my select, so I have 2 tables:
category
id name
product
id name category_id
Now I want to select products for one category:
public function getCategory($cat_id)
{
return View::make('store.category')
->with('products', Product::where('category_id', '=' , $cat_id))
}
This query is empty, exists another solution??Help me please.
Your function isn't built quite right. You aren't calling get() on the query, so its not actually running. And also, you aren't passing the products quite right. Try this
public function getCategory($cat_id)
{
$products = Product::where('category_id', '=' , $cat_id)->get();
return View::make('store.category')
->with(array('products' => $products));
}

How can I get the all value in one column using result()

Model:
function get_all() {
$query = $this->db->get('loan');
return $query->result();
}
function get_loan_name($code) {
$this->db->where('loan_type_code',$code);
$query = $this->db->get("loan_type");
return $query->row();
}
Controller:
function index()
{
$this->data['loans'] = $this->loan_approval->get_all();
$this->build_view("loan_approval/list.php");
}
In "LOAN TABLE" it have a "loan_type_code" column i want to get all the value in this column and match to 2nd function.
Example
Loan table Loan Type Table
ID loan_type ID loan_type_code loan_type_name
1 EDL 1 EDL Education Loan
2 DF 2 DF Damayan Fund
I need to match the "loan_type" in table "LOAN" to "loan_type_code" in "Loan Type" table.
Im thinking if of query like this "$this->controler->get_loan_name($this->data['loans']->loan_type);" but its not working.
Why not doing it at DB level using a JOIN?
$query = $this->db->select('loan.*, loan_type.loan_type_name')
->from('loan')
->join('loan_type','loan.loan_type = loan_type.loan_type_code')
->get();
return $query->result();

Categories