Select from table with values separated by comma - php

I am having trouble with my query since I really don't know what to write. I want to get value from the database from my data which is inside an array separated by comma.
Let's say I have this method on my controller
query_array = array(
'tags' => explode("%2C", $this->input->get('tags'))
);
Then currently I have this query on my model.
if(strlen($query_array['title'])) {
$this->db->select("
si.song_info_file_name AS filename,
si.song_info_revised_file_name AS revised_filename,
si.song_info_original_title AS title,
a.artist_name AS artist,
si.song_info_id AS songinfoid,
array_to_string(array_agg(c.composer_name),'/') AS composer,
array_to_string(array_agg(p.publisher_name),'/') AS publisher,
array_to_string(array_agg(st.tag_name),',') AS tags"
);
$this->db->from('song_info si');
$this->db->join('original_artist a', 'si.artist_id = a.artist_id', 'left');
$this->db->join('composer c', 'si.composer_id = c.composer_id', 'left');
$this->db->join('publisher p', 'si.publisher_id = p.publisher_id', 'left');
$this->db->join('song_info_tags sit', 'si.song_info_id = sit.song_info_id', 'left');
$this->db->join('song_tags st', 'st.tag_id = sit.tag_id', 'left');
$this->db->like('LOWER(a.artist_name)', strtolower($query_array['title']));
$this->db->group_by(array("si.song_info_id", "a.artist_name", "c.composer_name" , "p.publisher_name" ));
$query = $this->db->get();
return $query->result_array();
}
The value of 'tags' => explode("%2C", $this->input->get('tags')) is Array ( [0] => 4,10,11,14 ) if I already explode it. my question now is that how would I be able to add this to my select LIKE criteria? for example LIKE tag_id = from the result of the array. I really need some help since I'm stuck with this one for kinda while now.

Related

How to get two id data from same table using codeigniter join funtion

I have table call jobs and i need to get jobs.user_iduser and jobs.adminId from same table call user
I try this code it output only one user data only.
$this->db->from('jobs');
$this->db->join('user', 'user.iduser = jobs.user_iduser', 'left');
$this->db->join('user', 'user.iduser = jobs.adminId', 'left');
then i try like this it output only one user data only.
$this->db->from('jobs');
$this->db->join('user', 'user.iduser = jobs.user_iduser', 'left');
$this->db->join('user as admin', 'admin.iduser = jobs.adminId', 'left');
Is there any way to achieve this?
You can try this.
$this->db->select('u.iduser as uiduser, ua.iduser as uaiduser and so on');
$this->db->from('jobs');
$this->db->join('user u', 'u.iduser = jobs.user_iduser', 'left');
$this->db->join ('user ua', 'ua.iduser = jobs.adminId', 'left');
In your case since the user joined in the first join is being replaced by the second join. So you need to try aliasing the tables.
Try this:
$this->db->from('user');
$this->db->join('jobs', 'user.iduser = jobs.user_iduser', 'left');
$this->db->join('jobs', 'user.iduser = jobs.adminId', 'left');
I don't exactly know the data in tables but left join in SQL means:
Select all records from Table A (user in above example),
along with records from Table B (jobs in above example) for
which the join condition is met (if at all). Ref
So, since you have said only one user data is being returned, with jobs being the left table I am assuming the table jobs contains only one user data. Let me know if you don't follow me.

mysql data fetch with codeigniter query

I am trying to perform reporting from database where user will enter his own criteria to pull data,
Here is modal function of my fetch,
public function fetch_insight($insight_start, $insight_end, $user_id,
$manager_id, $category_id, $expense_method, $report_status, $policy_breach)
{
$array = array('ec_expensedetails.expense_date >' => $insight_start,
'ec_expensedetails.expense_date <=' => $insight_end,
'ec_reports.report_status' => $report_status,
'ec_expensedetails.user_id' => $user_id,
'ec_reports.manager_id' => $manager_id,
'ec_expensedetails.category_id' => $category_id,
'ec_expensedetails.expense_method' => $expense_method,
'ec_expensedetails.policy_breach' => $policy_breach);
$this->db->where($array);
$this->db->select('ec_expensedetails.*');
$this->db->select('ec_expensecategory.*');
$this->db->select('ec_reports.*');
$this->db->select('ec_users.*');
$this->db->from('ec_users');
$this->db->join('ec_expensedetails', 'ec_expensedetails.user_id = '.$user_id,'INNER');
$this->db->join('ec_expensecategory', 'ec_expensecategory.category_id = ec_expensedetails.category_id','INNER');
$this->db->join('ec_reports', 'ec_reports.report_status = ec_reports.report_status ','INNER');
return $this->db->get()->result_array();
}
Now in when user selects fields like $user_id, $manager_id, $category_id, it gives me result array,
Problems,
But example if user just want to pull data with dates without any other where conditions, how can i remove that other where conditions of user_id, manager_id, category_id?
report_status in JOIN is column value field, i can join tables with ID field, but how can i JOIN table with value field in codeigniter? see last join in code.
Thanks,

how can i pass array in where clause with codeigniter?

How can I pass an array variable in where clause with codeigniter?
For example with a SQL query like:
Select(" item,price Where(userid=123 and proid=3 or userid=124 and proid=3... [upto n user id]userid=nth_id and proid=3");
If userid is stored in an array variable then how can I create such a condition in codeigniter?
I you are looking for this, it should look for a userid that is in the array and also has a proid of 3. I have not tested it but let us know how you get on.
$array = array( '123', '124' );
$this->db
->select( 'item, price' )
->from( 'table')
->where( 'proid', '3' )
->where_in( 'userid', $array )
->get();
$result = $query->result();

Query-not getting right answer

I want to used group by so that I can get data as country-state-district. I want only display the country then state and a district level want to add checkbox.
$this->db->distinct();
$this->db->select('country_name,s_name,dist_name');
$this->db->from('resource_details');
$this->db->join('location','reso_dtail_location=loc_id');
$this->db->join('go_state', 'go_stste_id = loc_state', 'left');
$this->db->join('go_country', 'num = loc_country', 'left');
$this->db->join('go_dist', 'id = loc_district', 'left');
$this->db->where('loc_id !=1 AND loc_id !=2');
$query = $this->db->get();
//result
$location = $query->result();
but the query does not give right answer after using group_by it only shows 1st record
You can use group by country. So that you can get distinct country name. But states and city you can use group concat.

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