Undefined Offset: 0 - Codeigniter - php

When I send a Request to this Page including POST-DATA ({"bot_hw_id":"2147483647"}), i get the following error:
<p>Severity: Notice</p>
<p>Message: Undefined offset: 0</p>
<p>Filename: models/prometheus_model.php</p>
<p>Line Number: 26</p>
My Code:
Controller(update_bot function):
[code]public function update_bot()
{
$bot_data = json_decode($this->input->post('bot_data'));
$to_update = array(
'bot_last_update' => time(),
'bot_ip' => $_SERVER['REMOTE_ADDR'],
'bot_port' => $_SERVER['REMOTE_PORT']
);
$bot_data = $this->prometheus_model->get_bot_data_by_hw_id(/*$bot_data->{'bot_hw_id'}*/$bot_data->{'bot_hw_id'});
//echo $bot_data['bot_id'];
print_r($bot_data);
$this->prometheus_model->update('bots', array('bot_id' => $bot_data['bot_id']), $to_update);
//var_dump($bot_data);
}
Model(prometheus_model):
<?php
class Prometheus_model extends CI_Model {
var $tables = array(
'bots' => 'bots'
);
function __construct() {
parent::__construct();
}
public function tablename($table = NULL) {
if(! isset($table)) return FALSE;
return $this->tables[$table];
}
public function get($table, $where = array(), $single = FALSE, $order = NULL) {
$this->db->where($where);
if(isset($order)) {
$this->db->order_by($order);
}
$q = $this->db->get_where($this->tablename($table),$where);
$result = $q->result_array();
if($single) {
return $result[0];
}
return $result;
}
public function update($table, $where = array(), $data) {
$this->db->update($this->tablename($table),$data,$where);
return $this->db->affected_rows();
}
public function insert($table, $data) {
$this->db->insert($this->tablename($table),$data);
return $this->db->insert_id();
}
public function delete($table, $where = array()) {
$this->db->delete($this->tablename($table),$where);
return $this->db->affected_rows();
}
public function explicit($query) {
$q = $this->db->query($query);
if(is_object($q)) {
return $q->result_array();
} else {
return $q;
}
}
public function num_rows($table, $where = NULL) {
if(isset($where)){
$this->db->where($where);
}
$q = $this->db->get($table);
return $q->num_rows();
}
public function get_bot_data_by_hw_id($bot_hw_id) {
$q = $this->get('bots', array('bot_hw_id' => $bot_hw_id), TRUE);
return $q;
}
}
?>;
How can i fix this error?

First, I should mention that it's not absolutely a good and safe idea to query user input exactly without any process (as we see in your code). It's better to have different models at least for each table.
Anyway...
This way your function will be corrected:
public function get($table, $where = array(), $single = FALSE, $order = NULL) {
$this->db->where($where);
if(isset($order)) {
$this->db->order_by($order);
}
$q = $this->db->get_where($this->tablename($table),$where);
$result = $q->result_array();
// You should use $q->num_rows() to detect the number of returned rows
if($q->num_rows() == 1) {
// Return the first row:
return $result[0];
}
return $result;
}
It returns the first row when there is only one, and brings an array when $q->num_rows() is not equal to 1.
Hope it helps

Related

Codeigniter Call to a member function row() on bool

