I am working in codeigniter.I have created one function in model.The function is like this :
function pr($id)
{
//$query5 = $this->db->query("select max(to_date) as last_date from agent_print_details where agent_id = ".$id);
//$result5 = $query5->result();
$this->db->select('max(to_date) as last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id',$id);
$result = $this->db->get();
if($result->num_rows()>0)
return $result->result();
else
return "empty";
}
my controller is :
$pr_detail = $this->cashier1_model->pr($role['id']);
if($pr_detail != 0)
{
echo "nisarg";
}
else
{
echo "123";
}
when I print pr_detail then it will display output like this:
Array ( [0] => stdClass Object ( [last_date] => ) )
it will give blank data so it has to print 123 but it is display nisarg.
So What should i have to do to print 123?
If result not found just return FALSE in model file. Also use CI select_max to get max value
MODEL
$this->db->select_max('to_date','last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id', $id);
$result = $this->db->get();
if ($result->num_rows() > 0) {
return $result->result();
} else {
return FALSE;
}
CONTROLLER
$pr_detail = $this->cashier1_model->pr($role['id']);
if($pr_detail)
{
echo "nisarg";
}
else
{
echo "123";
}
you could use count function, also, do you have defined a table name in get function?
...
$result = $this->db->get('agent_print_details');
//if($result->num_rows() > 0)
if(count($result) > 0)
...
When you use $result->num_rows() > 0 you CANT use $result->result(). If u want to return these results, you need to check if its not empty first, and then query again to get results.
$this->db->select_max('to_date','last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id', $id);
$result = $this->db->get();
if ($result->num_rows() > 0) {
$this->db->select_max('to_date','last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id', $id);
$result = $this->db->get();
return $result->result();
} else {
return FALSE;
}
Related
I have the following function in my model to filter officer records user wise. The function is working fine.
public function getOfficer()
{
$q = $this->db->order_by('last_name','ASC')->get_where('tbl_officer', array('status' => '1', 'usr' =>$this->session->userdata('id_user')));
if ($q->num_rows() > 0) {
return $q->result();
}
return false;
}
Further, I want to filter officer records only for the user = 4 in the session, by p_code->8,10 and 24
The If functions as follows :
if($usr == 4)
{
$this->datatables->where('tbl_officer.p_code', 8)->or_where('tbl_officer.p_code', 10)->or_where('tbl_officer.p_code', 24);
} else {
$this->datatables->where('tbl_officer.usr', $usr);
}
How can I include this If condition into my model function mentioned above ? What can be changed ? Can anyone help ?
PLease use where_in
$this->datatables->where('tbl_officer.p_code', 8)->or_where('tbl_officer.p_code', 10)->or_where('tbl_officer.p_code', 24);
to
$this->datatables->where_in('tbl_officer.p_code', [8,10,24]);
Just need to change the method of query writing
public function getOfficer()
{
$usr = $this->session->userdata('id_user');
$userArray = array(8,10,24);
$this->db->select('*');
$this->db->from('tbl_officer');
if($usr == 4){
$this->db->where_in('usr',$userArray );
}else{
$this->db->where('usr',$usr);
}
$q = $this->db->get();
if ($q->num_rows() > 0) {
return $q->result();
}
return false;
}
I am working with rest api using codeigniter,I want to fetch records + total records + per page records,but not worked for me
Here is my function in controller
<?php
public function search_shop()
{
$users['rec'] = $this->Model_users->find_shop($_POST);
if($users['rec']!="")
{
$responseJSON = array("Status" => true,"Result" => $users['rec']);
header("content-type:application/json");
$response = json_encode($responseJSON);
echo $response;
}
else
{
$responseJSON = array("Status" => false,"Message" => "There is no matching record");
header("content-type:application/json");
$response = json_encode($responseJSON);
echo $response;
}
}
And here is my "find_shop" function in model,How can i fetch all records with total number of records and per page records ?
public function find_shop()
{
$add_data['page_number'] = ($this->input->post('page_number') && !empty($this->input->post('page_number'))) ? $this->input->post('page_number') : NULL;
$add_data['search'] = ($this->input->post('search') && !empty($this->input->post('search'))) ? $this->input->post('search') : NULL;
$records="10";
$mins="10";
if($add_data['page_number']=="" || $add_data['page_number']=="0" || $add_data['page_number']=="1")
{
$starting_row="0";
$last_row="10";
}
else
{
$last_row="9";
$cd="1";
$lim="10";
$starting_row=$add_data['page_number']*$lim-$last_row-$cd;
$last_row=$add_data['page_number']*$records-$cd;
}
$this->db->select('*');
$this->db->from('shop s');
$this->db->like('shop_name',$add_data['search']);
$this->db->or_like('city',$add_data['search']);
$this->db->limit($last_row,$starting_row);
$query = $this->db->get();
if ( $query->num_rows() > 0 )
{
$row = $query->result_array();
return $row;
return $query->num_rows;
}
else
{
return false;
}
}
?>
Try This, Hope it helps
in the Controller please test this after model related changes
$users = array();
$users = $this->Model_users->find_shop($_POST);
echo'<pre>';print_r($users);die;
in the Model, There is a mistake in the return, You cannot do two return at the same time
$query = $this->db->get();
if ( $query->num_rows() > 0 )
{
$data = array();
$data['row'] = $query->result_array();
$data['total_records'] = $this->db->count_all_results('shop');
$data['per_page_records'] = $query->num_rows();
return $data;
}else{
return array('row'=>array(),'total_records'=>'0','per_page_records'=>'0');
}
Change your if condition like this:
$records="10"; // This is records per page showing
if ($add_data['page_number']=="" || $add_data['page_number']=="0" || $add_data['page_number']=="1")
{
$starting_row="0";
} else {
$starting_row = ($add_data['page_number'] - 1) * $records;
}
You need to set limit and offset of query. For example: limit 0, 10: this will show first 10 records. Here 0 is starting point and 10 is the length of records which you want to show. 10 will always be same as you are showing 10 records. Limit 10, 10: it will show records from 10 to 19. hope it clears your doubt.
I want to loop through the results of a query and then compare each results to an input field, if the value is repeated it must return false if not the result is true, I know that I could use unique index in database to avoid repeated values and then catch the error, but in this case repeated values are possible but they can´t both be "active" there a column called "status" with two values A or I (active and inactive) so "clabe" value can be repeated only when one is active and the other inactive.
Controller:
function agregar()
{
$this->load->helper('date');
date_default_timezone_set('America/Mexico_City');
$now = date('Y-m-d H:i:s');
$Interno = $this->input->post('intern');
$clabe = $this->input->post('clabe2') . $this->input->post('control2');
$cve_banco = $this->input->post('cve_banco');
$Banco = $this->input->post('Banco2');
$Observaciones = $this->input->post('Observaciones');
$data = array(
'Interno' => $Interno,
'clabe' => $clabe,
'cve_banco' => $cve_banco,
'Banco' => $Banco,
'Observaciones' => $Observaciones,
'Fecha_alta' => $now,
);
if($this->consultas_M->insert($data) == true){
//redirect('Inicio/busqueda', 'refresh');
} else{
echo "Hubo un problema al insertar";
}
}
Model:
function insert($data)
{
$clabe = $this->input->post('clabe2') . $this->input->post('control2');
$this->db->select('Clabe');
$this->db->where('Status =', 'A');
$query= $this->db ->get('cuentas');
return $query->row();
foreach ($query as $row) {
$i = $row;
if ($clabe = $i) {
return false;
} else {
$this-> db -> insert('cuentas', $data);
if ($this->db->affected_rows() > 0) {
return true;
}
}
}
}
Why would you want to have possible duplicated index ? If you want to have different rows with a common "index" for some reason you can create a column called index or something and add in your WHERE clause that index.
PS: in your model in the foreach loop's if condition use :
if ($clabe == $row){...
instead of
$i = $row
if ($clabe = $i){...
I solved myself the issue, i´m posting in in case it helps anybody, it was easier than I thought.
function insert($data, $clabe)
{
$Status = 'A';
$clabe = $this->input->post('clabe2') . $this->input->post('control2');
$this->db->select('Clabe');
$this->db->where('Status =', $Status);
$this->db->where('Clabe =', $clabe);
$q= $this->db ->get('cuentas');
if($q -> num_rows() > 0){
return false;
}else{
$this-> db -> insert('cuentas', $data);
return true;
}
And this is the controller:
if($this->consultas_M->insert($data, $clabe) == true){
redirect('Inicio/busqueda', 'refresh');
} else{
echo "There was a problem";
$this->load->view('errors/error');
}
The only thing I needed and wasn´t doing was to select the "clabe" I wanted and then compare it with the input where user writes this "clabe". And then just a simple if-else statement.
function insert($data)
{
$Status = 'A';
$clabe = $this->input->post('clabe2') . $this->input->post('control2');
$this->db->select('Clabe');
$this->db->where('Status', $Status);
$query= $this->db ->get('cuentas');
$flag=true;
foreach ($query as $row) {
$i = $row;
if ($clabe = $i){
$flag=false;
}
}
if($flag){
$this-> db -> insert('cuentas', $data);
if ($this->db->affected_rows() > 0) {
$flag= true;
}
}
return $flag;
}
Your code will add it on first else block ad return. Instead it should check all the rows first to decide.
I am using Codeigniter to fetch rows. Please go through the below code.
//Model
public function selectData() {
$query = $this->db->query('SELECT * FROM tbl_favourites WHERE favourites_status = 1');
$count = $query->num_rows();
if($count > 0)
return $result = $query->result_array();
else
return 0;
}
//Controller
$all_favourites = $this->select->selectData();
if(!empty($all_favourites)) {
foreach($all_favourites as $favourites)
echo json_encode($favourites);
}
From the above code, the output is
{"favourite_online_id":"3","favourite_online_url":"ddd","favourite_online_status":"0"}
{"favourite_online_id":"2","favourite_online_url":"http:\/\/www.google.com","favourite_online_status":"0"}
But I want the output to be formatted properly to satisfy the android JSON.
The required output is,
[
{
"favourite_online_id": "3",
"favourite_online_url": "ddd",
"favourite_online_status": "0"
},
{
"favourite_online_id": "2",
"favourite_online_url": "http://www.google.com",
"favourite_online_status": "0"
}
]
instead of using
if(!empty($all_favourites)) {
foreach($all_favourites as $favourites)
echo json_encode($favourites);
}
use directly
if(!empty($all_favourites)) {
echo json_encode($all_favourites );
}
I think you can modify your function
public function selectData() {
$query = $this->db->query('SELECT * FROM tbl_favourites WHERE favourites_status = 1');
$count = $query->num_rows();
if($count > 0)
return $result = $query->result_array();
else
return array();
}
//Controller
$all_favourites = $this->select->selectData();
// if(!empty($all_favourites)) {
// foreach($all_favourites as $favourites)
// echo json_encode($favourites);
// }
echo json_encode($all_favourites);
You can do in 2 way
1st way
if(!empty($all_favourites)) {
echo json_encode($all_favourites);
}
2nd way
if you want to add something in that object then
$favouriteArray=[];
if(!empty($all_favourites)) {
foreach($all_favourites as $favourites){
array_push($favouriteArray, $favourites);
}
}
echo json_encode($favouriteArray);
I'm using Codeigniter active records for some time now and this is the most ridiculous error I've got.
My query is
$this->db->select('*');
$this->db->from('my_table');
$this->db->where('is_active', 1);
$query = $this->db->get();
if ($query->num_rows() > 0) {
//return $query->result();
return $query;
} else {
return FALSE;
}
when I use$query->result(); It is empty. When I use return $query;
the result is like below,
CI_DB_pdo_result Object
(
[num_rows] => 21
[conn_id] => PDO Object
(
)
[result_id] => PDOStatement Object
(
[queryString] => SELECT * FROM my_table WHERE is_active = 1
)
[result_array] => Array
(
)
[result_object] => Array
(
)
[custom_result_object] => Array
(
)
[current_row] => 0
[row_data] =>
)
Count is
[num_rows] => 21
what is missing/problem here?
I have found the issue. This thing happens because my application use PDO DB Driver. Not MySQL Driver.
$db['default']['dbdriver'] = 'pdo';
So I changed my model to support that.
$sql = "SELECT * FROM my_table WHERE is_active = 1";
$stmt1 = $this->db->conn_id->prepare($sql);
$stmt1->execute();
return $stmt1->fetchAll(PDO::FETCH_ASSOC);
Now it's working fine.
I am really sorry for your time people. And thank you very much for
trying to help.
Try this
$this->db->select('*');
$this->db->from('my_table');
$this->db->where('is_active', 1);
$query = $this->db->get();
$result = $query->result_array();
if (count($result) > 0) {
return $result;
}
else{
return FALSE;
}
You're not formatting the query array. So format it.
simple add
$query = $this->db->get()->result();
in your code
$this->db->select('*');
$this->db->from('my_table');
$this->db->where('is_active', 1);
$query = $this->db->get()->result();
if ($query->num_rows() > 0) {
//return $query->result();
return $query;
} else {
return FALSE;
}
if ($query->num_rows() > 0) then return $query->result() which returns results in object format.Like this..
if ($query->num_rows() > 0) {
return $query->result();
} else {
return FALSE;
}
Then use arrow(->) operator to retrieved required values.
For more see here Codeigniter Result Formats
If u want to get result you must use row() or result() function.
$this->db->select('*');
$this->db->from('my_table');
$this->db->where('is_active', 1);
$query = $this->db->get();
if ($query->num_rows() > 0) {
//return $query->result();
return $this->db->get()->result();
//return $query;
} else {
return FALSE;
}
$this->db->select('*');
$this->db->from('my_table');
$this->db->where('is_active', 1);
$query = $this->db->get();
$i = 0;
foreach ($query->result() as $row) {
$i++;
$data['id'] = $row->id;
$data['username'] = $row->username;
}
if ($i > 0) {
return $data;
}