Use multiple where condition in mysql - php

i have a select field named categories[] with multiple select and i written one on change function so on change fuction it will go the controller as an array of id's now what i want is i want to select foods from mysql table with these categories
<select name="categories[]">
<?php
foreach ($category as $c) {
?>
<option value="<?php echo $c->category_id; ?>"><?php echo $c->category_name;?></option>
<?php
}
?>
</select>
controller
public function get_foods(){
$categories = $this->input->post(NULL,true);
$sql = $this->subscription->get_foods($categories);
$result = array(
'result' => $sql
);
echo json_encode($result);
}
and model
public function get_foods($id){
$sql = "SELECT * FROM food_category fc
LEFT JOIN food f ON fc.food_id=f.food_id
WHERE f.food_status = 1 AND
fc.category_id = $id
ORDER BY f.food_id DESC";
$query = $this->db->query($sql);
return $query->result_array();
}
i want to fetch all the foods in these categories, so i think i should have to use some multiple where condition ?

You just need to store category with each food and when you get the foods you can use a where condition :
$this->db->where('category',$category);

use the where columns in () sql function for this.
for detail description on it. find here
http://www.w3schools.com/sql/sql_in.asp

If this can help, I think you need to post the categories one by one, then get it and put it in one array. Here is a code that may help
public function get_foods()
{
$result = array();
foreach($this->input->post('categories') as $c)
{
$result[] = $this->subscription->get_foods($c);
}
echo json_encode($result);
}
and for the model I suggest making use of AR of CI
public function get_foods($category)
{
$this->db->select('*');
$this->db->from('food_category as fc');
$this->db->join('food as f','fc.food_id = f.food_id','left');
$this->db->where('f.food_status', '1');
$this->db->where('fc.category_id',$category);
$query = $this->db->get();
return $query->result_array();
}
First we make an empty array, then using the foreach in post we get all the foods per category then return them inside the result array.
Hope this helps and if there's any error in the sql since I may have forgotten some of the join, I'd be willing to help again.

Related

how to get value from another table with join code igniter

I have 2 tables that joined from 1 database, I need to get "kategori" column from "kategori" table joined with "artikel" table. I can't get the "kategori" colum and it said
ERROR
undefined index:kategori
CONTROLLER
public function edit($id){
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('judul', 'judul', 'required');
$this->form_validation->set_rules('konten','konten','required');
$this->form_validation->set_rules('id_kategori','id_kategori','required');
if($this->form_validation->run()===false){
$data['artikel'] = $this->m_daftar->ambil($id);
$this->load->view('v_daftar_edit',$data);
}else{
$this->m_daftar->update($id);
redirect('/daftar','refresh');
}
}
MODELS
public function ambil($id = FALSE){
if($id===FALSE){
$this->db->select('*');
$this->db->form('artikel');
$this->db->join('kategori', 'kategori.id_kategori = artikel.id_kategori');
$query = $this->db->get();
return $query->result_array();
}
$query = $this->db->get_where('artikel',array('id'=>$id));
return $query->row_array();
}
and this is my view, i need it to show the value when i'm editing a table
VIEW
<select name="id_kategori" id="id_kategori" class="form-control">
<option selected><?php echo $artikel['kategori']; ?></option>
</select>
sorry for my bad english
You need to define which Join you are going to use like left, right, inner, outer
$this->db->join('kategori', 'kategori.id_kategori = artikel.id_kategori');
replace with
$this->db->join('kategori', 'kategori.id_kategori = artikel.id_kategori','left');
Please try this I hope there is problem in your code.
public function ambil($id = FALSE){
$select = ($id === FALSE) ? "artikel.*, kategori.kategori AS kategori" : "*";
$this->db->select($select);
$this->db->form('artikel');
if($id===FALSE){
$this->db->join('kategori', 'artikel.id_kategori = kategori.id_kategori', 'left'); // You need to define which Join you are going to use like left, right, inner, outer
return $this->db->get()->result_array();
}
return $this->db->get_where('artikel',array('id'=>$id))->row_array();
}
If this did not work call my attention let me check another way of solving this problem. Thanks

Cannot get data after joining from multiple tables in Codeigniter