I am getting error in my model file
An uncaught Exception was encountered
Type: Error
Message: Call to a member function row() on bool
Filename: application/models/Ledger_model.php
Line Number: 47
Backtrace:
File: application/controllers/Purchase.php
Line: 56
Function: get_branch_from_ledger_id
File: index.php
Line: 315
Function: require_once
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Ledger_model extends CI_Model
{
function __construct() {
parent::__construct();
}
public function index(){
}
/*
return all discount details to display list
*/
public function get_records(){
return $this->db->select('l.*,a.group_title')
->from('ledger l')
->join('account_group a','a.id = l.accountgroup_id')
->get()
->result();
}
public function get_records_by_accountgroup_category($category)
{
return $this->db->select('l.*,a.group_title')
->from('ledger l')
->join('account_group a','a.id = l.accountgroup_id')
->where_in('category',$category)
->get()
->result();
}
function get_single_record($id)
{
return $this->db->get_where("ledger",array('id'=>$id))->row();
}
function get_branch_from_ledger_id($id)
{
return $this->db->select('l.*,a.branch_id')
->from('ledger l')
->join('account_group a','a.id = l.accountgroup_id')
->where('l.id',$id)
->get()
->row()
->branch_id;
}
// public function get_records_by_branch($branch_id)
// {
// return $this->db->select('l.*,b.branch_name as branch_name,ag.category as category')
// ->from('ledger l')
// ->join('account_group ag','l.accountgroup_id = ag.id')
// ->join('account_group_branch agb','agb.account_group_id = l.accountgroup_id')
// ->join('branch b','b.branch_id = agb.branch_id')
// ->where('b.branch_id',$branch_id)
// ->get()
// ->result();
// }
function add_record($data)
{
if($this->db->insert('ledger',$data))
{
return $this->db->insert_id();
}else
{
return FALSE;
}
}
function edit_record($data,$id)
{
$this->db->where('id',$id);
if($this->db->update('ledger',$data)){
// echo $this->db->last_query();
// exit;
return true;
}else{
return false;
}
}
function delete_record($id)
{
// $ledger = $this->db->get_where('ledger',array('accountgroup_id'=>$id))->result();
// $no_ledger = sizeof($ledger);
// echo $no_users;
// exit;
// if($no_ledger > 0)
// {
// return $no_ledger;
// }
// else
// {
$this->db->where('id', $id);
if($this->db->delete('ledger'))
{
return true;
}
else
{
return false;
}
// }
}
public function getBranch()
{
$this->db->select('branch_id,branch_name');
$branch = $this->db->get('branch');
return $array = $branch->result_array();
}
public function getAccountGroup()
{
$this->db->select('id,group_title');
$acc_group = $this->db->get('account_group');
return $array = $acc_group->result_array();
}
public function addModel($data){
$sql = "insert into discount (discount_name,discount_value,user_id) values(?,?,?)";
if($this->db->query($sql,$data)){
return TRUE;
}
else{
return FALSE;
}
}
public function getRecord($id){
$sql = "select * from discount where discount_id = ?";
if($query = $this->db->query($sql,array($id))){
return $query->result();
}
else{
return FALSE;
}
}
public function editModel($data,$id){
$sql = "update discount set discount_name = ?,discount_value = ? where discount_id = ?";
if($this->db->query($sql,array($data['discount_name'],$data['discount_value'],$id))){
return TRUE;
}
else{
return FALSE;
}
}
public function deleteModel($id){
$sql = "delete from discount where discount_id = ?";
if($this->db->query($sql,array($id))){
return TRUE;
}
else{
return FALSE;
}
}
public function addLedger($data)
{
$this->db->insert('ledger', $data);
return $this->db->insert_id();
}
public function getledgerWithJoin($id)
{
$this->db->select('L.*,B.branch_name,A.group_title');
$this->db->from('ledger AS L');// I use aliasing make joins easier
$this->db->join('branch AS B', 'B.id = L.branch_id', 'INNER');
$this->db->join('account_group AS A', 'A.id = L.accountgroup_id', 'INNER');
$this->db->where('L.id',$id);
$q = $this->db->get();
if( $q->num_rows() > 0 )
{
return $q->row();
}
return FALSE;
}
public function getledgerById($id)
{
$q = $this->db->get_where('ledger', array('id' => $id), 1);
if( $q->num_rows() > 0 )
{
return $q->row();
}
return FALSE;
}
public function updateLedger($ledgerDetail,$id)
{
$ledgerData = array(
'branch_id' => $ledgerDetail['branch_id'],
'type' => $ledgerDetail['type'],
'title' => $ledgerDetail['title'],
'accountgroup_id' => $ledgerDetail['accountgroup_id'],
'opening_balance' => $ledgerDetail['opening_balance'],
'closing_balance' => $ledgerDetail['closing_balance'],
);
$this->db->where('id', $id);
if($this->db->update('ledger', $ledgerData)) {
return true;
}
return false;
}
public function getBillerLedgerDetail($ledger_id){
$user = $this->db->get_where('biller', array('ledger_id' => $ledger_id))->row();
if(is_null($user)){
return false;
}else{
return $user->biller_id;
}
}
public function getCustomerLedgerDetails($ledger_id){
$user = $this->db->get_where('customer', array('ledger_id' => $ledger_id))->row();
if(is_null($user)){
return false;
}else{
return $user->customer_id;
}
}
public function getBankLedgerDetails($ledger_id){
$user = $this->db->get_where('bank_account', array('ledger_id' => $ledger_id))->row();
if(is_null($user)){
return false;
}else{
return true;
}
}
public function getSuppliersLedgerDetails($ledger_id){
$user = $this->db->get_where('suppliers', array('ledger_id' => $ledger_id))->row();
if(is_null($user)){
return false;
}else{
return true;
}
}
public function getPurchaserLedgerDetails($ledger_id){
$user = $this->db->get_where('users', array('ledger_id' => $ledger_id))->row();
if(is_null($user)){
return false;
}else{
return $user->id;
}
}
public function deleteLedger($id)
{
if($this->db->delete('ledger', array('id' => $id))) {
return true;
}
return FALSE;
}
}
?>
It sounds like there were no results, so there was nothing to get. Try this to check for results before you attempt to grab it.
function get_branch_from_ledger_id($id)
{
$rows = $this->db->select('l.*,a.branch_id')
->from('ledger l')
->join('account_group a','a.id = l.accountgroup_id')
->where('l.id',$id)
->get();
return $rows ? $rows->row()->branch_id : false;
}

