I'm new to Codeigniter and PHP, just learning. I've searched for this but couldn't find the right answer. I have a table that contain some data, lets call it posts and in this table, each post have a category_id. I have another table called categories and in this table I have 2 columns: id and name.
I want to join the two tables that the result I'll get is the same as posts with another column called category_name that will be taken from categories.name. Just can't figure out how to do so.
This is what I was up to so far:
function getPostsWithByCategoryID($numberOfRows, $start, $categoryId)
{
$this->db->select('*');
$this->db->from('posts')->order_by('id','desc')->limit($numberOfRows, $start);
$this->db->where('category_id', $categoryID);
$this->db->join('categories', 'posts.category_id == categories.id');
$query = $this->db->get();
return $query->result_array();
}
Thanks in advance.
========================================================================
EDIT:
After trying Adrian Forsius answer:
function getPostsWithByCategoryID($numberOfRows, $start, $categoryId)
{
$this->db->select('*, category.name AS category_name');
$this->db->from('posts')->order_by('id','desc')->limit($numberOfRows, $start);
$this->db->where('category_id', $categoryID);
$this->db->join('categories', 'posts.category_id = categories.id');
$query = $this->db->get();
return $query->result_array();
}
I'm getting this error:
Error Number: 1054: Unknown column 'category.name' in 'field list'
SELECT *, `category`.`name` AS category_name
FROM (`posts`)
JOIN `categories` ON `posts`.`category_id` = `categories`.`id`
WHERE `category_id` IS NULL
ORDER BY `id` desc
LIMIT 20
====
EDIT 2:
So I found some mistakes in the code and fixed them, tried again, but still I get an error: (The mistakes were using $categoryID instead of $categoryId and select category.name instead of the true name of the table categories which is categories.name
Error Number: 1052: Column 'id' in order clause is ambiguous
SELECT *, `categories`.`name` AS category_name
FROM (`posts`)
JOIN `categories` ON `posts`.`category_id` = `categories`.`id`
WHERE `category_id` = '3'
ORDER BY `id` desc
LIMIT 20
This should do what you are looking for:
function getPostsWithByCategoryID($numberOfRows, $start, $categoryId)
{
$this->db->select('*, categories.name AS category_name');
$this->db->from('posts')->order_by('posts.id','desc')->limit($numberOfRows, $start);
$this->db->where('category_id', $categoryId);
$this->db->join('categories', 'posts.category_id = categories.id');
$query = $this->db->get();
return $query->result_array();
}
replace
$this->db->select('*');
by
$this->db->select('posts.*');
$this->db->select('categories.name','category_name');
CI's documentation is pretty good.
Try this I think you forget to place = in place of ==
function getPostsWithByCategoryID($numberOfRows, $start, $categoryId)
{
$this->db->select('*');
$this->db->from('posts')->order_by('id','desc')->limit($numberOfRows, $start);
$this->db->where('category_id', $categoryID);
$this->db->join('categories', 'posts.category_id = categories.id');
$query = $this->db->get();
return $query->result_array();
}
Related
so i kinda new with codeigniter and im trying to join 3 table from my database
database 1 : dkm (id, tgl, ref, etc)
database 2 : order_product (kode_barang, packing, nama_barang, etc)
database 3 : product (kodeprod, tglpakai, etc)
im already trying what other ppl do to join more than 2 table in codeigniter but i got this error :
Error Number: 1066
Not unique table/alias: 'order_product'
SELECT *
FROM `order_product`
JOIN `order_product` ON `order_product`.`kode_barang` = `dkm`.`id`
JOIN `order_product` ON `order_product`.`kode_barang` = `produksi`.`kodeprod`
This is my code :
Bukaka_model.php
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('order_product','order_product.kode_barang = dkm.id');
$this->db->join('order_product','order_product.kode_barang = produksi.kodeprod');
$query = $this->db->get();
return $query->result();
}
You're trying to join to the same table multiple times, instead you need to join to the other tables once each.
You just need to change the names of the table you're joining to:
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('dkm','order_product.kode_barang = dkm.id');
$this->db->join('produksi','order_product.kode_barang = produksi.kodeprod');
$query = $this->db->get();
return $query->result();
}
Try this,
Here, you have a mistake in joining tables, in CI join() in the first parameter you need to pass/write table name with you want to join
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('dkm','order_product.kode_barang = dkm.id');
$this->db->join('produksi','order_product.kode_barang = produksi.kodeprod');
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}else{
return array();
}
}
Try this:
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('dkm','dkm.id= order_product.kode_barang');
$this->db->join('produksi',' produksi.kodeprod = order_product.kode_barang');
$query = $this->db->get();
return $query->result();
}
Here I have function index which is having two different model calls as follows:
CONTROLLER:
function index()
{
$people = $this->M_results->search_people(); // first model method call
// print_r($people); //shows result
$skills = $this->M_results->get_skill($category_name); // second model method call
}
MODEL:
function search_people()
{
$this->db->select("registration,gender,profile_img,location");
$this->db->from('search_result');
$this->db->join('services','search_result.registration = services.reg_id','left');
$this->db->join('review','search_result.registration = review.reg_id AND review.active = 0','left');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
function get_skill($category_name)
{
$this->db->select('GROUP_CONCAT(skills.id ORDER BY skills.name ASC) as id,GROUP_CONCAT(skills.name ORDER BY skills.name ASC) as skill,sub_categories.name as sub_cat,class');
$this->db->from('skills');
$this->db->join('sub_categories','skills.sub_cat_id = sub_categories.id');
$this->db->join('categories','sub_categories.cat_id = categories.id');
$this->db->where('categories.name',$category_name);
$this->db->where('skills.active',0);
$this->db->group_by('sub_categories.name');
// echo $this->db->get_compiled_select(); exit();
$query = $this->db->get();
return $query->result();
}
Now the problem is that, the call to search_people is returning exact result.
But on running the second model call i.e to get_skill function, the select query of get_skill is including the columns of select query in search_result function. And shows the below database error:
Error Number: 1054
Unknown column 'registration' in 'field list'
SELECT registration, gender, profile_img, location,GROUP_CONCAT(skills.id ORDER BY skills.name ASC) as id, GROUP_CONCAT(skills.name ORDER BY skills.name ASC) as skill, sub_categories.name as sub_cat, class FROM skills JOIN sub_categories ON skills.sub_cat_id = sub_categories.id JOIN categories ON sub_categories.cat_id = categories.id WHERE categories.name = 'Child and Pet Care' AND skills.active =0 GROUP BY sub_categories.name
In the above query you can see both columns in search_people and get_skill.
Posting the answer for future references.
Resets the current Query Builder state. Useful when you want to build a query that can be cancelled under certain conditions.
$this->db->reset_query();
I found the solution from below link:
https://www.codeigniter.com/userguide3/database/query_builder.html#CI_DB_query_builder::reset_query
how to write simple query into codeigniter query using join righ?????
$query = $this->db->query("Select staff_permissions_list.perm_type,staff_permissions_list.permission_key,staff_permissions_list.permission_label,
staff_permissions_list.id, staff_role_permissions.permission_id as p_id,staff_role_permissions.role_id
FROM staff_role_permissions
RIGHT JOIN staff_permissions_list ON staff_role_permissions.permission_id=staff_permissions_list.id
AND staff_role_permissions.role_id=$id WHERE staff_permissions_list.perm_type=0
ORDER BY staff_permissions_list.id ASC
");
if ($query->num_rows() > 0) {
return $query->result_array();
}
$this->db->select('book_id, book_name, author_name, category_name');
$this->db->from('books');
$this->db->join('category', 'category.category_id = books.category_id', 'right');
$query = $this->db->get();
you can get data using this method of right join
How about that ?
$query = $this->db
->select("Select staff_permissions_list.perm_type,staff_permissions_list.permission_key,staff_permissions_list.permission_label,staff_permissions_list.id, staff_role_permissions.permission_id as p_id,staff_role_permissions.role_id")
->from("staff_role_permissions AS srp")
->join("staff_permissions_list AS spl","srp.permission_id = spl.id","right")
->where("spl.perm_type","0")
->where("srp.role_id",$id)
->order_by("spl.id","ASC")
->get();
i put the role_id to the where section - maybe you need to put it back (not sure what you want to achieve here)
I have two tables. voting_ip and country. I want to retrieve results from country table where open_id_fk (foreign key) of voting_ip table is equal to open_id(Primary Key) of country table. How to write sql query to combine these queries and return the result. I am using the below code in my codeigniter model to retrieve number of occurances of open_id_fk in voting_ip table.
public function mostvotedans()
{
$this->db->select('open_id_fk, COUNT(*) as total');
$this->db->group_by('open_id_fk');
$this->db->order_by('total', 'desc');
$query = $this->db->get('voting_ip', 5);
return $query;
$query = $this->db->query("SELECT * FROM country WHERE open_id_fk=open_id;");
return $query;
}
change it as following.
public function mostvotedans()
{
$this->db->select('c.open_id,COUNT(ip.open_id_fk) as total')->from('voting_ip ip');
$this->db->join('country c','c.open_id=ip.open_id_fk');
$this->db->group_by('ip.open_id_fk');
$this->db->order_by('total', 'desc');
$this->db->limit(5);
$query = $this->db->get();
return $query;
}
Use a join statement :
$query = $this->db->select('v.open_id_fk, COUNT(v.*) AS total, c.country_name')
->join('country AS c', 'c.open_id = v.open_id_fk')
->from('voting_ip AS v')
->group_by('v.open_id_fk')
->order_by('total', 'DESC')
->limit(5);
Should work, I put 'country_name' because I don't know your tables.
I am trying to select column name level_name from table levels,
which contains level_id and level_name,
for a user to know what is their level,
The table of users named as users and contain a level_id and user_id,
but I get this error ->
Column 'level_id' in on clause is ambiguous
SELECT `level_name`
FROM `levels`
JOIN `users` ON `level_id` = `level_id` WHERE `user_id` = '9'
here it is the code in the model
public function level_ownprofile($user_id)
{
$this->db->select('level_name');
$this->db->from('levels');
$this->db->join('users', 'level_id = level_id');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query;
}
thanks in advance :)
Change the query to
SELECT `level_name`
FROM `levels` l
JOIN `users` u ON `u`.`level_id` = `l`.`level_id`
WHERE `user_id` = '9'
if you like the table name aliasing method, it is shorter and easier to read.
Or
SELECT `level_name`
FROM `levels`
JOIN `users` ON `users`.`level_id` = `levels`.`level_id`
WHERE `user_id` = '9'
If you prefere to use the full table name everywhere.
Because both tables contain a column with the name level_id the query analyser need to know which one you are addressing.
In codeigniter try
public function level_ownprofile($user_id)
{
$this->db->select('level_name');
$this->db->from('levels l');
$this->db->join('users u', 'u.level_id = l.level_id');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query;
}
Select l.level_name
FROM levels l
JOIN users u
ON u.level_id = l.level_id
and u.user_id = '9'
public function level_ownprofile($user_id)
{
$this->db->select('level_name');
$this->db->from('levels');
$this->db->join('users', 'levels.level_id = users.level_id');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query;
}
public function level_ownprofile($user_id)
{
$this->db->select('l.level_name');
$this->db->from('levels as l');
$this->db->join('users as u', 'l.level_id = u.level_id');
$this->db->where('l.user_id', $user_id);
$query = $this->db->get();
return $query->results();
}