I use this code to display data according to the user who is logged in, and display their data.
but with this syntax the data that appears does not match the defined data, and also other user data sometimes appears too.
$id=$this->session->userdata('ses_id');
$this->db->SELECT('*');
$this->db->from('doc_priv_std');
$this->db->join('acc',' doc_priv_std.id_acc=acc.id_acc');
$this->db->where('doc_priv_std.id_acc ',$id,('and
type_doc LIKE "%.doc%"
or "%.docx%"
or "%.pdf"
or "%.xls%"
or "%.xlsx%"
or "%.ppt%"
or "%.pptx%"
or "%.zip%"
or "%.rar%"'));
$query = $this->db->get();
return $query->result_array();
please help so that the data that appears as defined.
Your syntax is wrong - you can't do this with your way - you have to organise your Query properly - try the following
$id=$this->session->userdata('ses_id');
$arrExtension = ['.docx', '.pdf', '.xls', '.xlsx', '.ppt', '.pptx', '.zip', '.rar'];
$this->db
->select('*')
->from('doc_priv_std')
->join('acc',' doc_priv_std.id_acc=acc.id_acc')
->where('acc.id_acc', $id)
->group_start();
foreach($arrExtension AS $strExtension)
{
$this->db->or_like('type_doc', $strExtension);
}
$query = $this->db->group_end()->get();
return $query->result_array();
Use var_dump($this->db->last_query()); before your return to see the db query you are actually running to help with debugging, but it could be that you where statement is wrong? Try this instead:
$where = 'WHERE type_doc LIKE "%.doc%"
or "%.docx%"
or "%.pdf"
or "%.xls%"
or "%.xlsx%"
or "%.ppt%"
or "%.pptx%"
or "%.zip%"
or "%.rar%"';
$this->db->select('*')
->join('acc',' doc_priv_std.id_acc=acc.id_acc')
->where('doc_priv_std.id_acc', $id)
->where($where);
$query = $this->db->get();
return $query->num_rows() > 0 ? $query->result_array() : FALSE;
Related
is there a way to order codeigniter select query by a specific value?
I know that it can be done in mysql from this answer, but I'm wondering if there is a "codeigniter" way of doing it, here is what I've tried:
$this->db->select('...');
$this->db->from('table_one');
$this->db->join('table_two', 'table_one.some_id = table_two.id', 'inner');
$this->db->where('city',$city);
// this gives me error
$this->db->order_by("table_two.id=$id", "desc");
$query = $this->db->get();
return $query->result_array();
This gives me the unknown column error.
yes this is possible, but you need quotes:
$this->db->order_by("table_two.id='$id'", "desc");
hint: you can always doublecheck your query like this:
echo $this->db->last_query();die;
I am working on one Join query in CodeIgniter. Query run perfectly fine.
The problem is $query->num_rows().This is how I write my join query.
$this->db->select('related colums');
$this->db->from('table1 as t1');
$this->db->join('table2 as t2', 't1.id = t2.id', 'left outer');
$this->db->join('table3 as t3', 't1.id_second = t3.id', 'left outer');
$this->db->where('t1.column', $some_varibale);
$this->db->order_by("t1.column", "desc");
$query = $this->db->get();
$query_result = $query->result_array();
$number_of_row = $query->num_rows(); // This line of code is not working properly.
print_r($number_of_row);
// To check result is empty or not.
if(!empty($query_result)){
echo 'not empty';
} else {
echo "empty";
}
The problem:
When I print the $number_of_row it gives me 13 but when I print $query_result it will show only one row which is correct result.(I have double checked it)
so the problem is I was expecting that $query->num_rows() will return me 1 if I get only one row in the result.
I have the cross check it when I get an empty result then it will show 0 as expected. but as described before when I get one row in result it will show 13 number.
I am aware of count and it will work but the question is why this $query->num_rows() not working correctly?
I didn't get that what am I doing wrong?
Try this:
$number_of_row = $this->db->affected_rows();
Let me know if that works
I am trying to join two tables and return an array in my Model method in CodeIgniter with php. I've gone through a few previously posted similar questions on stackoverflow and modified my code accordingly. But they don't seem to work. Hence would love to know what's wrong with the following.
I'm using the following method but am currently getting exceptions. Would appreciate suggestions in this regard.
Model Method
public function getUserDetails($username)
{
$uid = $this->getUserUid($username);
$this->db->select('s.uid, s.name,s.phone, s.studentId, s.type, sp.profiledesc, sp.age');
$this->db->from('sysuser as s');
$this->db->join('studentprofile as sp', 's.uid = sp.uid', 'left');
$this->db->where(array('s.uid' => $uid));
$query = $this->db->get();
return $query->result();
}
Controller
$data1['details'] = $this->userModel->getUserDetails($username);
$this->load->view('studentDashboard/viewProfile',$data1);
View
...
<h2>
<?php foreach($details as $detail){?>
<?php echo $detail->s.name;?>
<?php }?>
</h2>
...
In the view, I've also tried just echoing $detail->name but this doesn't work either.
At first, use print_r($details) for checking your data. If it's returning anything or not.
Then echo your value like this $detail['name']
Fixed Code:
public function getUserDetails($username)
{
$uid = $this->getUserUid($username);
$this->db->select("*");
$this->db->from('sysuser');
$this->db->join('studentprofile', 'studentprofile.uid = sysuser.uid');
$this->db->where('sysuser.uid',$uid);
$query = $this->db->get();
return $query->result();
}
Look, i´m not sure that i understood your code, what means this line
$uid = $this->getUserUid($username);
You´re calling a method and sending the name to retrieve the userid, right?
I´ll write that method like you should have it:
public function getUserid($user){
$this->where->id($user);
return $this->get('whatever table')->row();
//i think you forgot this ->row()
}
then
public function getUserDetails($username)
{
$uid = $this->getUserUid($username);
//here already you should bring with ->row() already
//you can use this var_dump here to confirm too
//var_dump($uid);
//exit;
$this->db->select('s.uid, s.name,s.phone, s.studentId, s.type, sp.profiledesc, sp.age');
//line´s right
//the from method is disposable, so i put it into the get but here it´s right too
$this->db->join('studentprofile as sp', 's.uid = sp.uid', 'left'); //ok
$this->db->where($uid); //this line is the wronger, i´ve made the fix
//as this is an where, you´d bring only one value with ->row() from that other method
$query = $this->db->get('sysuser as s');
//the 'from' i putted here, just to write a line less
return $query->result();
when you need to test what you´re returng do a var_dump here
//commenting the return above
//$test = $query->result();
//var_dump($test);
}
I am trying to write this below query in codeigniter format and getting some problem with the aggregate function:
$stmt = "select sum(subscription_amt) as samt, bill_month,
sum(loan_refund_amt*no_of_loan_installment+error_amt) as lamt
from pf_bill_det
where trim(pf_number)='$pfno'
and fin_year='$fyear'
and aproved='Y' group by bill_month";
$query = $this->db->query($stmt);
This query ending up with an error loan_refund_amt*no_of_loan_installment+error_amt is not a column. Please help me how to write this query using codeigniter query format.
why don't you try this
$this->db->select("COUNT(*) AS MyCount");
$this->db->from("MyTable");
$this->db->where("field", $value);
$this->db->get();
OR
$this->db->select("SUM(field_name) AS MySum");
$this->db->from("MyTable");
$this->db->where("field", $value);
$this->db->get();
OR
$this->db->select("SUM(field_name) AS MySum, username, password");
$this->db->from("MyTable");
$this->db->where("field", $value);
$this->db->get();
BUT
In simple query function you can use
$query = $this->db->query("SELECT COUNT(field_name) AS total_names, fname");
$query->result(); \\ Returns an array of objects
$query->result_array(); \\ Returns result as a pure array
$query->row(); \\ Returns a single result and first row
Try this,
$query=$this->db->query("select sum(subscription_amt) as samt,bill_month,sum(`loan_refund_amt`*`no_of_loan_installment`+`error_amt`) as lamt from pf_bill_det where trim(pf_number)='$pfno' and fin_year='$fyear' and aproved='Y' group by bill_month");
I have piece of PHP code written in Codeigniter framework that returns nothing (an empty set).
Have a look at it and tell me what is wrong with it.
function sbsn($serial){
$this->db->select('asset_types.name as type_name,asset_brands.name as brand_name');
$this->db->from('asset_types,asset_brands,assets');
$this->db->where('assets.type_code','asset_types.code');
$this->db->where('assets.brand_code','asset_brands.code');
$this->db->where('serial_no',$serial);
$result = $this->db->get();
return $result;
}
hi with db get you are only getting result set not record to get results from result you have to the following thing
$this->db->get()->result(); // will return an array of objects
$this->db->get()->result_array(); //will return result in pure array
$this->db->get()->row() // just single row as object
$this->db->get()->row_array() // just single row as array
you can use $result to perform the same above things
$result->result();
$result->row();
for more information read generating result user guide
change:
$result = $this->db->get();
to
$result = $this->db->get()->row_array(); //to get single row
//OR
$result = $this->db->get()->result_array(); //to get multiple rows
OR try using JOIN, like
$this->db->select('asset_types.name as type_name,asset_brands.name as brand_name');
$this->db->from('assets');
$this->db->join('asset_types', 'asset_types.code = assets.type_code', 'left');
$this->db->join('asset_brands', 'asset_brands.code = assets.brand_code', 'left');
$this->db->where('assets.serial_no',$serial);
$result = $this->db->get()->result_array();
return $result;
The problem can be easily solved by joining the tables in the following way.
$this->db->select('at.name as type_name,ab.name as brand_name');
$this->db->from('asset_types as at,asset_brands as ab');
$this->db->join('assets as a', 'a.type_code = at.code and a.brand_code as ab.code');
$this->db->where('a.serial_no',$serial);
$result = $this->db->get()->result_array();
return $result;