Handing the ID from the database to the URL - CodeIgniter

I am starting my game with CodeIgniter and I have a problem - I would like to download the ID - of the product from the database so that it will be passed to the URL address:
Example address:
http://localhost/crm-SEO/api/admin/domains/{{ID in database}}/notes
When I have other methods like:
domains/GET/6
domains/update/6
Everything works fine for me, but I can not grasp it that I have domains/{{ID}}/
My code i models:
public function get( $id = false)
{
if ( $id == false ) {
$q = $this->db->get('domains');
$q = $q->result();
}
else{
$this->db->where('id', $id);
$q = $this->db->get('domains');
$q = $q->row();
}
return $q;
}
public function update($domain)
{
$this->db->where('id', $domain['id'] );
$this->db->update('domains', $domain);
}
public function create($domain)
{
$this->db->insert('domains', $domain);
}
public function delete($domain)
{
$this->db->where('id', $domain['id'] );
$this->db->delete('domains');
}
My code in controller:
public function __construct()
{
parent::__construct();
$post = file_get_contents('php://input');
$_POST = json_decode($post,true);
$this->load->model('admin/domains_model');
}
public function get($id = false)
{
$result = $this->domains_model->get($id);
echo '{"records":' . json_encode( $result ) . '}';
}
public function update()
{
$domain = $this->input->post('domain');
$this->domains_model->update($domain);
}
public function create()
{
$domain = $this->input->post('domain');
$this->domains_model->create($domain);
}
public function delete()
{
$domain = $this->input->post('domain');
$this->domains_model->delete($domain);
}

Strange query generated in Codeigniter

