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
Related
Hello i have 2 tables Student & Faculty
i want to call them to my index(view), So I figured how to join them. but I have a problem, I need to divide these 2 tables into a category but I don't know the code. Can someone please help me?
public function getStudentReguler()
{
return $this->db->table('student')
->join('faculty', 'faculty.id_faculty = student.id_student')
->where('category', *i want to be like, where categor = A*)
->get()->getResultArray();
}
thank you
public function getStudentReguler()
{
return $this->db->table('student')
->join('faculty', 'faculty.id_faculty = student.id_student')
->where('category', 'A')
->get()->getResultArray();
}
Please try this:
$this->db->select('*')
->from('student')
->join('faculty', 'faculty.id_faculty = student.id_student')
->where('category', 'A')
->get()->getResultArray();
I tried to fetch data using joins and the data is repeating,
The controller code is:
public function searchjobs2()
{
//$id=$_SESSION['id'];
$lan = $_POST["picke"]; //var_dump($id);die();
$value['list']=$this->Free_model->get_jobs($lan);//var_dump($value);die();
$this->load->view('free/header');
$this->load->view('free/searchjobs2',$value);
}
And the model:
public function get_jobs($lan)
{
$this->db->select('*');
$this->db->from("tbl_work_stats");
$this->db->join("tbl_work", "tbl_work.login_id = tbl_work_stats.login_id",'inner');
$this->db->where("language LIKE '%$lan%'");
// $this->db->where('tbl_work_stats.login_id',$id);
$this->db->order_by('insertdate','asc');
$query=$this->db->get()->result_array();//var_dump($query);die();
return $query;
}
I have used
foreach ($list as $row){
...
}
for listing.
Using distinct will remove duplicate fields:
$this->db->distinct();
From what I can see, your query has ambiguity, and an error in the join statement, also your where like is part of the problem, I would recommend trying this even do there are some missing info, find out wich field you need to join from the second table.
public function get_jobs($lan){
$this->db->select("tbl_work_stats.*, tbl_work.fields");
$this->db->from("tbl_work_stats");
$this->db->join("tbl_work", "tbl_work_stats.login_id = tbl_work.login_id","inner");
$this->db->where("tbl_work.language LIKE", "%" . $lan . "%" );
$this->db->order_by("tbl_work_stats.insertdate","asc");
$query=$this->db->get()->result_array();
return $query;}
do you mean to join on login_id?
I am guessing that is the user logging in and it is the same for many entries of tbl_work_stats and tbl_work.
you didn't post your schema, , but login_id doesn't seem like right thing to join on. how about something like tbl_work.id = tbl_work_stats.tbl_work_id or similar?
also CI $db returns self, so you can do:
public function get_jobs(string $lan):array
{
return $this->db->select()
->from('tbl_work_stats')
->join('tbl_work','tbl_work.id = tbl_work_stats.work_id')
->like('language',$lan)
->order_by('insertdate')
->get()
->result_array();
}
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.
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";
}
}
I am currently using Codeigniter. As shown below, a query looks up for the count of rows and then sent it to the view. However, the view does not shows the actual count. The actual count on DB (InnoDB) is 1001 but displays up to 1000 on my view. Any ideas?
Model:
public function check_source_email($location){
$query="SELECT COUNT(*) as row_count FROM feedback WHERE location = '$location' AND source_lead = 'Email' ";
$count=$this->db->query($query)->result();
return $count[0]->row_count;
}
Controller:
$data['email'] = $this->Stats->check_source_email($location);
View:
<strong>Email Listing</strong><span class="pull-right"><?php echo $email; ?></span>
Use num_rows:
public function check_source_email($location){
$query = $this->db->get_where('feedback',array('location'=>$location,'source_lead'=>'Email'));
return $query->num_rows();
}
First, I would try and do things the codeigniter way, if you are working with codeigniter:
public function check_source_email($location) {
return $this->db
->where('location', $location)
->where('source_lead', 'Email')
->count_all_results('feedback');
}
That theoretically should return the correct result, although I'm not sure why your original didn't, but maybe this will fix it.