How can I get the all value in one column using result() - php

Model:
function get_all() {
$query = $this->db->get('loan');
return $query->result();
}
function get_loan_name($code) {
$this->db->where('loan_type_code',$code);
$query = $this->db->get("loan_type");
return $query->row();
}
Controller:
function index()
{
$this->data['loans'] = $this->loan_approval->get_all();
$this->build_view("loan_approval/list.php");
}
In "LOAN TABLE" it have a "loan_type_code" column i want to get all the value in this column and match to 2nd function.
Example
Loan table Loan Type Table
ID loan_type ID loan_type_code loan_type_name
1 EDL 1 EDL Education Loan
2 DF 2 DF Damayan Fund
I need to match the "loan_type" in table "LOAN" to "loan_type_code" in "Loan Type" table.
Im thinking if of query like this "$this->controler->get_loan_name($this->data['loans']->loan_type);" but its not working.

Why not doing it at DB level using a JOIN?
$query = $this->db->select('loan.*, loan_type.loan_type_name')
->from('loan')
->join('loan_type','loan.loan_type = loan_type.loan_type_code')
->get();
return $query->result();

Related

Where condition based on two database fields

I do have two tables which are employee and loan
When I enter loan for particular user I am picking user employee_id and loan amount.
// loan form
https://imgur.com/VH6hflw
these details stores in loan table. As well as I have stored some employees details already in employee table like account number, employee official id, userpic.
When I submitting the loan form I am getting employee unique id. I will store the employee unique id to loan field called loan_employee_id.
My problem is When I retrieve the data from the loan table I will get loan amount but some more field are needs from employee table. So I have joined with two tables which are employee and loan.
my db structure https://imgur.com/XYxGjVH
public function fetchLoan()
{
$this->db->select('*');
$this->db->from('employee');
$this->db->join('loan','employee.emp_id = loan.employee_loan_id');
$this->db->where(array('loan.employee_loan_id' => "employee.emp_id") );
$query= $this->db->get();
return $query->result_array();
}
Here where condition should match with employee_loan_id from the loan table and emp_id from employee table
How Do i join the tables with above condition, where conditions both valion getting from the database.
First of all, the corresponding MySQL query would be:
SELECT * -- Put field names which are required
FROM loan
INNER JOIN employee ON employee.emp_id = loan.employee_loan_id
WHERE loan.employee_loan_id = 12; -- Consider we're fetching records for emp_id = 12
Corresponding code would be:
-- Pass a parameter for employee id for which you want to get loan details.
public function fetchLoan($empId)
{
$this->db->select('*');
$this->db->from('employee');
$this->db->join('loan', 'employee.emp_id = loan.employee_loan_id');
$this->db->where('employee.emp_id', $empId);
$query = $this->db->get();
return $query->result_array();
}
Please pass employe_unique_id on this based data geting
public function fetchLoan($employe_unique_id)
{
$this->db->select('*');
$this->db->from('employee');
$this->db->join('loan','employee.id = loan.employee_id');
$this->db->where('employee.id',$employe_unique_id);
$query= $this->db->get();
return $query->result_array();
}

Show data record using id in codeigniter

i got this error message
Column 'id_siswa' in where clause is ambiguous
SELECT * FROM `siswa` `a`
LEFT JOIN `pembayaran_spp` `b` ON `b`.`id_siswa`=`a`.`id_siswa`
WHERE `id_siswa` = '7%E2%80%8B'
I have 2 table.
1.table 'siswa'
structure(id_siswa,nama_siswa,id_tahun_masuk)
2.table 'pembayaran_spp'->
structure(id_pembayaran,id_siswa,jml_pembayaran,id_tahun,date)
I want to show data 'pembayaran_spp' by id_siswa.
so when i click detail on 'siswa', data 'pembayaran' is showed by id_siswa.
My Controller
function detailtagihan($id_siswa)
{
$data['siswa'] = $this->M_keuangan->tagihansiswa($id_siswa);
$this->load->view('template/header');
$this->load->view('template/sidebar');
$this->load->view('keuangan/v_detailtagihan',$data);
$this->load->view('template/footer');
}
My Model
function tagihansiswa($id_siswa)
{
//$data = array('id_siswa' => $id_siswa );
$this->db->select('*');
$this->db->from('siswa a');
$this->db->join('pembayaran_spp b','b.id_siswa=a.id_siswa', 'left');
$this->db->where('id_siswa',$id_siswa);
$query = $this->db->get();
if($query->num_rows()>0)
return $query->result();
}
You should mention of which table you want to use the column id_siswa in your where clause. As both of the tables are having a column with same name, you are getting this error.
If you want to use siswa then in your where condition write a.id_siswa
And if you want to use pembayaran_spp then in your where condition b.id_siswa.