Below is my code where I am calling three methods from three models to retrieve counts as below.
$this->load->model('orders_model');
$order_count = $this->orders_model->count_orders(array("executive_id" => $this->id));
$this->load->model('activities_model');
$activity_count = $this->activities_model->count_activities(array("users_id" => $this->id));
$this->load->model('leads_model');
$leads_count = $this->leads_model->count_leads(array("users_id" => $this->id));
And this is the query I am getting:
SELECT COUNT(*) AS numrows FROM orders, activities, leads WHERE executive_id = '5' AND users_id = '5' AND users_id = '5'
which leads to a database error
Why is this happening?
Orders_model
class Orders_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_orders($order_id = FALSE) {
if ($order_id === FALSE) {
$query = $this->db->get('orders');
return $query->result();
}
$this->db->where('id', $order_id);
$query = $this->db->get('orders');
return $query->result();
}
public function add_order($order_data = FALSE) {
if (!$order_data === FALSE) {
if (is_array($order_data)) {
return $this->db->insert('orders', $order_data);
} else {
return false;
}
} else {
return false;
}
}
public function update_order($order_update_data = FALSE, $order_update_condition = FALSE) {
if (!($order_update_data === FALSE && $order_update_condition === FALSE)) {
if (is_array($order_update_data) && is_array($order_update_condition)) {
return $this->db->update('orders', $order_update_data, $order_update_condition);
} else {
return false;
}
} else {
return false;
}
}
public function get_custom_orders($order_custom_condition = FALSE) {
if (!$order_custom_condition === FALSE) {
if (is_array($order_custom_condition)) {
#echo "Yes a parameter is passed which is also an array";
$this->db->where($order_custom_condition);
$query = $this->db->get('orders');
return $query->result();
}
}
}
public function get_last_ref_id() {
$query = $this->db->query('select sprx_ref_id from orders where id in (select max(id) from orders)');
foreach ($query->result() as $row) {
return $row->sprx_ref_id;
}
}
public function fetch_orders($limit, $start, $order_custom_condition) {
$this->db->limit($limit, $start);
$this->db->order_by("id", "desc");
$this->db->where($order_custom_condition);
$query = $this->db->get();
return $query->result();
}
public function count_orders($order_custom_condition) {
$this->db->where($order_custom_condition);
return $this->db->count_all_results('orders', FALSE);
}
}
Activities_model
class Activities_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_activities($activity_id = FALSE) {
if ($activity_id === FALSE) {
$query = $this->db->get('activities');
return $query->result();
}
$this->db->where('id', $activity_id);
#$this->db->order_by('id','ASC');
$query = $this->db->get('activities');
return $query->result();
}
public function add_activity($activity_data = FALSE) {
if (!$activity_data === FALSE) {
if (is_array($activity_data)) {
return $this->db->insert('activities', $activity_data);
} else {
return false;
}
} else {
return false;
}
}
public function update_activity($activity_update_data = FALSE, $activity_update_condition = FALSE) {
if (!($activity_update_data === FALSE && $activity_update_condition)) {
if (is_array($activity_update_data) && is_array($activity_update_condition)) {
return $this->db->update('activities', $activity_update_data, $activity_update_condition);
} else {
return false;
}
} else {
return false;
}
}
public function get_custom_activities($activity_custom_condition = FALSE) {
if (!$activity_custom_condition === FALSE) {
if (is_array($activity_custom_condition)) {
#echo "Yes a parameter is passed which is also an array";
$this->db->where($activity_custom_condition);
$query = $this->db->get('activities');
return $query->result();
}
}
}
public function fetch_activities($limit, $start, $custom_condition) {
$this->db->limit($limit, $start);
$this->db->order_by("id", "desc");
$this->db->where($custom_condition);
$query = $this->db->get();
return $query->result();
}
public function count_activities($custom_condition) {
$this->db->where($custom_condition);
return $this->db->count_all_results('activities', FALSE);
}
}
Leads_model
class Leads_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_leads($lead_id = FALSE) {
if ($lead_id === FALSE) {
$query = $this->db->get('leads');
return $query->result();
}
$this->db->where('id', $lead_id);
$query = $this->db->get('leads');
return $query->result();
}
public function add_lead($lead_data = FALSE) {
if (!$lead_data === FALSE) {
if (is_array($lead_data)) {
return $this->db->insert('leads', $lead_data);
} else {
return false;
}
} else {
return false;
}
}
public function update_lead($lead_update_data = FALSE, $lead_update_condition = FALSE) {
if (!($lead_update_data === FALSE && $lead_update_condition)) {
if (is_array($lead_update_data) && is_array($lead_update_condition)) {
return $this->db->update('leads', $lead_update_data, $lead_update_condition);
} else {
return false;
}
} else {
return false;
}
}
public function get_custom_leads($lead_custom_condition = FALSE) {
if (!$lead_custom_condition === FALSE) {
if (is_array($lead_custom_condition)) {
#echo "Yes a parameter is passed which is also an array";
$this->db->where($lead_custom_condition);
$query = $this->db->get('leads');
return $query->result();
} else {
return false;
}
} else {
return false;
}
}
public function fetch_leads($limit, $start, $lead_custom_condition) {
$this->db->limit($limit, $start);
$this->db->order_by("id", "desc");
$this->db->where($lead_custom_condition);
$query = $this->db->get();
return $query->result();
}
public function count_leads($lead_custom_condition) {
$this->db->where($lead_custom_condition);
return $this->db->count_all_results('leads', FALSE);
}
}
As far as i understand, you are surprised why the query builder uses additional parameter from the previous query.
You've to reset your Query according to docs
which means all your "count_" functions should be like
public function count_leads($lead_custom_condition) {
$this->db->where($lead_custom_condition);
return $this->db->count_all_results('leads');
}
obviously you did set the false flag on purpose - but i'm not sure why ;)
I believe that the error message is only a symptom, not the actual cause of the issue
. The way I read your code is that the count_*() methods in each of the 3 models should return the count from their respective tables only.
However, the way you wrote your count functions results in the query builder adding the tables and conditions to the overall query, not executing them just on the single tables
$this->db->where($custom_condition); <-- this adds a new where condition using "and" operator
return $this->db->count_all_results('activities', FALSE); <-- just adds another table without resetting the others
I would add a $this->db->reset_query(); line as the 1st line in each of the 3 count_*() methods to force the query builder to start from scratch.
The problem is due to using the second argument to $this->db->count_all_results(). When you set the second argument to FALSE then $this->db will not clear any select statements from its cache. Then each successive call to count_all_results() will include the table from any prior call to the function. The solution is simple - don't use the second parameter.
Change
return $this->db->count_all_results('activities', FALSE);
to
return $this->db->count_all_results('activities');
Not related to your problem but something that will improve your code is changing this
public function get_orders($order_id = FALSE) {
if ($order_id === FALSE) {
$query = $this->db->get('orders');
return $query->result();
}
$this->db->where('id', $order_id);
$query = $this->db->get('orders');
return $query->result();
}
to
public function get_orders($order_id = NULL) {
if (!empty($order_id))
{
$this->db->where('id', $order_id);
}
$query = $this->db->get('orders');
return $query->result();
}
Changing the argument default to NULL and using !empty($order_id) helps because it protects against and empty string or empty array being given as the argument. (Ready about empty() here.)
This new logic also keeps the code DRY - your not repeating the two line of code to get and return results.
Many of your other model functions could be cleaner too. For instance
public function add_order($order_data = FALSE) {
if (!$order_data === FALSE) {
if (is_array($order_data)) {
return $this->db->insert('orders', $order_data);
} else {
return false;
}
} else {
return false;
}
}
would be cleaner written like this
public function add_order($order_data = NULL) {
if (!empty($order_data) && is_array($order_data))
{
return $this->db->insert('orders', $order_data);
}
return false;
}
Sorry for nitpicking your code - I couldn't help myself.

