if condition with where clause - php

I just want to do this if(status==0) then only where condition is run and print only related to status==0 record.And when status==1 come then it bypass the where clause and print both data for status 0 and 1. I don't know how to build a query for this. Please help me in this.
my model code
function listing(){
$admin_id = $this->session->userdata('admin_id');
$query = $this->db->select('student_info.*,admin.status')
->from('student_info')
->where('student_info.admin_id',$admin_id)
->join('admin','admin.admin_id = student_info.admin_id')
->get();
return $query->result();
}
This code work properly and print separate data whose status 0 or 1.
Only admin_id = 1 contain Status = 1, other id's contain 0 status.

Replace your function with the following code...
<?php
function listing($status)
{
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
if($status==0)
{
$this->db->where('student_info.admin_id',$admin_id);
}
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$query = $this->db->get();
return $query->result();
}
?>

Try this:
function listing(){
$admin_id = $this->session->userdata('admin_id');
$query = $this->db->select('student_info.*,admin.status')
->from('student_info')
->join('admin','admin.admin_id = student_info.admin_id')
->where("student_info.admin_id = IF (admin.status = 0, $admin_id, '>-1'")
->get();
return $query->result();
}

It solves my problem..
function listing()
{
$admin_id = $this->session->userdata('admin_id');
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$this->db->where('student_info.admin_id',$admin_id);
$this->db->or_where('admin.status < 1');
$query = $this->db->get();
return $query->result();
}

<?php
function listing($status)
{
if($status == 0){
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
$this->db->where('student_info.admin_id',$admin_id);
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$query = $this->db->get();
return $query->result();
}else{
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$query = $this->db->get();
return $query->result();
}
}
?>

Related

How to get all columns

I have a problem in the model, how to get all the columns in the following select query:
public function getAllProvince($where = array())
{
$this->db->select("id, name");
$this->db->from('target_statis');
$this->db->where($where);
$this->db->group_by("id, name");
$this->db->order_by("id");
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->result();
}
return false;
}
Try This,
public function getAllProvince($where = array()){
$this->db->select("*");//changed line
$this->db->from('target_statis');
$this->db->where($where);
$this->db->group_by("id,name");
$this->db->order_by("id");
$query = $this->db->get();
if ($query->num_rows() >0){
return $query->result();
}else{
return array();//changed line
}
}
I'm deleting $this->db->select(column) because you want to get all columns
If you not defining $this->db->select(), codeigniter will default select all the columns
also, I'm deleting $this->db->from() and move the table name to $this->db->get('target_statis');
public function getAllProvince($where = array()){
$this->db->where($where);
$this->db->group_by("id,name");
$this->db->order_by("id");
$query = $this->db->get('target_statis');
if ($query->num_rows() >0){
return $query->result();
}
return FALSE;
}
Try this code...
public function getAllProvince($where = array())
{
$this->db->select("*");
$this->db->from('target_statis');
$this->db->where($where);
$this->db->group_by("id, name");
$this->db->order_by("id");
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->result();
}
return false;
}

Return only one row and modify it before sending from model to view in codeigniter

I have to return only one row from my model to controller, before passing it to controller i have to add some more parameters to it such as given below:
$condition = "id =" . "'" . $id . "'";
$this->db->select('*');
$this->db->from('subject');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
i have want to do something like this while returning it to controller
if ($query->num_rows() == 1) {
$rows[]=$query->result();
$rows['main1']='aaaa';
return $query->result();
} else {
return false;
}
Just grab the row_array and append a value to it like such:
if($query->num_rows() == 1){
$resultArray = $query->row_array();
$resultArray['main1']='aaaa';
return $resultArray;
} else {
return array();
}
Use $query->row() to fetch single rows from database and you can direction write your condition as $this->db->where('id', $id);
$this->db->select('*');
$this->db->from('subject');
$this->db->where('id', $id);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
$res= $query->row();
return $res->column_name;
} else {
return false;
}
In controller
function your_function() {
$this->load->model('your_model');
$res['name'] = $this->your_model->your_function($id);
$this->load->view('your_view',$res);
}

How to get a single value using codeigniter and mysql

