i want rows with different category id using codeigniter - php

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

Related

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

PHP - 2 MySQL Queries in One PHP Function

I have two functions that I am using 2 different queries. I need to make one function that will retrieve the number of views for an item. Below are the two functions:
public function products_views(){
$this->db->select('*');
$this->db->from('products');
$this->db->order_by('view_count', 'DESC');
$query = $this->db->get();
return $query->result();
}
public function getViewCount($product_id) {
$this->db->select('COUNT(*) AS cnt');
$this->db->from(views);
$this->db->where('product_id', $product_id);
$query = $this->db->get();
return $query->row()->cnt;
}
I want a query that will return all the total view count for each product from the views table and display all the products from the products table showing the total view count.
You need to use JOIN for tables products and views connecting products.id with views.product_id. As a result, the request should look like this:
SELECT products.id, COUNT(views.id) as cnt
FROM views JOIN product ON products.id = views.product_id
GROUP BY views.product_id
You need to interpret this request using your active records.

how to join a two tables with arrays of datas

My first table category contain categoryid and categories.
Second table bloggers contains bloggercategory.
bloggercategory contain array of categoryid (more than one categoryid).
function selectusercategories($sess_id)
{
$this->db->select('*');
$this->db->from('categories');
$this->db->join('bloggers', 'blogger_category = category_ID');
$this->db->where('ID', $sess_id);
$querycat = $this->db->get();
return $querycat->result();
}
Can I join the two tables to display the bloggercategory individualy from the array with its categories. I tried this way but its not working.
For comma separated field, use MySQL FIND_IN_SET()
SELECT *
FROM categories c
JOIN bloggers b
ON FIND_IN_SET(c.category_ID ,b.blogger_category)
Try this..
$this->db->select("*");
$this->db->from('categories');
$this->db->join('bloggers', 'categories.category_ID= bloggers.blogger_category ');
$query = $this->db->get();
return $query->result();

codeigniter join 2 table data

hi everyone i am new to codeigniter and currently working on a small project in the project i am trying to join two tables and display there data in single table. i looked at the user guide that codeigniter has an i am not sure how this work
$this->db->join();
what table should be first and what id key should be firs. Can someone explain me more in detail about this please use examples if u can. I am trying to join credential table and tblanswers. Tnx for answering.
i have tried to code a function using this example:
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
EDIT:
instead of using join method in codeigniter is it possible to use a simple function to retrieve the two table data separately? all i want is to echo the data from database table on to a html table in my website page to be displayed is it possible to write two get functions to retrieve two tables separately ?
It doesn't matter what table is first... Simply:
<?php
$this->db->select('t1.name, t2.something, t3.another')
->from('table1 as t1')
->where('t1.id', $id)
->join('table2 as t2', 't1.id = t2.id', 'LEFT')
->join('table3 as t3', 't1.id = t3.id', 'LEFT')
->get();
?>
here is how it works:
suppose we have two tables namely student, library.
note: but remember that one of the column should match if you want to use where condition/ here we assume that both tables have std_id column
Write the the select query as follows, in the brackets write what are all the things you want
note:write as shown below don't put quotes to each single one put it on whole at once.
*note: suppose we want name, phone_no. from student table and book_name form library table.*
$this->db->select('name, phone_number, book_name');
Now write the from query and write one of the table name(No rule)
$this->db->from('student');
Now join this with the another table with join query
$this->db->join('library', 'students.std_id=library_std_id');
Now write the where condition that like you want book name form library table where std id=1(in practical you need to fetch this id from view/database)
$this->db->where('std_id', 1);
$q= $this->db->get();
That's it it's done now you can print and check the result.
$this->db->join('comments', 'comments.id = blogs.id');
With this line you tell: search me inside comments all comments with id equal blogs.id.
Usually is something like that I think:
$this->db->join('comments', 'comments.blogs_id = blogs.id');
You have to insert into your table a field named blogs_id (int value unisgned) because blogs can have more comments.
Isn't important the position of first or second value
Hi this will work for joining two tables in codeIgnator.
$this->db->select("chat.id,chat.name,chat.email,chat.phone,chat.date,post.post");
$this->db->from('chat');
$this->db->join('post', 'chat.id = post.id');
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result();
}
else
{
return false;
}
You can change to the query as you like do trail and error method to get your appropriate results.
This is my code for joint many tables as possible.
I have 3 tables professeurs,publications and support.
public function toutesdonnées(){
$this->db->select("*");
$this->db->from('publication');
$this->db->join('support', 'publication.idsup = support.idsup');
$this->db->join('professeurs', 'publication.emailprof = professeurs.emailprof');
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result();
}
else
{
return false;
}

CodeIgniter/PHP/MySQL: Retrieving data with JOIN

I'm new to PHP/MySQL and super-new to CodeIgniter..
I have information in many MySQL tables. I want to retrieve it with JOIN where the tables primary keys are equal to $variable... How can I do it and get all the fields without the primary key field???
What I'm doing now is this (only two tables joined here):
function getAll($id) {
$this->db->select('*');
$this->db->from('movies');
$this->db->join('posters', 'movies.id= posters.id');
// WHERE id = $id ... goes here somehow...
$q = $this->db->get();
if ($q->num_rows() == 1) {
$row = $q->row();
$data = array(
'id' => $row->id,
'title' => $row->title,
'year' => $row->year,
'runtime' => $row->runtime,
'plotoutline' => $row->plotoutline,
'poster_url' => $row->poster_url
);
}
$q->free_result();
return $data;
id (PK), title, year, runtime and plotoutline are columns from the first table and poster_url is a field from the second table. The second table also contains an ID (PK) column that I don't want to Retrieve because I already have.
Jon is right. Here's an example:
$this->db->select('movies.id,
movies.title,
movies.year,
movies.runtime as totaltime,
posters.poster_url');
$this->db->from('movies');
$this->db->join('posters', 'movies.id= posters.id');
$this->db->where('movies.id', $id);
$q = $this->db->get();
This will return objects that have ->id, ->title, ->year, ->totaltime, and ->poster_url properties. You won't need the additional code to fetch the data from each row.
Don't forget, if the Active Record syntax gets a little unwieldy, you can use full SQL queries and get the same results:
$sql = "SELECT movies.id,
movies.title,
movies.year,
movies.runtime as totaltime,
posters.poster_url
FROM movies
INNER JOIN posters ON movies.id = posters.id
WHERE movies.id = ?"
return $this->db->query($sql, array($id))->result();
Both forms will ensure that your data is escaped properly.
CodeIgniter Active Record
Query Binding in CodeIgniter
An asterisk will return all the fields. To return a subset of these, i.e. all fields apart form the repeated id field, simply list the columns which you require rather than use '*'.
It is often a good idea to not use asterisk anyway. In the future of the app, someone may add a large field to the table which will be surplus to your requirements, and will slow your queries.
Simply put with method chaining:
$this->db->select('*')
->from('movies')
->join('posters', 'movies.id= posters.id')
->where('movies.id', $id)
->get();
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();

Categories