I want to get data from two tables, the second table is for rating so i want to get rating of products simultaneosly. Below code is not working for me if i change
$this->db->select('dg_products.',', AVG(dg_rating.rating) As
averageRating');
to
$this->db->select('*');
then it is working.
Please help to sort out my issue.
public function get_rating()
{
$this->db->select('dg_products.*','*, AVG(`dg_rating.rating`) As averageRating');
$this->db->from('dg_products');
$this->db->join('dg_rating', 'dg_products.id = dg_rating.product_id','left');
$this->db->where('dg_products.is_featured_prod','1');
$this->db->group_by("dg_products.id");
$query = $this->db->get();
$result = $query->result();
return $result;
}
Try it like this :
$this->db->select('dg_products.*, AVG(`dg_rating.rating`) As averageRating');
you just have an unneeded quotes in there.

Codeigniter Count Query

How do I structure the below query in Codeigniter. I can't seem to figure it out.
This is the function in my model:
public function update_daily_inventory(){
foreach ($this->parseInventoryHtml() as $item)
{
$_item = $this->db->query('SELECT COUNT(*) as `count` FROM product WHERE product_code = "'.$item['sku'].'"');
}
return $_item;
}
I want to use an array key as the where variable. Apologize if this is simple I am new to coding. This keeps returning 0.
$this->db->where('product_code', $item['sku']);
$this->db->select('COUNT(*) as `count`');
$_item = $this->db->get('product')->row()->count;
There is no need to set alias for count.here is simple query to count number of rows found in result.
$this->db->where('product_code', $item['sku']);
$this->db->select('*');
$_item = $this->db->get('product')->num_rows();
You can use below code to structure your query.
$this->db->select('*');
$this->db->where('product_code', $item['sku']);
$_item = $this->db->count_all_results('product');
Hope this helps.

Only last row getting displayed in view in Codeigniter

I'm trying to fetch certain values from and then pass it to another model in the same control.
However I'm only able to display the last row in the view.
I have shared my code below and I'm not sure where I'm going wrong.
Controller:
public function test($id){
$mapping_details = $this->queue_model->get_mapping_details($id);
foreach ($mapping_details as $value) {
$data['agent_details'] = array($this->agent_model->get_agent_details($value['user_id']));
}
$this->load->view('app/admin_console/agent_queue_mapping_view', $data);
}
Model:
public function get_agent_details($id) {
$query = "select * from user_table where id = ".$id." and company_id = ".$this->session->userdata('user_comp_id');
$res = $this->db->query($query);
return $res->result_array();
}
Welcome to StackOverflow. The problem is the iteration in your controller. You are iterating through the $mapping_details results and per every iteration you are re-assigning the value to $data['agent_details'] , thus losing the last stored information. What you need to do is push to an array, like this:
foreach ($mapping_details as $value) {
$data['agent_details'][] = $this->agent_model->get_agent_details($value['user_id']);
}
However, wouldn't it be best if you created a query that uses JOIN to get the related information from the database? This will be a more efficient way of creating your query, and will stop you from iterating and calling that get_agent_details() over and over again. Think of speed. To do this, you would create a model method that looks something like this (this is just an example):
public function get_mapping_details_with_users($id){
$this->db->select('*');
$this->db->from('mapping_details_table as m');
$this->db->join('user_table as u', 'u.id=m.user_id');
$this->db->where('m.id', $id);
$this->db->where('u.company_id', $this->session->userdata('user_comp_id'));
return $this->db->get()->result();
}
Then your controller will only need to get that model result and send it to the view:
public function test($id){
$data['details_w_users'] = $this->queue_model->get_mapping_details_with_users($id);
$this->load->view('app/admin_console/agent_queue_mapping_view', $data);
}
Hope this helps. :)

Codeigniter selecting a certain table from database

I just want to ask if you can select a certain table in the database? There is this code in my
model:
public function select_tables(){
$tables = $this->db->list_tables();
return $tables;
}
and in my controller:
public function maintenance(){
$this->data['title'] = "Inventory Maintenance";
$this->load->vars($this->data);
$this->load->view('homeview');
$selecttable['tablename'] = $this->inventory_model->select_tables();
$this->load->view('maintenance_view', $selecttable);
$this->load->view('footer_view');
}
Here is the printscreen in my view:
There is the list of my tables in the database, what I want is I can only show limited tables, for example I just want to show "tbladditional, tblemployees, tblinventorytype". Is there a syntax where I can select a certain table? An equivalent to this syntax
"Select * from 'tablename' where 'tablename' = 'something'"
It's so confusing so please I really need your help. Thank you so much!!
public function select_tables(){
$tables = $this->db->list_tables();
$req_tables = array("tble1", "table2"); //pass your table names as Array
$tables = array_intersect($tables, $req_tables);
return $tables;
}
This is the programmatic way I can come with. As CI doesn't have any method to retrieve specific table names.
you can put a mixture of Codeigniter and the query result
For example:
$table_list = $this->db->list_tables();
foreach($table_list as $tl)
{
if(strpos(strtolower($tl),'tbladditional')==true ||
strpos(strtolower($tl),'tblemployees')==true ||
strpos(strtolower($tl),'tblinventorytype')==true)
{
$query = "Select * from $tl";
}
}

Categories