i need to get service_price field from db by using this code.
public function get_service_price($id) {
$this->db->select('*');
$this->db->from('service');
$this->db->where('id', $id);
$query = $this->db->get('service');
return $query->row('service_price');
}
here i called this function function.
public function vat($vat=14.5)
{
$id = $this->uri->segment(4);
$data['service_price'] = $this->service_model->get_service_price($id);
$price_with_vat=0;
$price_with_vat = $data + ($vat*($data/100));
//$price_with_vat=78.005;
$price_with_vat = round($price_with_vat, 2);
$this->load->view('admin/service/vat', $price_with_vat);
}
There some mistake. please kindly help me
To get first row use following
$query = $this->db
->where('id', $id);
->get('service');
$firstRow = $query->first_row();
return $firstRow->service_price;
Use this code for your model :
public function get_service_price($id) {
$this->db->select('service_price');
$this->db->from('service');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->row();
}
in model
public function get_service_price($id) {
$query = $this->db->query("SELECT * FROM service WHERE id='$id'");
$result = $query->result_array();
return $result;
}
in controller
$id = $this->uri->segment(4);
$data['service_price'] = $this->service_model->get_service_price($id);
$price_with_vat=0;
$price_with_vat = $data[0]['table_field_name'] + ($vat*($data[0]['table_field_name']/100)); //$price_with_vat=78.005;
$price_with_vat = round($price_with_vat, 2);
$this->load->view('admin/service/vat', $price_with_vat);
in view
foreach ($service_price as $new_service_price)
{
echo $new_service_price['table_field_name'];//exapmle echo $new_service_price['service_price']
}

Can't return data from table - Codeigniter

The below query is only returning one result, even though there are multiple entries in the table with user_is 2. I m not sure what I m doing wrong.
function get_payments($user_id)
{
$sql = "SELECT txn_id, payer_email FROM orders WHERE user_id = ?";
$query = $this->db->query($sql, $user_id);
//echo $this->db->last_query();
if($query->num_rows() != 0) {
foreach($query->result() as $payment)
{
$data[] = $payment;
return $data;
}
}else{
return false;
}
}
This is what $this->db->last_query(); returns. SELECT txn_id, payer_email FROM orders WHERE user_id = '2'
Controller:
$profile_data['user_payments'] = $this->profile_model->get_payments($user_id);
View:
<?php
//var_dump($user_payments);
foreach($user_payments as $payment)
{
echo '<p>';
echo $payment->txn_id;
echo $payment->payer_email;
echo '</p>';
}
?>
try this:
<?php
function get_payments($user_id){
$this->db->select('txn_id, payer_email')
->from('orders')
->where('user_id', $user_id);
$qry = $this->db->get();
$res = $qry->result();
if($res){
return $res;
}else{
return false;
}
}
?>
there is a slight mistake here
foreach($query->result() as $payment)
{
$data[] = $payment;
return $data; // this line returns the data and it stops execution in first loop itself
}
it should be like
if($query->num_rows() != 0) {
foreach($query->result() as $payment)
{
$data[] = $payment;
}
return $data;
}
You should return $data from model after foreach loop completes.
if($query->num_rows() != 0) {
foreach($query->result() as $payment)
{
$data[] = $payment;
}
return $data;
}
else
{
return false;
}
Simple CI way:
...
$query = $this->db->query($sql, $user_id);
return $query->result_array();
No looping needed, nothing.
And I see you're using the object form, in that case do this:
return $query->result();
Then just loop it in your view like you're already doing.
So this is the whole model method:
function get_payments($user_id) {
$sql = "SELECT txn_id, payer_email FROM orders WHERE user_id = ?";
$query = $this->db->query($sql, $user_id);
return $query->result();
}
See the relevant docs here: http://ellislab.com/codeigniter/user-guide/database/results.html
And using Active Records (the most "natural" to CI):
function get_payments($user_id) {
return $this->db
->select('txn_id, payer_email')
->where('user_id', $user_id)
->get('orders')
->result();
}

Codeigniter cannot get data from mysql table

I get following error while trying to get data from my database:
Error Number: 1066
Not unique table/alias: 'faq'
SELECT *FROM (faq, faq)WHERE faq_title = 'title 1'
Please help me to find my mistake. Here is my model:
public function did_get_faq_data($title){
$this->db->select('*');
$this->db->from('faq');
$this->db->where('faq_title', $title);
$query = $this->db->get('faq');
if ($query->num_rows() > 0){
return $query->result();
}
else {
return false;
}
}
In your query table name is called two times. This is unnecessary.
Just replace $query = $this->db->get('faq'); to $query = $this->db->get();
the bold one is correct.
public function did_get_faq_data($title){
$this->db->select('*');
$this->db->from('faq');
$this->db->where('faq_title', $title);
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->result();
}
else {
return false;
}
}

Categories