run multiple select quires in one page using codeigniter

Hello any one can tell that how to load two function from model class in controller one method. I want to run multiple select quires in one page using codeigniter:
Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$select1 = $this->insertmodel->find($id);
$select2 = $this->insertmodel->detail_list();
$data = array();
$this->load->view('home/property_detail', ['select1'=>$select1], ['select2'=>$select2]);
//$this->load->view('home/property_detail', ['select2'=>$select2]);
}
Model
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list(){
$query1 = $this->db->query("select * from article");
return $query1->result();
}
In Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->find($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}
In Model
public function find($id)
{
$query = $this->db->get_where('article', array('id' => $id), 0, 0)->get();
if( $query->num_rows() > 0 )
{
$result = $query->result_array();
return $result;
}
else
{
return false;
}
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
$result = $query1->result_array();
return $result;
}
In View
foreach ($select2 as $item) {
# your foreach lop goes here
}
As well check empty() before passing it to the foreach loop
As an alternative to #Rijin's useful answer newMethod() can make calls to the existing model methods. This might be useful if you don't want to break the interface already created for the model because you are using find($id) and detail_list() in other code.
Model:
public function find($id)
{
$query = $this->db->from('article')->where(['id' => $id])->get();
if($query->num_rows())
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
public function newMethod($id)
{
$result['select1'] = $this->find($id);
if($result['select1'] !== FALSE)
{
$result['select2'] = $this->detail_list();
return $result;
}
return FALSE;
}
Controller:
public function property_detail($id)
{
$this->load->model('insertmodel');
$data = $this->insertmodel->newMethod($id);
$this->load->view('home/property_detail', $data);
}
Model :
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
Controller :
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->newMethod($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}

how to debug this? Call to a member function result() on a non-object

Please Help.. im using CODEIGNITER
I want to print the value of my userlevel and email using echo.. to know if im getting the right data from the database.. then suddenly this error keep bugging me.
beginner in CODEIGNITER need some help here.. thanks a lot!
in my controller:
if ($this->is_validated($rules))
{
$where = array('email' => $this->input->post('txt_email'));
$data['query'] = $this->database_model->select_userlevel('userlevel', $where);
$user = $data['query'];
foreach($user->result() as $row)
{
echo $row->email;
echo $row->userlevel;
}
}
in my model:
public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return true;
}
else
{
echo 'no value';
return false;
}
}
Instead of return true you need to return your query in model
Model
public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return $query;// return query here
}
else
{
return false;
}
Controller
$user = $this->database_model->select_userlevel('userlevel', $where);
foreach($user->result() as $row)
{
// your code here
}
UPDATED FOR better solution and proper use of MVC.
Instead of return query you need to return data from your model
MODEL
public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return $query->result(); // return your result
}
else
{
return false;
}
}
CONTROLLER
$user = $this->database_model->select_userlevel('userlevel', $where);
foreach ($user as $row) {
// your code here
}
$user is not an active record object, so it does not have a reference to the result method.
All of this logic belongs in the Model and not the Controller.
Controller
public function doSomething()
{
if( !$this->is_validated($rules) )
return;
$email = $this->input->post('txt_email');
$users = $this->database_model->select_userlevel($email);
// for debugging
var_dump( $users )
die();
}
Model
public function select_userlevel($email)
{
$query = $this->db->select()
->where('email', $email)
->get('userlevel');
return ( $query->num_rows() > 0) ? $query->result() : false;
}

Categories