How to get count with joins in CodeIgniter

I am joining 3 tables and fetching data from database. But my problem is that I have to use 2 more tables to query to fetch a number of likes and numbers of comments on a post.
The query I am using is:
function GetHomeDeals($limit,$start)
{
$this->db->from('tbl_coupons');
$this->db->where('coupon_status', 'active');
$this->db->join('tbl_stores','tbl_stores.store_id=tbl_coupons.coupon_store');
$this->db->join('tbl_users','tbl_users.user_id=tbl_coupons.coupon_postedby');
$this->db->limit( $start,$limit);
$this->db->order_by("coupon_id", "desc");
$query = $this->db->get();
//echo $this->db->last_query();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
table structure of likes tables is:
like_id
like_by
like_on
table structure for comments
comment_id
comment_by
comment_on
comment
How can i add a count of likes and comments my function?
I just want total number of likes and count in result
like_on= coupon_id
comment_on=coupon_id
use this:
$query->num_rows();
example:
$query = $this->db->get('table');
$num = $query->num_rows();

SQL selecting from two tables returns duplicate values - Codeigniter

I have to retrieve some all data from two tables. There is no condition. But my code returns duplicated values of data.
each table contain 4 rows
Centres tbl:
Training Course tbl:
Output:
My Controller code:
$this->load->model("admindata");
$data ['query'] = $this->admindata->getcentrelist();
$this->load->helper('url');
$this->load->view('admin/header');
$this->load->view('admin/training',$data);
$this->load->view('admin/footer');
My query on Model is:
public function getcentrelist()
{
$this->load->database();
$query= $this->db->query('SELECT centre_name,course_name from tbl_training_courses, tbl_traning_centres');
return $query->result();
}
View: (training.php)
<?php foreach($query as $row): ?>
<tr>
<td><?php echo $row->centre_name; ?></td>
</tr>
<?php endforeach; ?>
This is normal because of the implicit join you made! Why don't you simply request two queries, one per table, and then merge the results in one array?
public function getcentrelist()
{
$this->load->database();
$query_courses= $this->db->query('SELECT course_name as name from tbl_training_courses');
$query_centers= $this->db->query('SELECT centre_name as name from tbl_traning_centres');
$courses = $query_courses->result();
$centers = $query_centers->result();
return array_merge($courses,$centers);
}
Note please that I have renamed the fields course_name and center name to a unified name: "name", this is necessary to have a coherent merged table.
Use DISTINCT
public function getcentrelist()
{
$this->load->database();
$query= $this->db->query('SELECT DISTINCT centre_name,course_name from tbl_training_courses, tbl_traning_centres GROUP BY center_name');
return $query->result();
}
or use Group By
public function getcentrelist()
{
$this->load->database();
$query= $this->db->query('SELECT centre_name,course_name from tbl_training_courses, tbl_traning_centres GROUP BY center_name');
return $query->result();
}
The data returns by your query is normal because you select One line and you fetch all lines of the second table so if you have 10 lines in the second line you will get each value 10 times and if you even put distinct you will get a problem. To avoid such a problem you need to use condition or Join between two tables.

Codeigniter getting data by passing comma separated values

Here is my application details:
In mysql database, I have products table with id, description and price. And in basket table, I have products_id in the form 1,2,3 i.e. comma separated values.
Now, in controller, I got the product_id list from basket table by this function:
$data['baskets'] = $this->baskets_model->get_all();
$pr_id=$this->baskets_model->get_all();
foreach ($pr_id as $pr){
$get_pr_info=$this->products_model->get_all($pr->product_id);
}
I have this function in products_model model:
public function get_all($ids=NULL) {
if($ids !== NULL) {
echo $ids;
}
else {
$query = $this->db->get('products');
return $query->result_array();
}
}
The value $ids echoes the products_id as 1,2,3. Now, how to get the details of each product by passing this products_id from product table and show in basket? Please drop comment if my question is not clear.
Hope this may help you
public function get_all($ids=NULL)
{
$this->db->from('products');
if($ids !== NULL)
{
$this->db->where("id in ($ids)");//use this
//$this->db->where_in('id',$ids);//This may work but I think it will not give your right answer
//if your id like 1,2,3. this will not work i think but if you send only one id it will work.
}
$query = $this->db->get();
return $query->result_array();
}
Add where condition in to the database query i.e.
$this->db->where_in('your_column_name',$ids);

Categories